- # 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