Show More
@@ -1,94 +1,99 b'' | |||
|
1 | 1 | from boards.models import Thread, Post |
|
2 | 2 | from django.db import models |
|
3 | 3 | from django.db.models import Count |
|
4 | 4 | |
|
5 | 5 | __author__ = 'neko259' |
|
6 | 6 | |
|
7 | 7 | MAX_TAG_FONT = 1 |
|
8 | 8 | MIN_TAG_FONT = 0.2 |
|
9 | 9 | |
|
10 | 10 | TAG_POPULARITY_MULTIPLIER = 20 |
|
11 | 11 | |
|
12 | 12 | ARCHIVE_POPULARITY_MODIFIER = 0.5 |
|
13 | 13 | |
|
14 | 14 | |
|
15 | 15 | class TagManager(models.Manager): |
|
16 | 16 | |
|
17 | 17 | def get_not_empty_tags(self): |
|
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): |
|
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 | |
|
35 | 35 | name = models.CharField(max_length=100) |
|
36 | 36 | threads = models.ManyToManyField(Thread, null=True, |
|
37 | 37 | blank=True, related_name='tag+') |
|
38 | 38 | linked = models.ForeignKey('Tag', null=True, blank=True) |
|
39 | 39 | |
|
40 | 40 | def __unicode__(self): |
|
41 | 41 | return self.name |
|
42 | 42 | |
|
43 | 43 | def is_empty(self): |
|
44 |
return self.get_ |
|
|
44 | return self.get_thread_count() == 0 | |
|
45 | 45 | |
|
46 |
def get_ |
|
|
46 | def get_thread_count(self): | |
|
47 | 47 | return self.threads.count() |
|
48 | 48 | |
|
49 | 49 | def get_popularity(self): |
|
50 | 50 | all_post_count = Post.objects.all().count() |
|
51 | 51 | |
|
52 | 52 | tag_reply_count = 0.0 |
|
53 | 53 | |
|
54 | for thread in self.threads.all(): | |
|
55 | if thread.archived: | |
|
56 |
|
|
|
57 | else: | |
|
58 | modifier = 1 | |
|
59 | tag_reply_count += thread.get_reply_count() * modifier \ | |
|
60 | / thread.tags.all().count() | |
|
54 | tag_reply_count += self.get_post_count() | |
|
55 | tag_reply_count +=\ | |
|
56 | self.get_post_count(archived=True) * ARCHIVE_POPULARITY_MODIFIER | |
|
61 | 57 | |
|
62 | 58 | popularity = tag_reply_count / all_post_count |
|
63 | 59 | |
|
64 | 60 | return popularity |
|
65 | 61 | |
|
66 | 62 | def get_linked_tags(self): |
|
67 | 63 | tag_list = [] |
|
68 | 64 | self.get_linked_tags_list(tag_list) |
|
69 | 65 | |
|
70 | 66 | return tag_list |
|
71 | 67 | |
|
72 | 68 | def get_linked_tags_list(self, tag_list=[]): |
|
73 | 69 | """ |
|
74 | 70 | Returns the list of tags linked to current. The list can be got |
|
75 | 71 | through returned value or tag_list parameter |
|
76 | 72 | """ |
|
77 | 73 | |
|
78 | 74 | linked_tag = self.linked |
|
79 | 75 | |
|
80 | 76 | if linked_tag and not (linked_tag in tag_list): |
|
81 | 77 | tag_list.append(linked_tag) |
|
82 | 78 | |
|
83 | 79 | linked_tag.get_linked_tags_list(tag_list) |
|
84 | 80 | |
|
85 | 81 | def get_font_value(self): |
|
86 | 82 | """Get tag font value to differ most popular tags in the list""" |
|
87 | 83 | |
|
88 | 84 | popularity = self.get_popularity() |
|
89 | 85 | |
|
90 | 86 | font_value = popularity * Tag.objects.get_not_empty_tags().count() |
|
91 | 87 | font_value = max(font_value, MIN_TAG_FONT) |
|
92 | 88 | font_value = min(font_value, MAX_TAG_FONT) |
|
93 | 89 | |
|
94 | 90 | return str(font_value) |
|
91 | ||
|
92 | def get_post_count(self, archived=False): | |
|
93 | posts_count = 0 | |
|
94 | ||
|
95 | for thread in self.threads.annotate(Count('replies')).filter( | |
|
96 | archived=archived): | |
|
97 | posts_count += thread.replies__count | |
|
98 | ||
|
99 | return posts_count |
General Comments 0
You need to be logged in to leave comments.
Login now