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 class PersonListField(forms.Field):
18 def __init__(self, *args, **kwargs):
19 super(PersonListField, self).__init__(*args, **kwargs)
21 def _get_initial(self):
22 return self._initial and (u'\n'.join( ( unicode(person) for person in self._initial)))
24 def _set_initial(self, value):
27 elif isinstance(value, list):
28 self._initial = [ e if isinstance(e, dcparser.Person) \
29 else dcparser.Person.from_text(e) for e in value ]
30 elif isinstance(value, basestring):
31 self._initial = [dcparser.Person.from_text(token) for token in value.split('\n') ]
33 raise ValueError("Invalid value. Must be a list of dcparser.Person or string")
35 initial = property(_get_initial, _set_initial)
37 def clean(self, value):
38 super(PersonListField, self).clean(value)
39 people = value.split('\n')
41 return [dcparser.Person.from_text(person) for person in people]
43 raise django.utils.ValidationError(e.message)
45 class BookForm(forms.Form):
46 content = forms.CharField(widget=forms.Textarea)
47 commit_message = forms.CharField(required=False)
49 class BookUploadForm(forms.Form):
50 file = forms.FileField()
52 class ImageFoldersForm(forms.Form):
53 folders = forms.ChoiceField(required=False)
55 def __init__(self, *args, **kwargs):
56 super(ImageFoldersForm, self).__init__(*args, **kwargs)
57 self.fields['folders'].choices = [('', '-- Wybierz folder z obrazkami --')] + [(fn, fn) for fn in models.get_image_folders()]
60 class DublinCoreForm(forms.Form):
61 about = forms.URLField(verify_exists=False)
62 author = PersonField()
63 title = forms.CharField()
64 epoch = forms.CharField()
65 kind = forms.CharField()
66 genre = forms.CharField()
67 created_at = forms.DateField()
68 released_to_public_domain_at = forms.DateField()
69 editors = PersonListField(widget=forms.Textarea, required=False)
70 translators = PersonListField(widget=forms.Textarea, required=False)
71 technical_editors = PersonListField(widget=forms.Textarea, required=False)
72 publisher = forms.CharField()
73 source_name = forms.CharField(widget=forms.Textarea)
74 source_url = forms.URLField(verify_exists=False)
75 url = forms.URLField(verify_exists=False)
76 parts = forms.CharField(widget=forms.Textarea, required=False)
77 license = forms.CharField(required=False)
78 license_description = forms.CharField(widget=forms.Textarea, required=False)
80 commit_message = forms.CharField(required=False, widget=forms.HiddenInput)
82 def __init__(self, *args, **kwargs):
84 info = kwargs.pop('info', None)
86 super(DublinCoreForm, self).__init__(*args, **kwargs)
88 if isinstance(info, dcparser.BookInfo):
89 for name, value in info.to_dict().items():
90 self.fields[name].initial = value