1 # -*- coding: utf-8 -*-
2 from urllib2 import urlopen
4 from django import forms
5 from django.utils.translation import ugettext_lazy as _
6 from fnpdjango.utils.text.slughifi import slughifi
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
14 class WLISBNForm(forms.Form):
15 platform_url = forms.URLField(label=u'Adres na platformie')
16 publishing_date = forms.DateField(label=u'Data publikacji')
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))
24 data['publishing_date'] = self.cleaned_data['publishing_date']
28 class WLConfirmForm(WLISBNForm):
29 platform_url = forms.URLField(widget=forms.HiddenInput)
30 publishing_date = forms.DateField(widget=forms.HiddenInput)
33 def contributors(data):
34 person_name = data['name']
35 corporate_name = data['corporate_name']
37 # assuming that unknown can't be a co-author
38 if person_name == UNKNOWN:
39 return [{'role': 'A01', 'unnamed': '01'}]
41 return [{'role': 'A01', 'name': name} for name in person_name.split('; ')]
43 return [{'role': 'A01', 'corporate_name': name} for name in corporate_name.split('; ')]
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)
55 class FNPISBNForm(forms.Form):
62 ('SOFT', _('Soft cover book')),
66 ('eng', u'angielski'),
67 ('ger', u'niemiecki'),
68 ('fre', u'francuski'),
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()
77 def prepare_author(self, name):
78 if name == u'Wielu autorów':
79 return {'role': 'A01', 'unnamed': '04'}
81 first_name, last_name = [s.strip() for s in name.rsplit(' ', 1)]
82 output_name = '%s, %s' % (last_name, first_name)
84 output_name = name.strip()
85 return {'role': 'A01', 'name': output_name}
88 return slughifi('fnp %s %s' % (self.cleaned_data['authors'], self.cleaned_data['title']))
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(),
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]
106 del data['product_form_detail']
107 ONIXRecord.new_record('FNP', data)