Show More
@@ -1,103 +1,104 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 | tags = self.annotate(Count('threads')) \ |
|
18 | tags = self.annotate(Count('threads')) \ | |
19 |
.filter(threads__count__gt=0). |
|
19 | .filter(threads__count__gt=0).filter(threads__archived=False) \ | |
|
20 | .order_by('name') | |||
20 |
|
21 | |||
21 | return tags |
|
22 | return tags | |
22 |
|
23 | |||
23 |
|
24 | |||
24 | class Tag(models.Model): |
|
25 | class Tag(models.Model): | |
25 | """ |
|
26 | """ | |
26 | A tag is a text node assigned to the thread. The tag serves as a board |
|
27 | A tag is a text node assigned to the thread. The tag serves as a board | |
27 | section. There can be multiple tags for each thread |
|
28 | section. There can be multiple tags for each thread | |
28 | """ |
|
29 | """ | |
29 |
|
30 | |||
30 | objects = TagManager() |
|
31 | objects = TagManager() | |
31 |
|
32 | |||
32 | class Meta: |
|
33 | class Meta: | |
33 | app_label = 'boards' |
|
34 | app_label = 'boards' | |
34 |
|
35 | |||
35 | name = models.CharField(max_length=100, db_index=True) |
|
36 | name = models.CharField(max_length=100, db_index=True) | |
36 | threads = models.ManyToManyField(Thread, null=True, |
|
37 | threads = models.ManyToManyField(Thread, null=True, | |
37 | blank=True, related_name='tag+') |
|
38 | blank=True, related_name='tag+') | |
38 | linked = models.ForeignKey('Tag', null=True, blank=True) |
|
39 | linked = models.ForeignKey('Tag', null=True, blank=True) | |
39 |
|
40 | |||
40 | def __unicode__(self): |
|
41 | def __unicode__(self): | |
41 | return self.name |
|
42 | return self.name | |
42 |
|
43 | |||
43 | def is_empty(self): |
|
44 | def is_empty(self): | |
44 | return self.get_thread_count() == 0 |
|
45 | return self.get_thread_count() == 0 | |
45 |
|
46 | |||
46 | def get_thread_count(self): |
|
47 | def get_thread_count(self): | |
47 | return self.threads.count() |
|
48 | return self.threads.count() | |
48 |
|
49 | |||
49 | def get_popularity(self): |
|
50 | def get_popularity(self): | |
50 | all_post_count = Post.objects.all().count() |
|
51 | all_post_count = Post.objects.all().count() | |
51 |
|
52 | |||
52 | tag_reply_count = 0.0 |
|
53 | tag_reply_count = 0.0 | |
53 |
|
54 | |||
54 | tag_reply_count += self.get_post_count() |
|
55 | tag_reply_count += self.get_post_count() | |
55 | tag_reply_count +=\ |
|
56 | tag_reply_count +=\ | |
56 | self.get_post_count(archived=True) * ARCHIVE_POPULARITY_MODIFIER |
|
57 | self.get_post_count(archived=True) * ARCHIVE_POPULARITY_MODIFIER | |
57 |
|
58 | |||
58 | popularity = tag_reply_count / all_post_count |
|
59 | popularity = tag_reply_count / all_post_count | |
59 |
|
60 | |||
60 | return popularity |
|
61 | return popularity | |
61 |
|
62 | |||
62 | def get_linked_tags(self): |
|
63 | def get_linked_tags(self): | |
63 | tag_list = [] |
|
64 | tag_list = [] | |
64 | self.get_linked_tags_list(tag_list) |
|
65 | self.get_linked_tags_list(tag_list) | |
65 |
|
66 | |||
66 | return tag_list |
|
67 | return tag_list | |
67 |
|
68 | |||
68 | def get_linked_tags_list(self, tag_list=[]): |
|
69 | def get_linked_tags_list(self, tag_list=[]): | |
69 | """ |
|
70 | """ | |
70 | Returns the list of tags linked to current. The list can be got |
|
71 | Returns the list of tags linked to current. The list can be got | |
71 | through returned value or tag_list parameter |
|
72 | through returned value or tag_list parameter | |
72 | """ |
|
73 | """ | |
73 |
|
74 | |||
74 | linked_tag = self.linked |
|
75 | linked_tag = self.linked | |
75 |
|
76 | |||
76 | if linked_tag and not (linked_tag in tag_list): |
|
77 | if linked_tag and not (linked_tag in tag_list): | |
77 | tag_list.append(linked_tag) |
|
78 | tag_list.append(linked_tag) | |
78 |
|
79 | |||
79 | linked_tag.get_linked_tags_list(tag_list) |
|
80 | linked_tag.get_linked_tags_list(tag_list) | |
80 |
|
81 | |||
81 | def get_font_value(self): |
|
82 | def get_font_value(self): | |
82 | """Get tag font value to differ most popular tags in the list""" |
|
83 | """Get tag font value to differ most popular tags in the list""" | |
83 |
|
84 | |||
84 | popularity = self.get_popularity() |
|
85 | popularity = self.get_popularity() | |
85 |
|
86 | |||
86 | font_value = popularity * Tag.objects.get_not_empty_tags().count() |
|
87 | font_value = popularity * Tag.objects.get_not_empty_tags().count() | |
87 | font_value = max(font_value, MIN_TAG_FONT) |
|
88 | font_value = max(font_value, MIN_TAG_FONT) | |
88 | font_value = min(font_value, MAX_TAG_FONT) |
|
89 | font_value = min(font_value, MAX_TAG_FONT) | |
89 |
|
90 | |||
90 | return str(font_value) |
|
91 | return str(font_value) | |
91 |
|
92 | |||
92 | def get_post_count(self, archived=False): |
|
93 | def get_post_count(self, archived=False): | |
93 | posts_count = 0 |
|
94 | posts_count = 0 | |
94 |
|
95 | |||
95 | threads = self.threads.filter(archived=archived) |
|
96 | threads = self.threads.filter(archived=archived) | |
96 | if threads.exists(): |
|
97 | if threads.exists(): | |
97 | posts_count = threads.annotate(posts_count=Count('replies')).aggregate( |
|
98 | posts_count = threads.annotate(posts_count=Count('replies')).aggregate( | |
98 | posts_sum=Sum('posts_count'))['posts_sum'] |
|
99 | posts_sum=Sum('posts_count'))['posts_sum'] | |
99 |
|
100 | |||
100 | if not posts_count: |
|
101 | if not posts_count: | |
101 | posts_count = 0 |
|
102 | posts_count = 0 | |
102 |
|
103 | |||
103 | return posts_count |
|
104 | return posts_count |
General Comments 0
You need to be logged in to leave comments.
Login now