fix
[wolnelektury.git] / src / isbn / forms.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from datetime import date
5 import json
6 from urllib.request import urlopen
7
8 from django import forms
9 from slugify import slugify
10
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
15
16
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)
20
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))
26         data = isbn_data(doc)
27         data['publishing_date'] = self.cleaned_data['publishing_date']
28         return data
29
30
31 class WLConfirmForm(WLISBNForm):
32     platform_url = forms.URLField(widget=forms.HiddenInput)
33     publishing_date = forms.DateField(widget=forms.HiddenInput)
34
35     @staticmethod
36     def contributors(data):
37         person_name = data['name']
38         corporate_name = data['corporate_name']
39         if person_name:
40             # assuming that unknown can't be a co-author
41             if person_name == UNKNOWN:
42                 return [{'role': 'A01', 'unnamed': '01'}]
43             else:
44                 return [{'role': 'A01', 'name': name} for name in person_name.split('; ')]
45         if corporate_name:
46             return [{'role': 'A01', 'corporate_name': name} for name in corporate_name.split('; ')]
47
48     def save(self):
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)
55         return data
56
57
58 class FNPISBNForm(forms.Form):
59     FORMAT_CHOICES = (
60         ('HTML', 'HTML'),
61         ('PDF', 'PDF'),
62         ('EPUB', 'ePUB'),
63         ('MOBI', 'MOBI'),
64         ('TXT', 'TXT'),
65         ('SOFT', 'Miękka oprawa'),
66     )
67     LANGUAGE_CHOICES = (
68         ('pol', 'polski'),
69         ('eng', 'angielski'),
70         ('ger', 'niemiecki'),
71         ('fre', 'francuski'),
72     )
73
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()
79
80     def prepare_author(self, name):
81         if name == 'Wielu autorów':
82             return {'role': 'A01', 'unnamed': '04'}
83         if ' ' in name:
84             first_name, last_name = [s.strip() for s in name.rsplit(' ', 1)]
85             output_name = '%s, %s' % (last_name, first_name)
86         else:
87             output_name = name.strip()
88         return {'role': 'A01', 'name': output_name}
89
90     def slug(self):
91         return slugify('fnp %s %s' % (self.cleaned_data['authors'], self.cleaned_data['title']))
92
93     def save(self):
94         data = {
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(),
102         }
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]
108             else:
109                 del data['product_form_detail']
110             ONIXRecord.new_record('FNP', data)