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 slugify import slugify
11 from isbn.management.commands.import_onix import UNKNOWN
12 from isbn.models import ONIXRecord, ISBNPool
13 from isbn.utils import isbn_data, PRODUCT_FORMS, PRODUCT_FORM_DETAILS
14 from librarian.parser import WLDocument
17 class WLISBNForm(forms.Form):
18 platform_url = forms.URLField(label='Adres na platformie')
19 publishing_date = forms.DateField(label='Data publikacji', initial=date.today)
21 def prepare_data(self):
22 platform_url = self.cleaned_data['platform_url']
23 platform_slug = platform_url.strip('/').split('/')[-1]
24 xml_url = 'https://redakcja.wolnelektury.pl/documents/book/%s/xml' % platform_slug
25 doc = WLDocument.from_file(urlopen(xml_url))
27 data['publishing_date'] = self.cleaned_data['publishing_date']
31 class WLConfirmForm(WLISBNForm):
32 platform_url = forms.URLField(widget=forms.HiddenInput)
33 publishing_date = forms.DateField(widget=forms.HiddenInput)
36 def contributors(data):
37 person_name = data['name']
38 corporate_name = data['corporate_name']
40 # assuming that unknown can't be a co-author
41 if person_name == UNKNOWN:
42 return [{'role': 'A01', 'unnamed': '01'}]
44 return [{'role': 'A01', 'name': name} for name in person_name.split('; ')]
46 return [{'role': 'A01', 'corporate_name': name} for name in corporate_name.split('; ')]
49 data = self.prepare_data()
50 for file_format in data['formats']:
51 data['product_form'] = PRODUCT_FORMS[file_format]
52 data['product_form_detail'] = PRODUCT_FORM_DETAILS[file_format]
53 data['contributors'] = json.dumps(self.contributors(data))
54 ONIXRecord.new_record(purpose=ISBNPool.PURPOSE_WL, data=data)
58 class FNPISBNForm(forms.Form):
65 ('SOFT', 'Miękka oprawa'),
74 title = forms.CharField()
75 authors = forms.CharField(help_text='wartości oddzielone przecinkami lub „Wielu autorów”')
76 formats = forms.MultipleChoiceField(choices=FORMAT_CHOICES)
77 language = forms.ChoiceField(choices=LANGUAGE_CHOICES)
78 publishing_date = forms.DateField()
80 def prepare_author(self, name):
81 if name == 'Wielu autorów':
82 return {'role': 'A01', 'unnamed': '04'}
84 first_name, last_name = [s.strip() for s in name.rsplit(' ', 1)]
85 output_name = '%s, %s' % (last_name, first_name)
87 output_name = name.strip()
88 return {'role': 'A01', 'name': output_name}
91 return slugify('fnp %s %s' % (self.cleaned_data['authors'], self.cleaned_data['title']))
95 'title': self.cleaned_data['title'],
96 'language': self.cleaned_data['language'],
97 'publishing_date': self.cleaned_data['publishing_date'],
98 'contributors': json.dumps([self.prepare_author(a) for a in self.cleaned_data['authors'].split(',')]),
99 'edition_type': 'NED',
100 'imprint': 'Fundacja Nowoczesna Polska',
101 'dc_slug': self.slug(),
103 formats = self.cleaned_data['formats']
104 for book_format in formats:
105 data['product_form'] = PRODUCT_FORMS[book_format]
106 if book_format in PRODUCT_FORM_DETAILS:
107 data['product_form_detail'] = PRODUCT_FORM_DETAILS[book_format]
109 del data['product_form_detail']
110 ONIXRecord.new_record('FNP', data)