X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/2bf5eaca680da6ebd22e6fd799a9a5d68206f0b8..49e2be0521e11928d5f00378907e478d1bd703a4:/apps/picture/forms.py diff --git a/apps/picture/forms.py b/apps/picture/forms.py new file mode 100644 index 000000000..ad5096bf5 --- /dev/null +++ b/apps/picture/forms.py @@ -0,0 +1,24 @@ +from django import forms +from django.utils.translation import ugettext_lazy as _ +from picture.models import Picture + + +class PictureImportForm(forms.Form): + picture_xml_file = forms.FileField(required=False) + picture_xml = forms.CharField(required=False) + picture_image_file = forms.FileField(required=True) + + def clean(self): + from django.core.files.base import ContentFile + + if not self.cleaned_data['picture_xml_file']: + if self.cleaned_data['picture_xml']: + self.cleaned_data['picture_xml_file'] = \ + ContentFile(self.cleaned_data['picture_xml'].encode('utf-8')) + else: + raise forms.ValidationError(_("Please supply an XML.")) + return super(PictureImportForm, self).clean() + + def save(self, commit=True, **kwargs): + return Picture.from_xml_file(self.cleaned_data['picture_xml_file'], image_file=self.cleaned_data['picture_image_file'], + overwrite=True, **kwargs)