Zlacz takie same przyciski podczas naprawiania.
[redakcja.git] / apps / explorer / forms.py
1 from django import forms
2
3 from lxml import etree
4 from librarian import dcparser
5 import django.utils
6
7 from explorer import models
8
9 class PersonField(forms.CharField):
10     def clean(self, value):
11         try:
12             return dcparser.Person.from_text( super(PersonField, self).clean(value) )
13         except ValueError, e:
14             raise django.utils.ValidationError(e.message)
15
16 def person_conv(value):
17     if isinstance(value, dcparser.Person):
18         return value
19     elif isinstance(value, basestring):
20         return dcparser.Person.from_text( unicode(value) )
21     else:
22         raise ValueError("Can't convert '%s' to Person object." % value)
23
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)
30     
31     def _get_initial(self):
32         return self._initial and (u'\n'.join( ( unicode(person) for person in self._initial)))
33
34     def _set_initial(self, value):
35         if value is None:
36             self._initial = None
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') ]
41         else:
42             raise ValueError("Invalid value. Must be a list of dcparser.Person or string")    
43
44     initial = property(_get_initial, _set_initial)
45
46     def clean(self, value):
47         super(ListField, self).clean(value)
48         elems = value.split('\n')
49         try:
50             return [self.convert(el) for el in elems]
51         except ValueError, err:
52             raise django.utils.ValidationError(err.message)
53
54 class BookForm(forms.Form):
55     content = forms.CharField(widget=forms.Textarea)
56     commit_message = forms.CharField(required=False)
57
58 class MergeForm(forms.Form):
59     message = forms.CharField(error_messages={'required': 'Please write a merge description.'})
60
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')
65
66 class ImageFoldersForm(forms.Form):
67     folders = forms.ChoiceField(required=False)
68     
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()]
72
73 class DublinCoreForm(forms.Form):
74     about = forms.URLField(verify_exists=False)
75     author = PersonField()
76     title = forms.CharField()
77     epochs = ListField()
78     kinds = ListField()
79     genres = ListField()
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)
92     
93     commit_message = forms.CharField(required=False, widget=forms.HiddenInput)
94     
95     def __init__(self, *args, **kwargs):
96         info = kwargs.pop('info', None)        
97         super(DublinCoreForm, self).__init__(*args, **kwargs)
98         
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]