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