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