1 from django import forms
2 from django.utils.translation import ugettext_lazy as _
3 from picture.models import Picture
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)
13 from base64 import b64decode
14 from django.core.files.base import ContentFile
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'))
21 raise forms.ValidationError(_("Please supply an XML."))
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']))
29 raise forms.ValidationError(_("Please supply an image."))
31 return super(PictureImportForm, self).clean()
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)