1 from django.db import models
2 import catalogue.models
3 from django.db.models import permalink
4 from sorl.thumbnail import ImageField
5 from django.conf import settings
6 from django.core.files.storage import FileSystemStorage
7 from django.utils.datastructures import SortedDict
8 from librarian import dcparser, picture
10 from django.utils.translation import ugettext_lazy as _
11 from newtagging import managers
15 picture_storage = FileSystemStorage(location=path.join(settings.MEDIA_ROOT, 'pictures'), base_url=settings.MEDIA_URL + "pictures/")
18 class Picture(models.Model):
23 title = models.CharField(_('title'), max_length=120)
24 slug = models.SlugField(_('slug'), max_length=120, db_index=True, unique=True)
25 sort_key = models.CharField(_('sort key'), max_length=120, db_index=True, editable=False)
26 created_at = models.DateTimeField(_('creation date'), auto_now_add=True, db_index=True)
27 changed_at = models.DateTimeField(_('creation date'), auto_now=True, db_index=True)
28 xml_file = models.FileField('xml_file', upload_to="xml", storage=picture_storage)
29 image_file = ImageField(_('image_file'), upload_to="images", storage=picture_storage)
30 objects = models.Manager()
31 tagged = managers.ModelTaggedItemManager(catalogue.models.Tag)
32 tags = managers.TagDescriptor(catalogue.models.Tag)
34 class AlreadyExists(Exception):
38 ordering = ('sort_key',)
40 verbose_name = _('picture')
41 verbose_name_plural = _('pictures')
43 URLID_RE = r'[a-z0-9-]+'
44 FILEID_RE = r'[a-z0-9-]+'
46 def save(self, force_insert=False, force_update=False, reset_short_html=True, **kwargs):
47 from sortify import sortify
49 self.sort_key = sortify(self.title)
51 ret = super(Picture, self).save(force_insert, force_update)
55 def __unicode__(self):
59 def get_absolute_url(self):
60 return ('picture.views.picture_detail', [self.urlid()])
66 def from_xml_file(cls, xml_file, image_file=None, overwrite=False):
68 Import xml and it's accompanying image file.
70 from django.core.files import File
71 from librarian.picture import WLPicture
72 close_xml_file = False
74 if not isinstance(xml_file, File):
75 xml_file = File(open(xml_file))
78 # use librarian to parse meta-data
79 picture_xml = WLPicture.from_file(xml_file)
81 picture, created = Picture.objects.get_or_create(slug=picture_xml.slug)
82 if not created and not overwrite:
83 raise Picture.AlreadyExists('Picture %s already exists' % picture_xml.slug)
85 picture.title = picture_xml.picture_info.title
87 picture.tags = catalogue.models.Tag.tags_from_info(picture_xml.picture_info)
89 if image_file is not None:
92 img = picture_xml.image_file()
94 picture.image_file.save(path.basename(picture_xml.image_path), File(img))
96 picture.xml_file.save("%s.xml" % picture.slug, File(xml_file))
104 def picture_list(cls, filter=None):
105 """Generates a hierarchical listing of all pictures
106 Pictures are optionally filtered with a test function.
109 pics = cls.objects.all().order_by('sort_key')\
110 .only('title', 'slug', 'image_file')
113 pics = pics.filter(filter).distinct()
115 pics_by_author = SortedDict()
117 for tag in catalogue.models.Tag.objects.filter(category='author'):
118 pics_by_author[tag] = []
121 authors = list(pic.tags.filter(category='author'))
123 for author in authors:
124 pics_by_author[author].append(pic)
128 return pics_by_author, orphans
132 if not hasattr(self, '_info'):
133 info = dcparser.parse(self.xml_file.path, picture.PictureInfo)