diff --git a/boards/models/tag.py b/boards/models/tag.py
--- a/boards/models/tag.py
+++ b/boards/models/tag.py
@@ -1,9 +1,8 @@
from django.template.loader import render_to_string
from django.db import models
-from django.db.models import Count, Sum
+from django.db.models import Count
from django.core.urlresolvers import reverse
-from boards.models import Thread
from boards.models.base import Viewable
@@ -17,13 +16,8 @@ class TagManager(models.Manager):
Gets tags that have non-archived threads.
"""
- not_empty_tags = list()
- tags = self.order_by('-required', 'name')
- for tag in tags:
- if tag.get_thread_count() > 0:
- not_empty_tags.append(tag)
-
- return not_empty_tags
+ return self.filter(thread__archived=False)\
+ .annotate(num_threads=Count('thread')).filter(num_threads__gt=0)
class Tag(models.Model, Viewable):
@@ -54,41 +48,23 @@ class Tag(models.Model, Viewable):
def get_thread_count(self) -> int:
return self.get_threads().count()
- def get_post_count(self, archived=False):
- """
- Gets posts count for the tag's threads.
- """
-
- posts_count = 0
-
- threads = self.get_threads().filter(archived=archived)
- if threads.exists():
- posts_count = threads.annotate(posts_count=Count('replies')) \
- .aggregate(posts_sum=Sum('posts_count'))['posts_sum']
-
- if not posts_count:
- posts_count = 0
-
- return posts_count
-
def get_url(self):
return reverse('tag', kwargs={'tag_name': self.name})
def get_threads(self):
- return Thread.objects.filter(tags__in=[self]).order_by('-bump_time')
+ return self.thread_set.order_by('-bump_time')
def is_required(self):
return self.required
def get_view(self):
- #prefix = '##' if self.is_required() else '#'
link = '{}'.format(
- self.get_url(), self.name)
+ self.get_url(), self.name)
if self.is_required():
link = '{}'.format(link)
return link
def get_search_view(self, *args, **kwargs):
return render_to_string('boards/tag.html', {
- 'tag': self,
- })
+ 'tag': self,
+ })