Show More
@@ -1,100 +1,102 b'' | |||
|
1 | 1 | from django.template.loader import render_to_string |
|
2 | 2 | from boards.models import Thread, Post |
|
3 | 3 | from django.db import models |
|
4 | 4 | from django.db.models import Count, Sum |
|
5 | 5 | from django.core.urlresolvers import reverse |
|
6 | 6 | from boards.models.base import Viewable |
|
7 | 7 | |
|
8 | 8 | __author__ = 'neko259' |
|
9 | 9 | |
|
10 | 10 | |
|
11 | 11 | class TagManager(models.Manager): |
|
12 | 12 | |
|
13 | 13 | def get_not_empty_tags(self): |
|
14 | 14 | """ |
|
15 | 15 | Gets tags that have non-archived threads. |
|
16 | 16 | """ |
|
17 | 17 | |
|
18 | 18 | tags = self.annotate(Count('threads')) \ |
|
19 | 19 | .filter(threads__count__gt=0).order_by('name') |
|
20 | 20 | |
|
21 | 21 | return tags |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | class Tag(models.Model, Viewable): |
|
25 | 25 | """ |
|
26 | 26 | A tag is a text node assigned to the thread. The tag serves as a board |
|
27 | 27 | section. There can be multiple tags for each thread |
|
28 | 28 | """ |
|
29 | 29 | |
|
30 | 30 | objects = TagManager() |
|
31 | 31 | |
|
32 | 32 | class Meta: |
|
33 | 33 | app_label = 'boards' |
|
34 | 34 | ordering = ('name',) |
|
35 | 35 | |
|
36 | 36 | name = models.CharField(max_length=100, db_index=True) |
|
37 | 37 | threads = models.ManyToManyField(Thread, null=True, |
|
38 | 38 | blank=True, related_name='tag+') |
|
39 | 39 | linked = models.ForeignKey('Tag', null=True, blank=True) |
|
40 | 40 | |
|
41 | 41 | def __unicode__(self): |
|
42 | 42 | return self.name |
|
43 | 43 | |
|
44 | 44 | def is_empty(self): |
|
45 | 45 | """ |
|
46 | 46 | Checks if the tag has some threads. |
|
47 | 47 | """ |
|
48 | 48 | |
|
49 | 49 | return self.get_thread_count() == 0 |
|
50 | 50 | |
|
51 | 51 | def get_thread_count(self): |
|
52 | 52 | return self.threads.count() |
|
53 | 53 | |
|
54 | 54 | def get_linked_tags(self): |
|
55 | 55 | """ |
|
56 | 56 | Gets tags linked to the current one. |
|
57 | 57 | """ |
|
58 | 58 | |
|
59 | 59 | tag_list = [] |
|
60 | 60 | self.get_linked_tags_list(tag_list) |
|
61 | 61 | |
|
62 | 62 | return tag_list |
|
63 | 63 | |
|
64 |
def get_linked_tags_list(self, tag_list= |
|
|
64 | def get_linked_tags_list(self, tag_list=None): | |
|
65 | 65 | """ |
|
66 | 66 | Returns the list of tags linked to current. The list can be got |
|
67 | 67 | through returned value or tag_list parameter |
|
68 | 68 | """ |
|
69 | if not tag_list: | |
|
70 | tag_list = [] | |
|
69 | 71 | |
|
70 | 72 | linked_tag = self.linked |
|
71 | 73 | |
|
72 | 74 | if linked_tag and not (linked_tag in tag_list): |
|
73 | 75 | tag_list.append(linked_tag) |
|
74 | 76 | |
|
75 | 77 | linked_tag.get_linked_tags_list(tag_list) |
|
76 | 78 | |
|
77 | 79 | def get_post_count(self, archived=False): |
|
78 | 80 | """ |
|
79 | 81 | Gets posts count for the tag's threads. |
|
80 | 82 | """ |
|
81 | 83 | |
|
82 | 84 | posts_count = 0 |
|
83 | 85 | |
|
84 | 86 | threads = self.threads.filter(archived=archived) |
|
85 | 87 | if threads.exists(): |
|
86 | 88 | posts_count = threads.annotate(posts_count=Count('replies')) \ |
|
87 | 89 | .aggregate(posts_sum=Sum('posts_count'))['posts_sum'] |
|
88 | 90 | |
|
89 | 91 | if not posts_count: |
|
90 | 92 | posts_count = 0 |
|
91 | 93 | |
|
92 | 94 | return posts_count |
|
93 | 95 | |
|
94 | 96 | def get_url(self): |
|
95 | 97 | return reverse('tag', kwargs={'tag_name': self.name}) |
|
96 | 98 | |
|
97 | 99 | def get_view(self, *args, **kwargs): |
|
98 | 100 | return render_to_string('boards/tag.html', { |
|
99 | 101 | 'tag': self, |
|
100 | 102 | }) |
@@ -1,10 +1,10 b'' | |||
|
1 | south | |
|
1 | 2 | line_profiler |
|
2 | 3 | haystack |
|
3 | 4 | pillow |
|
4 | 5 | django>=1.6 |
|
5 | 6 | django_cleanup |
|
6 | 7 | django-markupfield |
|
7 | 8 | markdown |
|
8 | python-markdown | |
|
9 | 9 | django-simple-captcha |
|
10 | 10 | line-profiler |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now