Remove special chars from side identifiers

This commit is contained in:
Yohann D'ANELLO 2020-02-10 16:47:05 +01:00
parent e4d1ed852f
commit dc23ac0396
1 changed files with 10 additions and 2 deletions

View File

@ -4,6 +4,7 @@
import json import json
import urllib.request import urllib.request
import unicodedata
from django.forms import ModelForm from django.forms import ModelForm
@ -102,8 +103,8 @@ class MediaAdminForm(ModelForm):
if ',' not in author_name and ' ' in author_name: if ',' not in author_name and ' ' in author_name:
author_name = author_name.split(' ')[1] author_name = author_name.split(' ')[1]
side_identifier = "{:.3} {:.3}".format( side_identifier = "{:.3} {:.3}".format(
author_name.upper(), author_name,
self.cleaned_data['title'].upper(), ) self.cleaned_data['title'], )
if self.cleaned_data['subtitle']: if self.cleaned_data['subtitle']:
start = self.cleaned_data['subtitle'].split(' ')[0] \ start = self.cleaned_data['subtitle'].split(' ')[0] \
@ -112,6 +113,13 @@ class MediaAdminForm(ModelForm):
if start.isnumeric(): if start.isnumeric():
side_identifier += " {:0>2}".format(start, ) side_identifier += " {:0>2}".format(start, )
# Normalize side identifier, in order to remove accents
side_identifier = ''.join(
char
for char in unicodedata.normalize('NFKD', side_identifier.casefold())
if all(not unicodedata.category(char).startswith(cat)
for cat in {'M', 'P', 'Z', 'C'}) or char == ' '
).casefold().upper()
self.cleaned_data['side_identifier'] = side_identifier self.cleaned_data['side_identifier'] = side_identifier
return self.cleaned_data return self.cleaned_data