Fixed mercurial paths encoding.
[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 BookUploadForm(forms.Form):
59     file = forms.FileField()
60
61 class ImageFoldersForm(forms.Form):
62     folders = forms.ChoiceField(required=False)
63     
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()]
67
68 class DublinCoreForm(forms.Form):
69     about = forms.URLField(verify_exists=False)
70     author = PersonField()
71     title = forms.CharField()
72     epochs = ListField()
73     kinds = ListField()
74     genres = ListField()
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)
87     
88     commit_message = forms.CharField(required=False, widget=forms.HiddenInput)
89     
90     def __init__(self, *args, **kwargs):
91         info = kwargs.pop('info', None)        
92         super(DublinCoreForm, self).__init__(*args, **kwargs)
93         
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]