Show More
@@ -1,91 +1,91 b'' | |||||
1 | from boards.models import Thread |
|
1 | from boards.models import Thread | |
2 | from django.db import models |
|
2 | from django.db import models | |
3 | from django.db.models import Count |
|
3 | from django.db.models import Count | |
4 |
|
4 | |||
5 | __author__ = 'neko259' |
|
5 | __author__ = 'neko259' | |
6 |
|
6 | |||
7 | TAG_FONT_MULTIPLIER = 0.1 |
|
7 | TAG_FONT_MULTIPLIER = 0.1 | |
8 |
MAX_TAG_FONT = |
|
8 | MAX_TAG_FONT = 3 | |
9 |
|
9 | |||
10 |
OPENING_POST_POPULARITY = 0. |
|
10 | OPENING_POST_POPULARITY = 0.3 | |
11 |
REPLY_POPULARITY = 0. |
|
11 | REPLY_POPULARITY = 0.06 | |
12 | ARCHIVE_POPULARITY_MODIFIER = 0.1 |
|
12 | ARCHIVE_POPULARITY_MODIFIER = 0.1 | |
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).order_by('name') |
|
19 | .filter(threads__count__gt=0).order_by('name') | |
20 |
|
20 | |||
21 | return tags |
|
21 | return tags | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | class Tag(models.Model): |
|
24 | class Tag(models.Model): | |
25 | """ |
|
25 | """ | |
26 | A tag is a text node assigned to the thread. The tag serves as a board |
|
26 | 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 |
|
27 | section. There can be multiple tags for each thread | |
28 | """ |
|
28 | """ | |
29 |
|
29 | |||
30 | objects = TagManager() |
|
30 | objects = TagManager() | |
31 |
|
31 | |||
32 | class Meta: |
|
32 | class Meta: | |
33 | app_label = 'boards' |
|
33 | app_label = 'boards' | |
34 |
|
34 | |||
35 | name = models.CharField(max_length=100) |
|
35 | name = models.CharField(max_length=100) | |
36 | threads = models.ManyToManyField(Thread, null=True, |
|
36 | threads = models.ManyToManyField(Thread, null=True, | |
37 | blank=True, related_name='tag+') |
|
37 | blank=True, related_name='tag+') | |
38 | linked = models.ForeignKey('Tag', null=True, blank=True) |
|
38 | linked = models.ForeignKey('Tag', null=True, blank=True) | |
39 |
|
39 | |||
40 | def __unicode__(self): |
|
40 | def __unicode__(self): | |
41 | return self.name |
|
41 | return self.name | |
42 |
|
42 | |||
43 | def is_empty(self): |
|
43 | def is_empty(self): | |
44 | return self.get_post_count() == 0 |
|
44 | return self.get_post_count() == 0 | |
45 |
|
45 | |||
46 | def get_post_count(self): |
|
46 | def get_post_count(self): | |
47 | return self.threads.count() |
|
47 | return self.threads.count() | |
48 |
|
48 | |||
49 | def get_popularity(self): |
|
49 | def get_popularity(self): | |
50 | popularity = 0.0 |
|
50 | popularity = 0.0 | |
51 |
|
51 | |||
52 | for thread in self.threads.all(): |
|
52 | for thread in self.threads.all(): | |
53 | reply_count = thread.get_reply_count() |
|
53 | reply_count = thread.get_reply_count() | |
54 |
|
54 | |||
55 | thread_popularity = 0.0 |
|
55 | thread_popularity = 0.0 | |
56 | thread_popularity += REPLY_POPULARITY * reply_count |
|
56 | thread_popularity += REPLY_POPULARITY * reply_count | |
57 | thread_popularity += OPENING_POST_POPULARITY |
|
57 | thread_popularity += OPENING_POST_POPULARITY | |
58 | if thread.archived: |
|
58 | if thread.archived: | |
59 | thread_popularity *= ARCHIVE_POPULARITY_MODIFIER |
|
59 | thread_popularity *= ARCHIVE_POPULARITY_MODIFIER | |
60 | popularity += thread_popularity |
|
60 | popularity += thread_popularity | |
61 |
|
61 | |||
62 | return popularity |
|
62 | return popularity | |
63 |
|
63 | |||
64 | def get_linked_tags(self): |
|
64 | def get_linked_tags(self): | |
65 | tag_list = [] |
|
65 | tag_list = [] | |
66 | self.get_linked_tags_list(tag_list) |
|
66 | self.get_linked_tags_list(tag_list) | |
67 |
|
67 | |||
68 | return tag_list |
|
68 | return tag_list | |
69 |
|
69 | |||
70 | def get_linked_tags_list(self, tag_list=[]): |
|
70 | def get_linked_tags_list(self, tag_list=[]): | |
71 | """ |
|
71 | """ | |
72 | Returns the list of tags linked to current. The list can be got |
|
72 | Returns the list of tags linked to current. The list can be got | |
73 | through returned value or tag_list parameter |
|
73 | through returned value or tag_list parameter | |
74 | """ |
|
74 | """ | |
75 |
|
75 | |||
76 | linked_tag = self.linked |
|
76 | linked_tag = self.linked | |
77 |
|
77 | |||
78 | if linked_tag and not (linked_tag in tag_list): |
|
78 | if linked_tag and not (linked_tag in tag_list): | |
79 | tag_list.append(linked_tag) |
|
79 | tag_list.append(linked_tag) | |
80 |
|
80 | |||
81 | linked_tag.get_linked_tags_list(tag_list) |
|
81 | linked_tag.get_linked_tags_list(tag_list) | |
82 |
|
82 | |||
83 | def get_font_value(self): |
|
83 | def get_font_value(self): | |
84 | """Get tag font value to differ most popular tags in the list""" |
|
84 | """Get tag font value to differ most popular tags in the list""" | |
85 |
|
85 | |||
86 | popularity = self.get_popularity() |
|
86 | popularity = self.get_popularity() | |
87 |
|
87 | |||
88 | font_value = 1 + (popularity - 1) * TAG_FONT_MULTIPLIER |
|
88 | font_value = 1 + (popularity - 1) * TAG_FONT_MULTIPLIER | |
89 | font_value = min(font_value, MAX_TAG_FONT) |
|
89 | font_value = min(font_value, MAX_TAG_FONT) | |
90 |
|
90 | |||
91 | return str(font_value) |
|
91 | return str(font_value) |
General Comments 0
You need to be logged in to leave comments.
Login now