af691f8aed7591dc4dc8702a82fb1ce1172c41c9
[wolnelektury.git] / apps / picture / models.py
1 from django.db import models
2 import catalogue.models
3 from catalogue.fields import OverwritingFileField
4 from django.conf import settings
5 from django.core.files.storage import FileSystemStorage
6 from django.utils.translation import ugettext_lazy as _
7 from newtagging import managers
8 from os import path
9
10
11 picture_storage = FileSystemStorage(location=path.join(settings.MEDIA_ROOT, 'pictures'), base_url=settings.MEDIA_URL + "pictures/")
12
13
14 class Picture(models.Model):
15     """
16     Picture resource.
17
18     """
19     title       = models.CharField(_('title'), max_length=120)
20     slug        = models.SlugField(_('slug'), max_length=120, db_index=True, unique=True)
21     sort_key    = models.CharField(_('sort key'), max_length=120, db_index=True, editable=False)
22     created_at  = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True)
23     changed_at  = models.DateTimeField(_('creation date'), auto_now=True, db_index=True)
24     xml_file    = models.FileField('xml_file', upload_to="xml", storage=picture_storage)
25     image_file  = models.FileField(_('image_file'), upload_to="images", storage=picture_storage)
26     objects     = models.Manager()
27     tagged      = managers.ModelTaggedItemManager(catalogue.models.Tag)
28     tags        = managers.TagDescriptor(catalogue.models.Tag)
29
30     class AlreadyExists(Exception):
31         pass
32
33     class Meta:
34         ordering = ('sort_key',)
35
36         verbose_name = _('picture')
37         verbose_name_plural = _('pictures')
38
39     def save(self, force_insert=False, force_update=False, reset_short_html=True, **kwargs):
40         from sortify import sortify
41
42         self.sort_key = sortify(self.title)
43
44         ret = super(Picture, self).save(force_insert, force_update)
45
46         return ret
47
48     def __unicode__(self):
49         return self.title
50
51     @classmethod
52     def from_xml_file(cls, xml_file, images_path=None, overwrite=False):
53         """
54         Import xml and it's accompanying image file.
55         """
56         from django.core.files import File
57         from librarian.picture import WLPicture
58         close_xml_file = False
59
60         if not isinstance(xml_file, File):
61             xml_file = File(open(xml_file))
62             close_xml_file = True
63         try:
64             # use librarian to parse meta-data
65             picture_xml = WLPicture.from_file(xml_file)
66
67             picture, created = Picture.objects.get_or_create(slug=picture_xml.slug)
68             if not created and not overwrite:
69                 raise Picture.AlreadyExists('Picture %s already exists' % picture_xml.slug)
70
71             picture.title = picture_xml.picture_info.title
72
73             picture.tags = catalogue.models.Tag.tags_from_info(picture_xml.picture_info)
74
75             img = picture_xml.image_file()
76             try:
77                 picture.image_file.save(path.basename(picture_xml.image_path), File(img))
78             finally:
79                 img.close()
80
81             picture.xml_file.save("%s.xml" % picture.slug, File(xml_file))
82             picture.save()
83         finally:
84             if close_xml_file:
85                 xml_file.close()
86         return picture