Merge branch 'oaipmh'
[wolnelektury.git] / apps / picture / forms.py
1 from django import forms
2 from django.utils.translation import ugettext_lazy as _
3 from picture.models import Picture
4
5
6 class PictureImportForm(forms.Form):
7     picture_xml_file = forms.FileField(required=False)
8     picture_xml = forms.CharField(required=False)
9     picture_image_file = forms.FileField(required=False)
10     picture_image_data = forms.CharField(required=False)
11
12     def clean(self):
13         from base64 import b64decode
14         from django.core.files.base import ContentFile
15
16         if not self.cleaned_data['picture_xml_file']:
17             if self.cleaned_data['picture_xml']:
18                 self.cleaned_data['picture_xml_file'] = \
19                         ContentFile(self.cleaned_data['picture_xml'].encode('utf-8'))
20             else:
21                 raise forms.ValidationError(_("Please supply an XML."))
22
23         if not self.cleaned_data['picture_image_file']:
24             if self.cleaned_data['picture_image_data']:
25                 self.cleaned_data['picture_image_file'] = \
26                         ContentFile(b64decode(
27                                 self.cleaned_data['picture_image_data']))
28             else:
29                 raise forms.ValidationError(_("Please supply an image."))
30
31         return super(PictureImportForm, self).clean()
32
33     def save(self, commit=True, **kwargs):
34         return Picture.from_xml_file(self.cleaned_data['picture_xml_file'], image_file=self.cleaned_data['picture_image_file'],
35                                      overwrite=True, **kwargs)