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
11 picture_storage = FileSystemStorage(location=path.join(settings.MEDIA_ROOT, 'pictures'), base_url=settings.MEDIA_URL + "pictures/")
14 class Picture(models.Model):
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)
30 class AlreadyExists(Exception):
34 ordering = ('sort_key',)
36 verbose_name = _('picture')
37 verbose_name_plural = _('pictures')
39 def save(self, force_insert=False, force_update=False, reset_short_html=True, **kwargs):
40 from sortify import sortify
42 self.sort_key = sortify(self.title)
44 ret = super(Picture, self).save(force_insert, force_update)
48 def __unicode__(self):
52 def from_xml_file(cls, xml_file, images_path=None, overwrite=False):
54 Import xml and it's accompanying image file.
56 from django.core.files import File
57 from librarian.picture import WLPicture
58 close_xml_file = False
60 if not isinstance(xml_file, File):
61 xml_file = File(open(xml_file))
64 # use librarian to parse meta-data
65 picture_xml = WLPicture.from_file(xml_file)
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)
71 picture.title = picture_xml.picture_info.title
73 picture.tags = catalogue.models.Tag.tags_from_info(picture_xml.picture_info)
75 img = picture_xml.image_file()
77 picture.image_file.save(path.basename(picture_xml.image_path), File(img))
81 picture.xml_file.save("%s.xml" % picture.slug, File(xml_file))