Basic crowdfunding.
[wolnelektury.git] / apps / picture / forms.py
index ad5096b..9d0c5a5 100644 (file)
@@ -6,9 +6,11 @@ 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)
+    picture_image_file = forms.FileField(required=False)
+    picture_image_data = forms.CharField(required=False)
 
     def clean(self):
+        from base64 import b64decode
         from django.core.files.base import ContentFile
 
         if not self.cleaned_data['picture_xml_file']:
@@ -17,6 +19,15 @@ class PictureImportForm(forms.Form):
                         ContentFile(self.cleaned_data['picture_xml'].encode('utf-8'))
             else:
                 raise forms.ValidationError(_("Please supply an XML."))
+
+        if not self.cleaned_data['picture_image_file']:
+            if self.cleaned_data['picture_image_data']:
+                self.cleaned_data['picture_image_file'] = \
+                        ContentFile(b64decode(
+                                self.cleaned_data['picture_image_data']))
+            else:
+                raise forms.ValidationError(_("Please supply an image."))
+
         return super(PictureImportForm, self).clean()
 
     def save(self, commit=True, **kwargs):