##// END OF EJS Templates
More fixes...
More fixes...

File last commit:

r602:04582070 default
r602:04582070 default
Show More
tag.py
93 lines | 2.4 KiB | text/x-python | PythonLexer
from boards.models import Thread, Post
from django.db import models
from django.db.models import Count
__author__ = 'neko259'
MAX_TAG_FONT = 1
MIN_TAG_FONT = 0.2
TAG_POPULARITY_MULTIPLIER = 20
ARCHIVE_POPULARITY_MODIFIER = 0.5
class TagManager(models.Manager):
def get_not_empty_tags(self):
tags = self.annotate(Count('threads')) \
.filter(threads__count__gt=0).order_by('name')
return tags
class Tag(models.Model):
"""
A tag is a text node assigned to the thread. The tag serves as a board
section. There can be multiple tags for each thread
"""
objects = TagManager()
class Meta:
app_label = 'boards'
name = models.CharField(max_length=100)
threads = models.ManyToManyField(Thread, null=True,
blank=True, related_name='tag+')
linked = models.ForeignKey('Tag', null=True, blank=True)
def __unicode__(self):
return self.name
def is_empty(self):
return self.get_post_count() == 0
def get_post_count(self):
return self.threads.count()
def get_popularity(self):
all_post_count = Post.objects.all().count()
tag_reply_count = 0.0
for thread in self.threads.all():
if thread.archived:
modifier = ARCHIVE_POPULARITY_MODIFIER
else:
modifier = 1
tag_reply_count += thread.get_reply_count() * modifier
popularity = tag_reply_count / all_post_count / thread.tags.all().count()
return popularity
def get_linked_tags(self):
tag_list = []
self.get_linked_tags_list(tag_list)
return tag_list
def get_linked_tags_list(self, tag_list=[]):
"""
Returns the list of tags linked to current. The list can be got
through returned value or tag_list parameter
"""
linked_tag = self.linked
if linked_tag and not (linked_tag in tag_list):
tag_list.append(linked_tag)
linked_tag.get_linked_tags_list(tag_list)
def get_font_value(self):
"""Get tag font value to differ most popular tags in the list"""
popularity = self.get_popularity()
font_value = popularity * Tag.objects.get_not_empty_tags().count()
font_value = max(font_value, MIN_TAG_FONT)
font_value = min(font_value, MAX_TAG_FONT)
return str(font_value)