##// END OF EJS Templates
Include tags that have only archived threads to the general tags list
neko259 -
r700:7ec0b915 1.8-dev
parent child Browse files
Show More
@@ -1,139 +1,138 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 MAX_TAG_FONT = 1
11 11 MIN_TAG_FONT = 0.2
12 12
13 13 TAG_POPULARITY_MULTIPLIER = 20
14 14
15 15 ARCHIVE_POPULARITY_MODIFIER = 0.5
16 16
17 17
18 18 class TagManager(models.Manager):
19 19
20 20 def get_not_empty_tags(self):
21 21 """
22 22 Gets tags that have non-archived threads.
23 23 """
24 24
25 25 tags = self.annotate(Count('threads')) \
26 .filter(threads__count__gt=0).filter(threads__archived=False) \
27 .order_by('name')
26 .filter(threads__count__gt=0).order_by('name')
28 27
29 28 return tags
30 29
31 30
32 31 class Tag(models.Model, Viewable):
33 32 """
34 33 A tag is a text node assigned to the thread. The tag serves as a board
35 34 section. There can be multiple tags for each thread
36 35 """
37 36
38 37 objects = TagManager()
39 38
40 39 class Meta:
41 40 app_label = 'boards'
42 41 ordering = ('name',)
43 42
44 43 name = models.CharField(max_length=100, db_index=True)
45 44 threads = models.ManyToManyField(Thread, null=True,
46 45 blank=True, related_name='tag+')
47 46 linked = models.ForeignKey('Tag', null=True, blank=True)
48 47
49 48 def __unicode__(self):
50 49 return self.name
51 50
52 51 def is_empty(self):
53 52 """
54 53 Checks if the tag has some threads.
55 54 """
56 55
57 56 return self.get_thread_count() == 0
58 57
59 58 def get_thread_count(self):
60 59 return self.threads.count()
61 60
62 61 def get_popularity(self):
63 62 """
64 63 Gets tag's popularity value as a percentage of overall board post
65 64 count.
66 65 """
67 66
68 67 all_post_count = Post.objects.count()
69 68
70 69 tag_reply_count = 0.0
71 70
72 71 tag_reply_count += self.get_post_count()
73 72 tag_reply_count +=\
74 73 self.get_post_count(archived=True) * ARCHIVE_POPULARITY_MODIFIER
75 74
76 75 popularity = tag_reply_count / all_post_count
77 76
78 77 return popularity
79 78
80 79 def get_linked_tags(self):
81 80 """
82 81 Gets tags linked to the current one.
83 82 """
84 83
85 84 tag_list = []
86 85 self.get_linked_tags_list(tag_list)
87 86
88 87 return tag_list
89 88
90 89 def get_linked_tags_list(self, tag_list=[]):
91 90 """
92 91 Returns the list of tags linked to current. The list can be got
93 92 through returned value or tag_list parameter
94 93 """
95 94
96 95 linked_tag = self.linked
97 96
98 97 if linked_tag and not (linked_tag in tag_list):
99 98 tag_list.append(linked_tag)
100 99
101 100 linked_tag.get_linked_tags_list(tag_list)
102 101
103 102 def get_font_value(self):
104 103 """
105 104 Gets tag font value to differ most popular tags in the list
106 105 """
107 106
108 107 popularity = self.get_popularity()
109 108
110 109 font_value = popularity * Tag.objects.get_not_empty_tags().count()
111 110 font_value = max(font_value, MIN_TAG_FONT)
112 111 font_value = min(font_value, MAX_TAG_FONT)
113 112
114 113 return str(font_value)
115 114
116 115 def get_post_count(self, archived=False):
117 116 """
118 117 Gets posts count for the tag's threads.
119 118 """
120 119
121 120 posts_count = 0
122 121
123 122 threads = self.threads.filter(archived=archived)
124 123 if threads.exists():
125 124 posts_count = threads.annotate(posts_count=Count('replies')).aggregate(
126 125 posts_sum=Sum('posts_count'))['posts_sum']
127 126
128 127 if not posts_count:
129 128 posts_count = 0
130 129
131 130 return posts_count
132 131
133 132 def get_url(self):
134 133 return reverse('tag', kwargs={'tag_name': self.name})
135 134
136 135 def get_view(self, *args, **kwargs):
137 136 return render_to_string('boards/tag.html', {
138 137 'tag': self,
139 }) No newline at end of file
138 })
General Comments 0
You need to be logged in to leave comments. Login now