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