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