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