From: Marcin Koziej Date: Tue, 20 Dec 2011 16:33:48 +0000 (+0100) Subject: Picture form + test X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/49e2be0521e11928d5f00378907e478d1bd703a4?hp=2bf5eaca680da6ebd22e6fd799a9a5d68206f0b8 Picture form + test --- diff --git a/apps/api/tests.py b/apps/api/tests.py index 2c2e51ce8..74417acb9 100644 --- a/apps/api/tests.py +++ b/apps/api/tests.py @@ -8,6 +8,13 @@ from django.conf import settings from api.helpers import timestamp from catalogue.models import Book, Tag +from picture.tests.utils import RequestFactory +from picture.forms import PictureImportForm +from picture.models import Picture +import picture.tests +from django.core.files.uploadedfile import SimpleUploadedFile + +from os import path class ApiTest(TestCase): @@ -135,3 +142,21 @@ class TagTests(TestCase): tag = json.loads(self.client.get('/api/authors/joe/').content) self.assertEqual(tag['name'], self.tag.name, 'Wrong tag details.') + + +class PictureTests(ApiTest): + def test_publish(self): + slug = "kandinsky-composition-viii" + xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read()) + img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read()) + + import_form = PictureImportForm({}, { + 'picture_xml_file': xml, + 'picture_image_file': img + }) + + assert import_form.is_valid() + if import_form.is_valid(): + import_form.save() + + pic = Picture.objects.get(slug=slug) 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) diff --git a/apps/picture/tests/__init__.py b/apps/picture/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/apps/picture/tests/files/kandinsky-composition-viii.png b/apps/picture/tests/files/kandinsky-composition-viii.png new file mode 100644 index 000000000..a809eb5c4 Binary files /dev/null and b/apps/picture/tests/files/kandinsky-composition-viii.png differ diff --git a/apps/picture/tests/files/kandinsky-composition-viii.xml b/apps/picture/tests/files/kandinsky-composition-viii.xml new file mode 100644 index 000000000..036bdf7e1 --- /dev/null +++ b/apps/picture/tests/files/kandinsky-composition-viii.xml @@ -0,0 +1,35 @@ + + + + Kandinsky, Vasily + composition 8 + Fundacja Nowoczesna Polska + Sekuła, Aleksandra + Kwiatkowska, Katarzyna + Trzeciak, Weronika + Modernizm + Obraz + Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl). Reprodukcja cyfrowa wykonana przez Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN. + 55 1/8 x 79 1/8 inches + Olej na płótnie + http://wolnelektury.pl/katalog/obraz/kandinsky-composition-viii + http://www.ibiblio.org/wm/paint/auth/kandinsky/kandinsky.comp-8.jpg + Muzeum Narodowe, inw. 00000001. + Domena publiczna - Vasily Kandinsky zm. ? + 1940 + Image + image/png + 1090 x 755 px + 1923 + eng + + + + +
+ + +
+ + +