tag.py
82 lines
| 2.1 KiB
| text/x-python
|
PythonLexer
neko259
|
r385 | from boards.models import Post | |
neko259
|
r386 | from django.db import models | |
from django.db.models import Count | |||
neko259
|
r385 | ||
__author__ = 'neko259' | |||
TAG_FONT_MULTIPLIER = 0.1 | |||
MAX_TAG_FONT = 10 | |||
OPENING_POST_POPULARITY_WEIGHT = 2 | |||
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 post. The tag serves as a board | |||
section. There can be multiple tags for each message | |||
""" | |||
objects = TagManager() | |||
class Meta: | |||
app_label = 'boards' | |||
name = models.CharField(max_length=100) | |||
threads = models.ManyToManyField('Post', 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): | |||
posts_with_tag = Post.objects.get_threads(tag=self) | |||
reply_count = 0 | |||
for post in posts_with_tag: | |||
reply_count += post.get_reply_count() | |||
reply_count += OPENING_POST_POPULARITY_WEIGHT | |||
return reply_count | |||
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""" | |||
post_count = self.get_post_count() | |||
if post_count > MAX_TAG_FONT: | |||
post_count = MAX_TAG_FONT | |||
font_value = str(1 + (post_count - 1) * TAG_FONT_MULTIPLIER) | |||
return font_value |