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