Picture form + test
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Tue, 20 Dec 2011 16:33:48 +0000 (17:33 +0100)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Tue, 20 Dec 2011 16:34:18 +0000 (17:34 +0100)
apps/api/tests.py
apps/picture/forms.py [new file with mode: 0644]
apps/picture/tests/__init__.py [new file with mode: 0644]
apps/picture/tests/files/kandinsky-composition-viii.png [new file with mode: 0644]
apps/picture/tests/files/kandinsky-composition-viii.xml [new file with mode: 0644]

index 2c2e51c..74417ac 100644 (file)
@@ -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 (file)
index 0000000..ad5096b
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
diff --git a/apps/picture/tests/files/kandinsky-composition-viii.png b/apps/picture/tests/files/kandinsky-composition-viii.png
new file mode 100644 (file)
index 0000000..a809eb5
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 (file)
index 0000000..036bdf7
--- /dev/null
@@ -0,0 +1,35 @@
+<picture>
+  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">
+    <rdf:Description rdf:about="http://wiki.wolnepodreczniki.pl/Lektury:Andersen/Brzydkie_kaczątko">
+      <dc:creator xml:lang="pl">Kandinsky, Vasily</dc:creator>
+      <dc:title xml:lang="la">composition 8</dc:title>
+      <dc:publisher xml:lang="pl">Fundacja Nowoczesna Polska</dc:publisher>
+      <dc:contributor.editor xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">Sekuła, Aleksandra</dc:contributor.editor>
+      <dc:contributor.editor xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">Kwiatkowska, Katarzyna</dc:contributor.editor>
+      <dc:contributor.technical_editor xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">Trzeciak, Weronika</dc:contributor.technical_editor>
+      <dc:subject.period xml:lang="pl">Modernizm</dc:subject.period>
+      <dc:subject.type xml:lang="pl">Obraz</dc:subject.type>
+      <dc:description xml:lang="pl">Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl). Reprodukcja cyfrowa wykonana przez Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN.</dc:description>
+      <dc:description.dimensions xml:lang="pl">55 1/8 x 79 1/8 inches</dc:description.dimensions>
+      <dc:description.medium xml:lang="pl">Olej na płótnie</dc:description.medium>
+      <dc:identifier.url xml:lang="pl">http://wolnelektury.pl/katalog/obraz/kandinsky-composition-viii</dc:identifier.url>
+      <dc:source.URL xml:lang="pl">http://www.ibiblio.org/wm/paint/auth/kandinsky/kandinsky.comp-8.jpg</dc:source.URL>
+      <dc:source xml:lang="pl">Muzeum Narodowe, inw. 00000001.</dc:source>
+      <dc:rights xml:lang="pl">Domena publiczna - Vasily Kandinsky zm. ?</dc:rights>
+      <dc:date.pd xml:lang="pl">1940</dc:date.pd>
+      <dc:type>Image</dc:type>
+      <dc:format xml:lang="pl">image/png</dc:format>
+      <dc:format.dimensions.digital xml:lang="pl">1090 x 755 px</dc:format.dimensions.digital>
+      <dc:date xml:lang="pl">1923</dc:date>
+      <dc:language xml:lang="pl" xmlns:dc="http://purl.org/dc/elements/1.1/">eng</dc:language>
+    </rdf:Description>
+  </rdf:RDF>
+
+  <sem type="obiekt" obiekt="kosmos">
+    <div type="area" x1="17" y1="31" x2="275" y2="309"/>
+  </sem>
+  <sem type="obiekt" obiekt="nieporządek">
+    <div type="area" x1="300" y1="138" x2="976" y2="514"/>
+  </sem>
+</picture>