diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -41,9 +41,9 @@ class Tag(models.Model): return self.name def is_empty(self): - return self.get_post_count() == 0 + return self.get_thread_count() == 0 - def get_post_count(self): + def get_thread_count(self): return self.threads.count() def get_popularity(self): @@ -51,13 +51,9 @@ class Tag(models.Model): tag_reply_count = 0.0 - for thread in self.threads.all(): - if thread.archived: - modifier = ARCHIVE_POPULARITY_MODIFIER - else: - modifier = 1 - tag_reply_count += thread.get_reply_count() * modifier \ - / thread.tags.all().count() + tag_reply_count += self.get_post_count() + tag_reply_count +=\ + self.get_post_count(archived=True) * ARCHIVE_POPULARITY_MODIFIER popularity = tag_reply_count / all_post_count @@ -92,3 +88,12 @@ class Tag(models.Model): font_value = min(font_value, MAX_TAG_FONT) return str(font_value) + + def get_post_count(self, archived=False): + posts_count = 0 + + for thread in self.threads.annotate(Count('replies')).filter( + archived=archived): + posts_count += thread.replies__count + + return posts_count