More Py3 compatibility fixes.
[wolnelektury.git] / src / isbn / forms.py
1 # -*- coding: utf-8 -*-
2 from datetime import date
3 from urllib.request import urlopen
4
5 from django import forms
6 from django.utils.translation import ugettext_lazy as _
7 from slugify import slugify
8
9 from isbn.management.commands.import_onix import UNKNOWN
10 from isbn.models import ONIXRecord, ISBNPool
11 from isbn.utils import isbn_data, PRODUCT_FORMS, PRODUCT_FORM_DETAILS
12 from librarian.parser import WLDocument
13
14
15 class WLISBNForm(forms.Form):
16     platform_url = forms.URLField(label=u'Adres na platformie')
17     publishing_date = forms.DateField(label=u'Data publikacji', initial=date.today)
18
19     def prepare_data(self):
20         platform_url = self.cleaned_data['platform_url']
21         platform_slug = platform_url.strip('/').split('/')[-1]
22         xml_url = 'https://redakcja.wolnelektury.pl/documents/book/%s/xml' % platform_slug
23         doc = WLDocument.from_file(urlopen(xml_url))
24         data = isbn_data(doc)
25         data['publishing_date'] = self.cleaned_data['publishing_date']
26         return data
27
28
29 class WLConfirmForm(WLISBNForm):
30     platform_url = forms.URLField(widget=forms.HiddenInput)
31     publishing_date = forms.DateField(widget=forms.HiddenInput)
32
33     @staticmethod
34     def contributors(data):
35         person_name = data['name']
36         corporate_name = data['corporate_name']
37         if person_name:
38             # assuming that unknown can't be a co-author
39             if person_name == UNKNOWN:
40                 return [{'role': 'A01', 'unnamed': '01'}]
41             else:
42                 return [{'role': 'A01', 'name': name} for name in person_name.split('; ')]
43         if corporate_name:
44             return [{'role': 'A01', 'corporate_name': name} for name in corporate_name.split('; ')]
45
46     def save(self):
47         data = self.prepare_data()
48         for file_format in data['formats']:
49             data['product_form'] = PRODUCT_FORMS[file_format]
50             data['product_form_detail'] = PRODUCT_FORM_DETAILS[file_format]
51             data['contributors'] = self.contributors(data)
52             ONIXRecord.new_record(purpose=ISBNPool.PURPOSE_WL, data=data)
53         return data
54
55
56 class FNPISBNForm(forms.Form):
57     FORMAT_CHOICES = (
58         ('HTML', 'HTML'),
59         ('PDF', 'PDF'),
60         ('EPUB', 'ePUB'),
61         ('MOBI', 'MOBI'),
62         ('TXT', 'TXT'),
63         ('SOFT', _('Soft cover book')),
64     )
65     LANGUAGE_CHOICES = (
66         ('pol', u'polski'),
67         ('eng', u'angielski'),
68         ('ger', u'niemiecki'),
69         ('fre', u'francuski'),
70     )
71
72     title = forms.CharField()
73     authors = forms.CharField(help_text=u'wartości oddzielone przecinkami lub „Wielu autorów”')
74     formats = forms.MultipleChoiceField(choices=FORMAT_CHOICES)
75     language = forms.ChoiceField(choices=LANGUAGE_CHOICES)
76     publishing_date = forms.DateField()
77
78     def prepare_author(self, name):
79         if name == u'Wielu autorów':
80             return {'role': 'A01', 'unnamed': '04'}
81         if ' ' in name:
82             first_name, last_name = [s.strip() for s in name.rsplit(' ', 1)]
83             output_name = '%s, %s' % (last_name, first_name)
84         else:
85             output_name = name.strip()
86         return {'role': 'A01', 'name': output_name}
87
88     def slug(self):
89         return slugify('fnp %s %s' % (self.cleaned_data['authors'], self.cleaned_data['title']))
90
91     def save(self):
92         data = {
93             'title': self.cleaned_data['title'],
94             'language': self.cleaned_data['language'],
95             'publishing_date': self.cleaned_data['publishing_date'],
96             'contributors': [self.prepare_author(a) for a in self.cleaned_data['authors'].split(',')],
97             'edition_type': 'NED',
98             'imprint': 'Fundacja Nowoczesna Polska',
99             'dc_slug': self.slug(),
100         }
101         formats = self.cleaned_data['formats']
102         for book_format in formats:
103             data['product_form'] = PRODUCT_FORMS[book_format]
104             if book_format in PRODUCT_FORM_DETAILS:
105                 data['product_form_detail'] = PRODUCT_FORM_DETAILS[book_format]
106             else:
107                 del data['product_form_detail']
108             ONIXRecord.new_record('FNP', data)