- 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