1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-11-26 21:27:11 +00:00

Clean code

This commit is contained in:
Yohann D'ANELLO 2020-02-10 11:47:59 +01:00
parent 7fd8e92371
commit d3a4a246d9

View File

@ -43,6 +43,7 @@ class MediaAdminForm(ModelForm):
"&format=json&jscmd=data".format(isbn) "&format=json&jscmd=data".format(isbn)
with urllib.request.urlopen(api_url) as url: with urllib.request.urlopen(api_url) as url:
data = json.loads(url.read().decode()) data = json.loads(url.read().decode())
if data and data['ISBN:' + isbn]: if data and data['ISBN:' + isbn]:
data = data['ISBN:' + isbn] data = data['ISBN:' + isbn]
if 'url' in data: if 'url' in data:
@ -52,19 +53,22 @@ class MediaAdminForm(ModelForm):
self.cleaned_data['title'] = data['title'] self.cleaned_data['title'] = data['title']
if 'subtitle' in data: if 'subtitle' in data:
self.cleaned_data['subtitle'] = data['subtitle'] self.cleaned_data['subtitle'] = data['subtitle']
if 'number_of_pages' in data: if 'number_of_pages' in data:
self.cleaned_data['number_of_pages'] = \ self.cleaned_data['number_of_pages'] = \
data['number_of_pages'] data['number_of_pages']
elif not self.cleaned_data['number_of_pages']: elif not self.cleaned_data['number_of_pages']:
self.cleaned_data['number_of_pages'] = 0 self.cleaned_data['number_of_pages'] = 0
if 'publish_date' in data: if 'publish_date' in data:
months = ['January', 'February', "March", "April", "Mai", months = ['January', 'February', "March", "April", "Mai",
"June", "July", "August", "September", "June", "July", "August", "September",
"October", "November","December"] "October", "November", "December"]
split = data['publish_date'].replace(',', '').split(' ') split = data['publish_date'].replace(',', '').split(' ')
self.cleaned_data['publish_date'] = "{}-{:02d}-{:02d}"\ self.cleaned_data['publish_date'] = "{}-{:02d}-{:02d}"\
.format(split[2], months.index(split[0])+1, .format(split[2], months.index(split[0]) + 1,
int(split[1]),) int(split[1]),)
if 'authors' in data: if 'authors' in data:
if 'author' not in self.cleaned_data: if 'author' not in self.cleaned_data:
self.cleaned_data['authors'] = list() self.cleaned_data['authors'] = list()
@ -99,8 +103,8 @@ class MediaAdminForm(ModelForm):
self.cleaned_data['title'].upper(), ) self.cleaned_data['title'].upper(), )
if self.cleaned_data['subtitle']: if self.cleaned_data['subtitle']:
start = self.cleaned_data['subtitle'] \ start = self.cleaned_data['subtitle'].split(' ')[0] \
.split(' ')[0].replace('.', '') .replace('.', '')
if start.isnumeric(): if start.isnumeric():
side_identifier += " {:0>2}".format(start, ) side_identifier += " {:0>2}".format(start, )