mirror of
https://gitlab.crans.org/mediatek/med.git
synced 2025-06-29 18:31:09 +02:00
Less complexity for download_data_openlibrary
This commit is contained in:
@ -48,37 +48,40 @@ class MediaAdminForm(ModelForm):
|
||||
data = data['ISBN:' + isbn]
|
||||
if 'url' in data:
|
||||
# Fill the data
|
||||
self.cleaned_data['external_url'] = data['url']
|
||||
if 'title' in data:
|
||||
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"]
|
||||
split = data['publish_date'].replace(',', '').split(' ')
|
||||
self.cleaned_data['publish_date'] = "{}-{:02d}-{:02d}"\
|
||||
.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()
|
||||
for author in data['authors']:
|
||||
author_obj = Auteur.objects.get_or_create(
|
||||
name=author['name'])[0]
|
||||
self.cleaned_data['authors'].append(author_obj)
|
||||
self.parse_data_openlibrary(data)
|
||||
return True
|
||||
return False
|
||||
|
||||
def parse_data_openlibrary(self, data):
|
||||
self.cleaned_data['external_url'] = data['url']
|
||||
if 'title' in data:
|
||||
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"]
|
||||
split = data['publish_date'].replace(',', '').split(' ')
|
||||
self.cleaned_data['publish_date'] = "{}-{:02d}-{:02d}" \
|
||||
.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()
|
||||
for author in data['authors']:
|
||||
author_obj = Auteur.objects.get_or_create(
|
||||
name=author['name'])[0]
|
||||
self.cleaned_data['authors'].append(author_obj)
|
||||
|
||||
def clean(self):
|
||||
"""
|
||||
If user fetch ISBN data, then download data before validating the form
|
||||
|
Reference in New Issue
Block a user