+
+ @classmethod
+ def crop_to_frame(cls, wlpic, image_file):
+ img = Image.open(image_file)
+ if wlpic.frame is None:
+ return img
+ img = img.crop(itertools.chain(*wlpic.frame))
+ return img
+
+ @staticmethod
+ def add_source_note(wlpic, img):
+ from PIL import ImageDraw, ImageFont
+ from librarian import get_resource
+
+ annotated = Image.new(img.mode,
+ (img.size[0], img.size[1] + 40),
+ (255, 255, 255)
+ )
+ annotated.paste(img, (0, 0))
+ annotation = Image.new(img.mode, (3000, 120), (255, 255, 255))
+ ImageDraw.Draw(annotation).text(
+ (30, 15),
+ wlpic.picture_info.source_name,
+ (0, 0, 0),
+ font=ImageFont.truetype(get_resource("fonts/DejaVuSerif.ttf"), 75)
+ )
+ annotated.paste(annotation.resize((1000, 40), Image.ANTIALIAS), (0, img.size[1]))
+ return annotated
+
+ @classmethod
+ def picture_list(cls, filter=None):
+ """Generates a hierarchical listing of all pictures
+ Pictures are optionally filtered with a test function.
+ """
+
+ pics = cls.objects.all().order_by('sort_key')\
+ .only('title', 'slug', 'image_file')
+
+ if filter:
+ pics = pics.filter(filter).distinct()
+
+ pics_by_author = SortedDict()
+ orphans = []
+ for tag in catalogue.models.Tag.objects.filter(category='author'):
+ pics_by_author[tag] = []
+
+ for pic in pics.iterator():
+ authors = list(pic.tags.filter(category='author'))
+ if authors:
+ for author in authors:
+ pics_by_author[author].append(pic)
+ else:
+ orphans.append(pic)
+
+ return pics_by_author, orphans
+
+ @property
+ def info(self):
+ if not hasattr(self, '_info'):
+ from librarian import dcparser
+ from librarian import picture
+ info = dcparser.parse(self.xml_file.path, picture.PictureInfo)
+ self._info = info
+ return self._info
+
+ def reset_short_html(self):
+ if self.id is None:
+ return
+
+ type(self).objects.filter(pk=self.pk).update(_related_info=None)
+ for area in self.areas.all().iterator():
+ area.reset_short_html()
+
+ try:
+ author = self.tags.filter(category='author')[0].sort_key
+ except IndexError:
+ author = u''
+ type(self).objects.filter(pk=self.pk).update(sort_key_author=author)
+
+ cache_key = "Picture.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 = "Picture.short_html/%d/%s" % (self.id, get_language())
+ short_html = get_cache('permanent').get(cache_key)
+ else:
+ short_html = None
+
+ if short_html is not None:
+ return mark_safe(short_html)
+ else:
+ tags = self.tags.filter(category__in=('author', 'kind', 'epoch', 'genre'))
+ tags = split_tags(tags)
+
+ short_html = unicode(render_to_string(
+ 'picture/picture_short.html',
+ {'picture': self, 'tags': tags}))
+
+ if self.id:
+ get_cache('permanent').set(cache_key, short_html)
+ return mark_safe(short_html)
+
+ def pretty_title(self, html_links=False):
+ picture = self
+ # TODO Add translations (related_tag_info)
+ names = [(tag.name,
+ catalogue.models.Tag.create_url('author', tag.slug))
+ for tag in self.tags.filter(category='author')]
+ names.append((self.title, self.get_absolute_url()))
+
+ if html_links:
+ names = ['<a href="%s">%s</a>' % (tag[1], tag[0]) for tag in names]
+ else:
+ names = [tag[0] for tag in names]
+ return ', '.join(names)
+
+ def related_info(self):
+ """Keeps info about related objects (tags) in cache field."""
+ if self._related_info is not None:
+ return self._related_info
+ else:
+ rel = {'tags': {}}
+
+ tags = self.tags.filter(category__in=(
+ 'author', 'kind', 'genre', 'epoch'))
+ tags = split_tags(tags)
+ for category in tags:
+ cat = []
+ for tag in tags[category]:
+ tag_info = {'slug': tag.slug, 'name': tag.name}
+ for lc, ln in settings.LANGUAGES:
+ tag_name = getattr(tag, "name_%s" % lc)
+ if tag_name:
+ tag_info["name_%s" % lc] = tag_name
+ cat.append(tag_info)
+ rel['tags'][category] = cat
+
+
+ if self.pk:
+ type(self).objects.filter(pk=self.pk).update(_related_info=rel)
+ return rel
+
+ # copied from book.py, figure out
+ def related_themes(self):
+ # self.theme_counter hides a computation, so a line below actually makes sense
+ theme_counter = self.theme_counter
+ picture_themes = list(catalogue.models.Tag.objects.filter(pk__in=theme_counter.keys()))
+ for tag in picture_themes:
+ tag.count = theme_counter[tag.pk]
+ return picture_themes
+
+ def reset_tag_counter(self):
+ if self.id is None:
+ return
+
+ cache_key = "Picture.tag_counter/%d" % self.id
+ permanent_cache.delete(cache_key)
+ if self.parent:
+ self.parent.reset_tag_counter()
+
+ @property
+ def tag_counter(self):
+ if self.id:
+ cache_key = "Picture.tag_counter/%d" % self.id
+ tags = permanent_cache.get(cache_key)
+ else:
+ tags = None
+
+ if tags is None:
+ tags = {}
+ # do we need to do this? there are no children here.
+ for tag in self.tags.exclude(category__in=('book', 'theme', 'thing', 'set')).order_by().iterator():
+ tags[tag.pk] = 1
+
+ if self.id:
+ permanent_cache.set(cache_key, tags)
+ return tags
+
+ def reset_theme_counter(self):
+ if self.id is None:
+ return
+
+ cache_key = "Picture.theme_counter/%d" % self.id
+ permanent_cache.delete(cache_key)
+
+ @property
+ def theme_counter(self):
+ if self.id:
+ cache_key = "Picture.theme_counter/%d" % self.id
+ tags = permanent_cache.get(cache_key)
+ else:
+ tags = None
+
+ if tags is None:
+ tags = {}
+ for area in PictureArea.objects.filter(picture=self).order_by().iterator():
+ for tag in area.tags.filter(category__in=('theme','thing')).order_by().iterator():
+ tags[tag.pk] = tags.get(tag.pk, 0) + 1
+
+ if self.id:
+ permanent_cache.set(cache_key, tags)
+ return tags