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