ad5096bf57592ce4dc56df14f310e487524465d4
[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=True)
10
11     def clean(self):
12         from django.core.files.base import ContentFile
13
14         if not self.cleaned_data['picture_xml_file']:
15             if self.cleaned_data['picture_xml']:
16                 self.cleaned_data['picture_xml_file'] = \
17                         ContentFile(self.cleaned_data['picture_xml'].encode('utf-8'))
18             else:
19                 raise forms.ValidationError(_("Please supply an XML."))
20         return super(PictureImportForm, self).clean()
21
22     def save(self, commit=True, **kwargs):
23         return Picture.from_xml_file(self.cleaned_data['picture_xml_file'], image_file=self.cleaned_data['picture_image_file'],
24                                      overwrite=True, **kwargs)