1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from datetime import date
6 from urllib.request import urlopen
8 from django import forms
9 from django.utils.translation import gettext_lazy as _
10 from slugify import slugify
12 from isbn.management.commands.import_onix import UNKNOWN
13 from isbn.models import ONIXRecord, ISBNPool
14 from isbn.utils import isbn_data, PRODUCT_FORMS, PRODUCT_FORM_DETAILS
15 from librarian.parser import WLDocument
18 class WLISBNForm(forms.Form):
19 platform_url = forms.URLField(label='Adres na platformie')
20 publishing_date = forms.DateField(label='Data publikacji', initial=date.today)
22 def prepare_data(self):
23 platform_url = self.cleaned_data['platform_url']
24 platform_slug = platform_url.strip('/').split('/')[-1]
25 xml_url = 'https://redakcja.wolnelektury.pl/documents/book/%s/xml' % platform_slug
26 doc = WLDocument.from_file(urlopen(xml_url))
28 data['publishing_date'] = self.cleaned_data['publishing_date']
32 class WLConfirmForm(WLISBNForm):
33 platform_url = forms.URLField(widget=forms.HiddenInput)
34 publishing_date = forms.DateField(widget=forms.HiddenInput)
37 def contributors(data):
38 person_name = data['name']
39 corporate_name = data['corporate_name']
41 # assuming that unknown can't be a co-author
42 if person_name == UNKNOWN:
43 return [{'role': 'A01', 'unnamed': '01'}]
45 return [{'role': 'A01', 'name': name} for name in person_name.split('; ')]
47 return [{'role': 'A01', 'corporate_name': name} for name in corporate_name.split('; ')]
50 data = self.prepare_data()
51 for file_format in data['formats']:
52 data['product_form'] = PRODUCT_FORMS[file_format]
53 data['product_form_detail'] = PRODUCT_FORM_DETAILS[file_format]
54 data['contributors'] = json.dumps(self.contributors(data))
55 ONIXRecord.new_record(purpose=ISBNPool.PURPOSE_WL, data=data)
59 class FNPISBNForm(forms.Form):
66 ('SOFT', _('Soft cover book')),
75 title = forms.CharField()
76 authors = forms.CharField(help_text='wartości oddzielone przecinkami lub „Wielu autorów”')
77 formats = forms.MultipleChoiceField(choices=FORMAT_CHOICES)
78 language = forms.ChoiceField(choices=LANGUAGE_CHOICES)
79 publishing_date = forms.DateField()
81 def prepare_author(self, name):
82 if name == 'Wielu autorów':
83 return {'role': 'A01', 'unnamed': '04'}
85 first_name, last_name = [s.strip() for s in name.rsplit(' ', 1)]
86 output_name = '%s, %s' % (last_name, first_name)
88 output_name = name.strip()
89 return {'role': 'A01', 'name': output_name}
92 return slugify('fnp %s %s' % (self.cleaned_data['authors'], self.cleaned_data['title']))
96 'title': self.cleaned_data['title'],
97 'language': self.cleaned_data['language'],
98 'publishing_date': self.cleaned_data['publishing_date'],
99 'contributors': json.dumps([self.prepare_author(a) for a in self.cleaned_data['authors'].split(',')]),
100 'edition_type': 'NED',
101 'imprint': 'Fundacja Nowoczesna Polska',
102 'dc_slug': self.slug(),
104 formats = self.cleaned_data['formats']
105 for book_format in formats:
106 data['product_form'] = PRODUCT_FORMS[book_format]
107 if book_format in PRODUCT_FORM_DETAILS:
108 data['product_form_detail'] = PRODUCT_FORM_DETAILS[book_format]
110 del data['product_form_detail']
111 ONIXRecord.new_record('FNP', data)