Dodanie MySQL-python do requirements.txt.
[redakcja.git] / apps / explorer / forms.py
index e648aa5..6c5c88d 100644 (file)
@@ -1,15 +1,69 @@
+# -*- coding: utf-8 -*-
 from django import forms
 
 from lxml import etree
 from librarian import dcparser
+import django.utils
 
 from explorer import models
 
+class PersonField(forms.CharField):
+    def clean(self, value):
+        try:
+            return dcparser.Person.from_text( super(PersonField, self).clean(value) )
+        except ValueError, e:
+            raise django.utils.ValidationError(e.message)
+
+def person_conv(value):
+    if isinstance(value, dcparser.Person):
+        return value
+    elif isinstance(value, basestring):
+        return dcparser.Person.from_text( unicode(value) )
+    else:
+        raise ValueError("Can't convert '%s' to Person object." % value)
+
+class ListField(forms.Field):
+    def __init__(self, *args, **kwargs):
+        self.convert = kwargs.pop('converter', unicode)
+        if not kwargs.has_key('widget'):
+            kwargs['widget'] = forms.Textarea
+        super(ListField, self).__init__(*args, **kwargs)
+    
+    def _get_initial(self):
+        return self._initial and (u'\n'.join( ( unicode(person) for person in self._initial)))
+
+    def _set_initial(self, value):
+        if value is None:
+            self._initial = None
+        elif isinstance(value, list):
+            self._initial = [ self.convert(e) for e in value ]
+        elif isinstance(value, basestring):
+            self._initial = [ self.convert(e) for e in value.split('\n') ]
+        else:
+            raise ValueError("Invalid value. Must be a list of dcparser.Person or string")    
+
+    initial = property(_get_initial, _set_initial)
+
+    def clean(self, value):
+        super(ListField, self).clean(value)
+        elems = value.split('\n')
+        try:
+            return [self.convert(el) for el in elems]
+        except ValueError, err:
+            raise django.utils.ValidationError(err.message)
 
 class BookForm(forms.Form):
-    text = forms.CharField(widget=forms.Textarea)
-    commit_message = forms.CharField()
-    user = forms.CharField()
+    content = forms.CharField(widget=forms.Textarea)
+    commit_message = forms.CharField(required=False)
+
+class MergeForm(forms.Form):
+    message = forms.CharField(error_messages={'required': 'Please write a merge description.'})
+
+class BookUploadForm(forms.Form):
+    file = forms.FileField(label='Source OCR file')
+    bookname = forms.RegexField(regex=r'[0-9\.\w_-]+',  \
+        label='Publication name', help_text='Example: slowacki-beniowski')
+    autoxml = forms.BooleanField(required=False, initial=True, label=u"Generate DublinCore template")
 
 class ImageFoldersForm(forms.Form):
     folders = forms.ChoiceField(required=False)
@@ -18,50 +72,41 @@ class ImageFoldersForm(forms.Form):
         super(ImageFoldersForm, self).__init__(*args, **kwargs)
         self.fields['folders'].choices = [('', '-- Wybierz folder z obrazkami --')] + [(fn, fn) for fn in models.get_image_folders()]
 
+class SplitForm(forms.Form):
+    partname = forms.RegexField(regex='[0-9\.\w_-]+',  \
+        label='Part name', help_text='Example: rozdział-2')
+    autoxml = forms.BooleanField(required=False, initial=False, label=u"Split as new publication")
+    fulltext = forms.CharField(widget=forms.HiddenInput(), required=False)
+    splittext = forms.CharField(widget=forms.HiddenInput(), required=False)
 
 class DublinCoreForm(forms.Form):
-    wiki_url = forms.URLField(verify_exists=False)
-    author = forms.CharField()
+    about = forms.URLField(verify_exists=False)
+    author = PersonField()
     title = forms.CharField()
-    epoch = forms.CharField()
-    kind = forms.CharField()
-    genre = forms.CharField()
+    epochs = ListField()
+    kinds = ListField()
+    genres = ListField()
     created_at = forms.DateField()
-    released_to_public_domain_at = forms.DateField()
-    translator = forms.CharField(required=False)
-    technical_editor = forms.CharField(required=False)
+    released_to_public_domain_at = forms.DateField(required=False)
+    editors = ListField(widget=forms.Textarea, required=False, converter=person_conv)
+    translators = ListField(widget=forms.Textarea, required=False, converter=person_conv)
+    technical_editors = ListField(widget=forms.Textarea, required=False, converter=person_conv)
     publisher = forms.CharField()
-    source_name = forms.CharField(widget=forms.Textarea)
-    source_url = forms.URLField(verify_exists=False)
+    source_name = forms.CharField(widget=forms.Textarea, required=False)
+    source_url = forms.URLField(verify_exists=False, required=False)
     url = forms.URLField(verify_exists=False)
-    parts = forms.CharField(widget=forms.Textarea, required=False)
+    parts = ListField(required=False)
     license = forms.CharField(required=False)
     license_description = forms.CharField(widget=forms.Textarea, required=False)
     
+    commit_message = forms.CharField(required=False, widget=forms.HiddenInput)
+    
     def __init__(self, *args, **kwargs):
-        text = None
-        if 'text' in kwargs:
-            text = kwargs.pop('text')
-        
+        info = kwargs.pop('info', None)        
         super(DublinCoreForm, self).__init__(*args, **kwargs)
         
-        if text is not None:
-            book_info = dcparser.BookInfo.from_string(text)
-            for name, value in book_info.to_dict().items():
-                self.fields[name].initial = value
-    
-    def save(self, repository, path):
-        file_contents = repository.get_file(path).data()
-        doc = etree.fromstring(file_contents)
-                
-        book_info = dcparser.BookInfo()
-        for name, value in self.cleaned_data.items():
-            if value is not None and value != '':
-                setattr(book_info, name, value)
-        rdf = etree.XML(book_info.to_xml())
-        
-        old_rdf = doc.getroottree().find('//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF')
-        old_rdf.getparent().remove(old_rdf)
-        doc.insert(0, rdf)
-        repository.add_file(path, unicode(etree.tostring(doc), 'utf-8'))
-
+        if isinstance(info, dcparser.BookInfo):
+            vdict = info.to_dict()
+            for name in self.fields.keys():
+                if vdict.has_key(name):
+                    self.fields[name].initial = vdict[name]
\ No newline at end of file