- def related_info(self):
- """Keeps info about related objects (tags, media) in cache field."""
- if self._related_info is not None:
- return self._related_info
- else:
- rel = {'tags': {}, 'media': {}}
-
- 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
-
- for media_format in BookMedia.formats:
- rel['media'][media_format] = self.has_media(media_format)
-
- book = self
- parents = []
- while book.parent:
- parents.append((book.parent.title, book.parent.slug))
- book = book.parent
- parents = parents[::-1]
- if parents:
- rel['parents'] = parents
-
- if self.pk:
- type(self).objects.filter(pk=self.pk).update(_related_info=rel)
- return rel
-
- def related_themes(self):
- theme_counter = self.theme_counter
- book_themes = list(Tag.objects.filter(pk__in=theme_counter.keys()))
- for tag in book_themes:
- tag.count = theme_counter[tag.pk]
- return book_themes