2019-08-02 12:57:53 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2019-08-11 08:40:39 +00:00
|
|
|
import json
|
|
|
|
import urllib.request
|
|
|
|
|
2019-08-02 12:57:53 +00:00
|
|
|
from django.forms import ModelForm
|
|
|
|
|
2020-02-10 10:29:26 +00:00
|
|
|
from .models import Auteur
|
2019-08-19 10:17:49 +00:00
|
|
|
from .scraper import BedetequeScraper
|
|
|
|
|
2019-08-11 08:40:39 +00:00
|
|
|
|
|
|
|
class MediaAdminForm(ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-08-15 14:30:44 +00:00
|
|
|
isbn_field = self.fields.get('isbn')
|
|
|
|
if isbn_field:
|
|
|
|
isbn_field.widget.template_name = "media/isbn_button.html"
|
|
|
|
isbn_field.widget.attrs.update({'autofocus': 'autofocus'})
|
2019-08-11 08:40:39 +00:00
|
|
|
|
2019-08-19 10:17:49 +00:00
|
|
|
def download_data_bedeteque(self, isbn):
|
|
|
|
"""
|
|
|
|
Download data from bedeteque
|
|
|
|
:return True if success
|
2019-08-11 08:40:39 +00:00
|
|
|
"""
|
2019-08-19 10:17:49 +00:00
|
|
|
scraper = BedetequeScraper()
|
|
|
|
r = scraper.search_by_isbn(isbn)
|
|
|
|
if not r:
|
|
|
|
return False
|
|
|
|
# If results, then take the most accurate
|
|
|
|
data = scraper.scrap_bd_info(r[0])
|
|
|
|
self.cleaned_data.update(data)
|
2020-02-10 10:29:26 +00:00
|
|
|
print(self.cleaned_data)
|
2019-08-19 10:17:49 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def download_data_openlibrary(self, isbn):
|
|
|
|
"""
|
|
|
|
Download data from openlibrary
|
|
|
|
:return True if success
|
2019-08-11 08:40:39 +00:00
|
|
|
"""
|
|
|
|
api_url = "https://openlibrary.org/api/books?bibkeys=ISBN:{}" \
|
|
|
|
"&format=json&jscmd=data".format(isbn)
|
|
|
|
with urllib.request.urlopen(api_url) as url:
|
|
|
|
data = json.loads(url.read().decode())
|
2020-02-10 10:47:59 +00:00
|
|
|
|
2019-08-11 08:40:39 +00:00
|
|
|
if data and data['ISBN:' + isbn]:
|
|
|
|
data = data['ISBN:' + isbn]
|
2019-08-11 09:19:24 +00:00
|
|
|
if 'url' in data:
|
2019-08-19 10:17:49 +00:00
|
|
|
# Fill the data
|
2020-02-10 11:08:48 +00:00
|
|
|
self.parse_data_openlibrary(data)
|
|
|
|
return True
|
|
|
|
return False
|
2020-02-10 10:47:59 +00:00
|
|
|
|
2020-02-10 11:08:48 +00:00
|
|
|
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']
|
2020-02-10 10:47:59 +00:00
|
|
|
|
2020-02-10 11:08:48 +00:00
|
|
|
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
|
2020-02-10 10:47:59 +00:00
|
|
|
|
2020-02-10 11:08:48 +00:00
|
|
|
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)
|
2019-08-11 08:40:39 +00:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
"""
|
|
|
|
If user fetch ISBN data, then download data before validating the form
|
|
|
|
"""
|
2020-02-10 02:08:08 +00:00
|
|
|
super().clean()
|
|
|
|
|
2019-08-11 08:40:39 +00:00
|
|
|
if "_continue" in self.request.POST:
|
|
|
|
isbn = self.cleaned_data.get('isbn')
|
|
|
|
if isbn:
|
2019-08-19 10:17:49 +00:00
|
|
|
# ISBN is present, try with bedeteque
|
|
|
|
scrap_result = self.download_data_bedeteque(isbn)
|
|
|
|
if not scrap_result:
|
|
|
|
# Try with OpenLibrary
|
|
|
|
self.download_data_openlibrary(isbn)
|
2019-08-11 08:40:39 +00:00
|
|
|
|
2020-02-10 10:29:26 +00:00
|
|
|
if self.cleaned_data['authors']:
|
|
|
|
author_name = self.cleaned_data['authors'][0].name
|
|
|
|
if ',' not in author_name and ' ' in author_name:
|
|
|
|
author_name = author_name.split(' ')[1]
|
|
|
|
side_identifier = "{:.3} {:.3}".format(
|
|
|
|
author_name.upper(),
|
|
|
|
self.cleaned_data['title'].upper(), )
|
|
|
|
|
|
|
|
if self.cleaned_data['subtitle']:
|
2020-02-10 10:47:59 +00:00
|
|
|
start = self.cleaned_data['subtitle'].split(' ')[0] \
|
|
|
|
.replace('.', '')
|
2020-02-10 10:29:26 +00:00
|
|
|
|
|
|
|
if start.isnumeric():
|
|
|
|
side_identifier += " {:0>2}".format(start, )
|
|
|
|
|
|
|
|
self.cleaned_data['side_identifier'] = side_identifier
|
|
|
|
|
2020-02-10 02:08:08 +00:00
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
def _clean_fields(self):
|
|
|
|
for name, field in self.fields.items():
|
|
|
|
# value_from_datadict() gets the data from the data dictionaries.
|
|
|
|
# Each widget type knows how to retrieve its own data, because some
|
|
|
|
# widgets split data over several HTML fields.
|
|
|
|
if field.disabled:
|
|
|
|
value = self.get_initial_for_field(field, name)
|
|
|
|
else:
|
2020-02-10 03:59:39 +00:00
|
|
|
value = field.widget.value_from_datadict(
|
|
|
|
self.data, self.files, self.add_prefix(name))
|
2020-02-10 02:08:08 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
try:
|
|
|
|
# We don't want to check a field when we enter an ISBN.
|
2020-02-10 03:59:39 +00:00
|
|
|
if "_continue" not in self.request.POST \
|
|
|
|
or not self.cleaned_data.get('isbn'):
|
2020-02-10 02:08:08 +00:00
|
|
|
value = field.clean(value)
|
|
|
|
self.cleaned_data[name] = value
|
|
|
|
if hasattr(self, 'clean_%s' % name):
|
|
|
|
value = getattr(self, 'clean_%s' % name)()
|
|
|
|
self.cleaned_data[name] = value
|
|
|
|
except ValidationError as e:
|
|
|
|
self.add_error(name, e)
|