1 # -*- coding: utf-8 -*-
2 from datetime import date
3 from urllib2 import urlopen
5 from django import forms
6 from django.utils.translation import ugettext_lazy as _
7 from slugify import slugify
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
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)
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))
25 data['publishing_date'] = self.cleaned_data['publishing_date']
29 class WLConfirmForm(WLISBNForm):
30 platform_url = forms.URLField(widget=forms.HiddenInput)
31 publishing_date = forms.DateField(widget=forms.HiddenInput)
34 def contributors(data):
35 person_name = data['name']
36 corporate_name = data['corporate_name']
38 # assuming that unknown can't be a co-author
39 if person_name == UNKNOWN:
40 return [{'role': 'A01', 'unnamed': '01'}]
42 return [{'role': 'A01', 'name': name} for name in person_name.split('; ')]
44 return [{'role': 'A01', 'corporate_name': name} for name in corporate_name.split('; ')]
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)
56 class FNPISBNForm(forms.Form):
63 ('SOFT', _('Soft cover book')),
67 ('eng', u'angielski'),
68 ('ger', u'niemiecki'),
69 ('fre', u'francuski'),
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()
78 def prepare_author(self, name):
79 if name == u'Wielu autorów':
80 return {'role': 'A01', 'unnamed': '04'}
82 first_name, last_name = [s.strip() for s in name.rsplit(' ', 1)]
83 output_name = '%s, %s' % (last_name, first_name)
85 output_name = name.strip()
86 return {'role': 'A01', 'name': output_name}
89 return slugify('fnp %s %s' % (self.cleaned_data['authors'], self.cleaned_data['title']))
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(),
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]
107 del data['product_form_detail']
108 ONIXRecord.new_record('FNP', data)