##// END OF EJS Templates
Cache tag post count
neko259 -
r973:110ab3b8 default
parent child Browse files
Show More
@@ -1,74 +1,76 b''
1 1 from django.template.loader import render_to_string
2 2 from django.db import models
3 3 from django.db.models import Count
4 4 from django.core.urlresolvers import reverse
5 5
6 6 from boards.models.base import Viewable
7 from boards.utils import cached_result
7 8
8 9
9 10 __author__ = 'neko259'
10 11
11 12
12 13 class TagManager(models.Manager):
13 14
14 15 def get_not_empty_tags(self):
15 16 """
16 17 Gets tags that have non-archived threads.
17 18 """
18 19
19 20 return self.filter(thread__archived=False)\
20 21 .annotate(num_threads=Count('thread')).filter(num_threads__gt=0)\
21 22 .order_by('-required', 'name')
22 23
23 24
24 25 class Tag(models.Model, Viewable):
25 26 """
26 27 A tag is a text node assigned to the thread. The tag serves as a board
27 28 section. There can be multiple tags for each thread
28 29 """
29 30
30 31 objects = TagManager()
31 32
32 33 class Meta:
33 34 app_label = 'boards'
34 35 ordering = ('name',)
35 36
36 37 name = models.CharField(max_length=100, db_index=True)
37 38 required = models.BooleanField(default=False)
38 39
39 40 def __str__(self):
40 41 return self.name
41 42
42 43 def is_empty(self) -> bool:
43 44 """
44 45 Checks if the tag has some threads.
45 46 """
46 47
47 48 return self.get_thread_count() == 0
48 49
49 50 def get_thread_count(self) -> int:
50 51 return self.get_threads().count()
51 52
52 53 def get_url(self):
53 54 return reverse('tag', kwargs={'tag_name': self.name})
54 55
55 56 def get_threads(self):
56 57 return self.thread_set.order_by('-bump_time')
57 58
58 59 def is_required(self):
59 60 return self.required
60 61
61 62 def get_view(self):
62 63 link = '<a class="tag" href="{}">{}</a>'.format(
63 64 self.get_url(), self.name)
64 65 if self.is_required():
65 66 link = '<b>{}</b>'.format(link)
66 67 return link
67 68
68 69 def get_search_view(self, *args, **kwargs):
69 70 return render_to_string('boards/tag.html', {
70 71 'tag': self,
71 72 })
72 73
74 @cached_result
73 75 def get_post_count(self):
74 76 return self.get_threads().aggregate(num_posts=Count('post'))['num_posts']
General Comments 0
You need to be logged in to leave comments. Login now