From d3a4a246d9bf463cdbff61caf41294f918ac6612 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 10 Feb 2020 11:47:59 +0100 Subject: [PATCH] Clean code --- media/forms.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/media/forms.py b/media/forms.py index 8dd1df2..c16cb92 100644 --- a/media/forms.py +++ b/media/forms.py @@ -43,6 +43,7 @@ class MediaAdminForm(ModelForm): "&format=json&jscmd=data".format(isbn) with urllib.request.urlopen(api_url) as url: data = json.loads(url.read().decode()) + if data and data['ISBN:' + isbn]: data = data['ISBN:' + isbn] if 'url' in data: @@ -52,19 +53,22 @@ class MediaAdminForm(ModelForm): self.cleaned_data['title'] = data['title'] if 'subtitle' in data: self.cleaned_data['subtitle'] = data['subtitle'] + if 'number_of_pages' in data: self.cleaned_data['number_of_pages'] = \ data['number_of_pages'] elif not self.cleaned_data['number_of_pages']: self.cleaned_data['number_of_pages'] = 0 + if 'publish_date' in data: months = ['January', 'February', "March", "April", "Mai", "June", "July", "August", "September", - "October", "November","December"] + "October", "November", "December"] split = data['publish_date'].replace(',', '').split(' ') 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]),) + if 'authors' in data: if 'author' not in self.cleaned_data: self.cleaned_data['authors'] = list() @@ -99,8 +103,8 @@ class MediaAdminForm(ModelForm): self.cleaned_data['title'].upper(), ) if self.cleaned_data['subtitle']: - start = self.cleaned_data['subtitle'] \ - .split(' ')[0].replace('.', '') + start = self.cleaned_data['subtitle'].split(' ')[0] \ + .replace('.', '') if start.isnumeric(): side_identifier += " {:0>2}".format(start, )