d79bc74cf97e6cfa82abe7103604fd98aae39b5a
[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 class PersonListField(forms.Field):
17
18     def __init__(self, *args, **kwargs):
19         super(PersonListField, self).__init__(*args, **kwargs)
20     
21     def _get_initial(self):
22         return u'\n'.join( ( unicode(person) for person in self._initial) )
23
24     def _set_initial(self, value):
25         if value is None:
26             self._initial = None
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') ]
32         else:
33             raise ValueError("Invalid value. Must be a list of dcparser.Person or string")    
34
35     initial = property(_get_initial, _set_initial)
36
37     def clean(self, value):
38         super(PersonListField, self).clean(value)
39         people = value.split('\n')
40         try:
41             return [dcparser.Person.from_text(person) for person in people]
42         except ValueError, e:
43             raise django.utils.ValidationError(e.message)        
44
45 class BookForm(forms.Form):
46     content = forms.CharField(widget=forms.Textarea)
47     commit_message = forms.CharField(required=False)
48
49 class BookUploadForm(forms.Form):
50     file = forms.FileField()
51
52 class ImageFoldersForm(forms.Form):
53     folders = forms.ChoiceField(required=False)
54     
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()]
58
59
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     translator = PersonField(required=False)
71     technical_editor = PersonField(required=False)
72     publisher = PersonField()
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)
79     
80     commit_message = forms.CharField(required=False, widget=forms.HiddenInput)
81     
82     def __init__(self, *args, **kwargs):
83         text = None
84         info = kwargs.pop('info', None)
85         
86         super(DublinCoreForm, self).__init__(*args, **kwargs)
87         
88         if isinstance(info, dcparser.BookInfo):
89             for name, value in info.to_dict().items():
90                 self.fields[name].initial = value