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