-
- affected_tags = fix_subtree(self, parent_tags)
- for tag in affected_tags:
- tasks.touch_tag(tag)
-
- book = self
- while book is not None:
- book.reset_tag_counter()
- book.reset_theme_counter()
- book = book.parent
-
- 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:
- rel['tags'][category] = [
- (t.name, t.slug) for t in tags[category]]
-
- 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
-
- def reset_tag_counter(self):
- if self.id is None:
- return
-
- cache_key = "Book.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 = "Book.tag_counter/%d" % self.id
- tags = permanent_cache.get(cache_key)
- else:
- tags = None
-
- if tags is None:
- tags = {}
- for child in self.children.all().order_by().iterator():
- for tag_pk, value in child.tag_counter.iteritems():
- tags[tag_pk] = tags.get(tag_pk, 0) + value
- for tag in self.tags.exclude(category__in=('book', 'theme', '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 = "Book.theme_counter/%d" % self.id
- permanent_cache.delete(cache_key)
- if self.parent:
- self.parent.reset_theme_counter()
-
- @property
- def theme_counter(self):
- if self.id:
- cache_key = "Book.theme_counter/%d" % self.id
- tags = permanent_cache.get(cache_key)
- else:
- tags = None
-
- if tags is None:
- tags = {}
- for fragment in Fragment.tagged.with_any([self.book_tag()]).order_by().iterator():
- for tag in fragment.tags.filter(category='theme').order_by().iterator():
- tags[tag.pk] = tags.get(tag.pk, 0) + 1
-
- if self.id:
- permanent_cache.set(cache_key, tags)
- return tags