Generate tags for picture themes
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Wed, 21 Dec 2011 12:03:51 +0000 (13:03 +0100)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Wed, 21 Dec 2011 12:03:51 +0000 (13:03 +0100)
apps/picture/models.py
apps/picture/tests/__init__.py
apps/picture/tests/files/kandinsky-composition-viii.xml
apps/picture/tests/picture_import.py [new file with mode: 0644]
lib/librarian

index 6ac54fe..2d24408 100644 (file)
@@ -6,6 +6,7 @@ from django.conf import settings
 from django.core.files.storage import FileSystemStorage
 from django.utils.datastructures import SortedDict
 from librarian import dcparser, picture
+from slughifi import slughifi
 
 from django.utils.translation import ugettext_lazy as _
 from newtagging import managers
@@ -67,6 +68,7 @@ class Picture(models.Model):
         """
         Import xml and it's accompanying image file.
         """
+        from sortify import sortify
         from django.core.files import File
         from librarian.picture import WLPicture
         close_xml_file = False
@@ -78,27 +80,39 @@ class Picture(models.Model):
             # use librarian to parse meta-data
             picture_xml = WLPicture.from_file(xml_file)
 
-            picture, created = Picture.objects.get_or_create(slug=picture_xml.slug)
+            pict, created = Picture.objects.get_or_create(slug=picture_xml.slug)
             if not created and not overwrite:
                 raise Picture.AlreadyExists('Picture %s already exists' % picture_xml.slug)
 
-            picture.title = picture_xml.picture_info.title
+            pict.title = picture_xml.picture_info.title
+
+            #            from nose.tools import set_trace; set_trace()
+            motif_tags = set()
+            for part in picture_xml.partiter():
+                for motif in part['themes']:
+                    tag, created = catalogue.models.Tag.objects.get_or_create(slug=slughifi(motif), category='theme')
+                    if created:
+                        tag.name = motif
+                        tag.sort_key = sortify(tag.name)
+                        tag.save()
+                    motif_tags.add(tag)
 
-            picture.tags = catalogue.models.Tag.tags_from_info(picture_xml.picture_info)
+            pict.tags = catalogue.models.Tag.tags_from_info(picture_xml.picture_info) + \
+                list(motif_tags)
 
             if image_file is not None:
                 img = image_file
             else:
                 img = picture_xml.image_file()
 
-            picture.image_file.save(path.basename(picture_xml.image_path), File(img))
+            pict.image_file.save(path.basename(picture_xml.image_path), File(img))
 
-            picture.xml_file.save("%s.xml" % picture.slug, File(xml_file))
-            picture.save()
+            pict.xml_file.save("%s.xml" % pict.slug, File(xml_file))
+            pict.save()
         finally:
             if close_xml_file:
                 xml_file.close()
-        return picture
+        return pict
 
     @classmethod
     def picture_list(cls, filter=None):
index e69de29..8817a1c 100644 (file)
@@ -0,0 +1 @@
+from picture.tests.picture_import import *
index 036bdf7..014410e 100644 (file)
     </rdf:Description>
   </rdf:RDF>
 
-  <sem type="obiekt" obiekt="kosmos">
+  <sem type="object" object="kosmos">
     <div type="area" x1="17" y1="31" x2="275" y2="309"/>
   </sem>
-  <sem type="obiekt" obiekt="nieporządek">
+  <sem type="theme" theme="nieporządek">
     <div type="area" x1="300" y1="138" x2="976" y2="514"/>
   </sem>
  
diff --git a/apps/picture/tests/picture_import.py b/apps/picture/tests/picture_import.py
new file mode 100644 (file)
index 0000000..9e965f9
--- /dev/null
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+from __future__ import with_statement
+
+from django.core.files.base import ContentFile, File
+from catalogue.test_utils import *
+from catalogue import models
+from librarian import WLURI
+from picture.models import Picture
+
+from nose.tools import raises
+import tempfile
+from os import unlink, path, makedirs
+
+
+class PictureTest(TestCase):
+    
+    def test_import(self):
+        picture = Picture.from_xml_file(path.join(path.dirname(__file__), "files/kandinsky-composition-viii.xml"))
+
+        motifs = set([tag.name for tag in picture.tags if tag.category == 'theme'])
+        assert motifs == set([u'nieporządek']), 'theme tags are wrong. %s' % motifs
+    
index 1f3e067..97ed31c 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 1f3e067b075c8c20609c4ebbada2c543f2717082
+Subproject commit 97ed31c1ea6c21e0ac6cad0bc25a3cf63ecdd1ad