1 from django import forms
4 from librarian import dcparser
7 from explorer import models
9 class PersonField(forms.CharField):
10 def clean(self, value):
12 return dcparser.Person.from_text( super(PersonField, self).clean(value) )
14 raise django.utils.ValidationError(e.message)
16 def person_conv(value):
17 if isinstance(value, dcparser.Person):
19 elif isinstance(value, basestring):
20 return dcparser.Person.from_text( unicode(value) )
22 raise ValueError("Can't convert '%s' to Person object." % value)
24 class ListField(forms.Field):
25 def __init__(self, *args, **kwargs):
26 self.convert = kwargs.pop('converter', unicode)
27 if not kwargs.has_key('widget'):
28 kwargs['widget'] = forms.Textarea
29 super(ListField, self).__init__(*args, **kwargs)
31 def _get_initial(self):
32 return self._initial and (u'\n'.join( ( unicode(person) for person in self._initial)))
34 def _set_initial(self, value):
37 elif isinstance(value, list):
38 self._initial = [ self.convert(e) for e in value ]
39 elif isinstance(value, basestring):
40 self._initial = [ self.convert(e) for e in value.split('\n') ]
42 raise ValueError("Invalid value. Must be a list of dcparser.Person or string")
44 initial = property(_get_initial, _set_initial)
46 def clean(self, value):
47 super(ListField, self).clean(value)
48 elems = value.split('\n')
50 return [self.convert(el) for el in elems]
51 except ValueError, err:
52 raise django.utils.ValidationError(err.message)
54 class BookForm(forms.Form):
55 content = forms.CharField(widget=forms.Textarea)
56 commit_message = forms.CharField(required=False)
58 class MergeForm(forms.Form):
59 message = forms.CharField(error_messages={'required': 'Please write a merge description.'})
61 class BookUploadForm(forms.Form):
62 file = forms.FileField(label='Source OCR file')
63 bookname = forms.RegexField(regex='[\w-]+', \
64 label='Publication name', help_text='Example: slowacki-beniowski')
66 class ImageFoldersForm(forms.Form):
67 folders = forms.ChoiceField(required=False)
69 def __init__(self, *args, **kwargs):
70 super(ImageFoldersForm, self).__init__(*args, **kwargs)
71 self.fields['folders'].choices = [('', '-- Wybierz folder z obrazkami --')] + [(fn, fn) for fn in models.get_image_folders()]
73 class DublinCoreForm(forms.Form):
74 about = forms.URLField(verify_exists=False)
75 author = PersonField()
76 title = forms.CharField()
80 created_at = forms.DateField()
81 released_to_public_domain_at = forms.DateField()
82 editors = ListField(widget=forms.Textarea, required=False, converter=person_conv)
83 translators = ListField(widget=forms.Textarea, required=False, converter=person_conv)
84 technical_editors = ListField(widget=forms.Textarea, required=False, converter=person_conv)
85 publisher = forms.CharField()
86 source_name = forms.CharField(widget=forms.Textarea)
87 source_url = forms.URLField(verify_exists=False)
88 url = forms.URLField(verify_exists=False)
89 parts = forms.CharField(widget=forms.Textarea, required=False)
90 license = forms.CharField(required=False)
91 license_description = forms.CharField(widget=forms.Textarea, required=False)
93 commit_message = forms.CharField(required=False, widget=forms.HiddenInput)
95 def __init__(self, *args, **kwargs):
96 info = kwargs.pop('info', None)
97 super(DublinCoreForm, self).__init__(*args, **kwargs)
99 if isinstance(info, dcparser.BookInfo):
100 vdict = info.to_dict()
101 for name in self.fields.keys():
102 if vdict.has_key(name):
103 self.fields[name].initial = vdict[name]