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