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 BookUploadForm(forms.Form):
59 file = forms.FileField()
61 class ImageFoldersForm(forms.Form):
62 folders = forms.ChoiceField(required=False)
64 def __init__(self, *args, **kwargs):
65 super(ImageFoldersForm, self).__init__(*args, **kwargs)
66 self.fields['folders'].choices = [('', '-- Wybierz folder z obrazkami --')] + [(fn, fn) for fn in models.get_image_folders()]
68 class DublinCoreForm(forms.Form):
69 about = forms.URLField(verify_exists=False)
70 author = PersonField()
71 title = forms.CharField()
75 created_at = forms.DateField()
76 released_to_public_domain_at = forms.DateField()
77 editors = ListField(widget=forms.Textarea, required=False, converter=person_conv)
78 translators = ListField(widget=forms.Textarea, required=False, converter=person_conv)
79 technical_editors = ListField(widget=forms.Textarea, required=False, converter=person_conv)
80 publisher = forms.CharField()
81 source_name = forms.CharField(widget=forms.Textarea)
82 source_url = forms.URLField(verify_exists=False)
83 url = forms.URLField(verify_exists=False)
84 parts = forms.CharField(widget=forms.Textarea, required=False)
85 license = forms.CharField(required=False)
86 license_description = forms.CharField(widget=forms.Textarea, required=False)
88 commit_message = forms.CharField(required=False, widget=forms.HiddenInput)
90 def __init__(self, *args, **kwargs):
91 info = kwargs.pop('info', None)
92 super(DublinCoreForm, self).__init__(*args, **kwargs)
94 if isinstance(info, dcparser.BookInfo):
95 vdict = info.to_dict()
96 for name in self.fields.keys():
97 if vdict.has_key(name):
98 self.fields[name].initial = vdict[name]