+permanent_cache = caches['permanent']
+
+picture_storage = FileSystemStorage(location=path.join(
+ settings.MEDIA_ROOT, 'pictures'),
+ base_url=settings.MEDIA_URL + "pictures/")
+
+
+class PictureArea(models.Model):
+ picture = models.ForeignKey('picture.Picture', related_name='areas')
+ area = jsonfield.JSONField(_('area'), default={}, editable=False)
+ kind = models.CharField(_('kind'), max_length=10, blank=False,
+ null=False, db_index=True,
+ choices=(('thing', _('thing')),
+ ('theme', _('theme'))))
+
+ objects = models.Manager()
+ tagged = managers.ModelTaggedItemManager(catalogue.models.Tag)
+ tags = managers.TagDescriptor(catalogue.models.Tag)
+ tag_relations = GenericRelation(catalogue.models.Tag.intermediary_table_model)
+
+ @classmethod
+ def rectangle(cls, picture, kind, coords):
+ pa = PictureArea()
+ pa.picture = picture
+ pa.kind = kind
+ pa.area = coords
+ return pa
+
+ def reset_short_html(self):
+ if self.id is None:
+ return
+
+ cache_key = "PictureArea.short_html/%d/%s"
+ for lang, langname in settings.LANGUAGES:
+ permanent_cache.delete(cache_key % (self.id, lang))
+
+
+ def short_html(self):
+ if self.id:
+ cache_key = "PictureArea.short_html/%d/%s" % (self.id, get_language())
+ short_html = permanent_cache.get(cache_key)
+ else:
+ short_html = None
+
+ if short_html is not None:
+ return mark_safe(short_html)
+ else:
+ theme = self.tags.filter(category='theme')
+ theme = theme and theme[0] or None
+ thing = self.tags.filter(category='thing')
+ thing = thing and thing[0] or None
+ area = self
+ short_html = unicode(render_to_string(
+ 'picture/picturearea_short.html', locals()))
+ if self.id:
+ permanent_cache.set(cache_key, short_html)
+ return mark_safe(short_html)