Add grammar to tags. Also: respect custom tag slugs on publishing.
[wolnelektury.git] / src / isbn / forms.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. 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 django.utils.translation import gettext_lazy as _
10 from slugify import slugify
11
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
16
17
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)
21
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))
27         data = isbn_data(doc)
28         data['publishing_date'] = self.cleaned_data['publishing_date']
29         return data
30
31
32 class WLConfirmForm(WLISBNForm):
33     platform_url = forms.URLField(widget=forms.HiddenInput)
34     publishing_date = forms.DateField(widget=forms.HiddenInput)
35
36     @staticmethod
37     def contributors(data):
38         person_name = data['name']
39         corporate_name = data['corporate_name']
40         if person_name:
41             # assuming that unknown can't be a co-author
42             if person_name == UNKNOWN:
43                 return [{'role': 'A01', 'unnamed': '01'}]
44             else:
45                 return [{'role': 'A01', 'name': name} for name in person_name.split('; ')]
46         if corporate_name:
47             return [{'role': 'A01', 'corporate_name': name} for name in corporate_name.split('; ')]
48
49     def save(self):
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)
56         return data
57
58
59 class FNPISBNForm(forms.Form):
60     FORMAT_CHOICES = (
61         ('HTML', 'HTML'),
62         ('PDF', 'PDF'),
63         ('EPUB', 'ePUB'),
64         ('MOBI', 'MOBI'),
65         ('TXT', 'TXT'),
66         ('SOFT', _('Soft cover book')),
67     )
68     LANGUAGE_CHOICES = (
69         ('pol', 'polski'),
70         ('eng', 'angielski'),
71         ('ger', 'niemiecki'),
72         ('fre', 'francuski'),
73     )
74
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()
80
81     def prepare_author(self, name):
82         if name == 'Wielu autorów':
83             return {'role': 'A01', 'unnamed': '04'}
84         if ' ' in name:
85             first_name, last_name = [s.strip() for s in name.rsplit(' ', 1)]
86             output_name = '%s, %s' % (last_name, first_name)
87         else:
88             output_name = name.strip()
89         return {'role': 'A01', 'name': output_name}
90
91     def slug(self):
92         return slugify('fnp %s %s' % (self.cleaned_data['authors'], self.cleaned_data['title']))
93
94     def save(self):
95         data = {
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(),
103         }
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]
109             else:
110                 del data['product_form_detail']
111             ONIXRecord.new_record('FNP', data)