From: Marcin Koziej Date: Wed, 21 Dec 2011 12:03:51 +0000 (+0100) Subject: Generate tags for picture themes X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/cfe30a8b506379030bd36bfabd5a17efe83c3100?ds=inline Generate tags for picture themes --- diff --git a/apps/picture/models.py b/apps/picture/models.py index 6ac54fe82..2d2440884 100644 --- a/apps/picture/models.py +++ b/apps/picture/models.py @@ -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): diff --git a/apps/picture/tests/__init__.py b/apps/picture/tests/__init__.py index e69de29bb..8817a1c18 100644 --- a/apps/picture/tests/__init__.py +++ b/apps/picture/tests/__init__.py @@ -0,0 +1 @@ +from picture.tests.picture_import import * diff --git a/apps/picture/tests/files/kandinsky-composition-viii.xml b/apps/picture/tests/files/kandinsky-composition-viii.xml index 036bdf7e1..014410ec5 100644 --- a/apps/picture/tests/files/kandinsky-composition-viii.xml +++ b/apps/picture/tests/files/kandinsky-composition-viii.xml @@ -25,10 +25,10 @@ - +
- +
diff --git a/apps/picture/tests/picture_import.py b/apps/picture/tests/picture_import.py new file mode 100644 index 000000000..9e965f91d --- /dev/null +++ b/apps/picture/tests/picture_import.py @@ -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 + diff --git a/lib/librarian b/lib/librarian index 1f3e067b0..97ed31c1e 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit 1f3e067b075c8c20609c4ebbada2c543f2717082 +Subproject commit 97ed31c1ea6c21e0ac6cad0bc25a3cf63ecdd1ad