# HG changeset patch # User neko259 # Date 2013-11-18 21:42:51 # Node ID 72926030e39ae1b59cd7f44a985f579f0c7e6170 # Parent d51739f72332d01c2c523feaff732da801c5f112 Split up tag module from post module diff --git a/boards/models/__init__.py b/boards/models/__init__.py --- a/boards/models/__init__.py +++ b/boards/models/__init__.py @@ -1,7 +1,7 @@ __author__ = 'neko259' from boards.models.post import Post -from boards.models.post import Tag +from boards.models.tag import Tag from boards.models.post import Ban from boards.models.post import Setting from boards.models.post import User diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -16,10 +16,6 @@ from boards import thumbs import re -TAG_FONT_MULTIPLIER = 0.2 - -MAX_TAG_FONT = 4 - BAN_REASON_MAX_LENGTH = 200 BAN_REASON_AUTO = 'Auto' @@ -34,7 +30,6 @@ NO_PARENT = -1 NO_IP = '0.0.0.0' UNKNOWN_UA = '' ALL_PAGES = -1 -OPENING_POST_POPULARITY_WEIGHT = 2 IMAGES_DIRECTORY = 'images/' FILE_EXTENSION_DELIMITER = '.' @@ -201,80 +196,6 @@ class PostManager(models.Manager): return int(math.ceil(thread_count / float(settings.THREADS_PER_PAGE))) -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 - - class Post(models.Model): """A post is a message.""" @@ -311,7 +232,7 @@ class Post(models.Model): poster_user_agent = models.TextField() thread = models.ForeignKey('Post', null=True, default=None) - tags = models.ManyToManyField(Tag) + tags = models.ManyToManyField('Tag') last_edit_time = models.DateTimeField() bump_time = models.DateTimeField() user = models.ForeignKey('User', null=True, default=None) @@ -392,7 +313,7 @@ class User(models.Model): registration_time = models.DateTimeField() - fav_tags = models.ManyToManyField(Tag, null=True, blank=True) + fav_tags = models.ManyToManyField('Tag', null=True, blank=True) fav_threads = models.ManyToManyField(Post, related_name='+', null=True, blank=True) diff --git a/boards/models/tag.py b/boards/models/tag.py new file mode 100644 --- /dev/null +++ b/boards/models/tag.py @@ -0,0 +1,85 @@ +from boards.models import Post + +__author__ = 'neko259' + + +from django.db import models +from django.db.models import Count + +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 \ No newline at end of file diff --git a/boards/views.py b/boards/views.py --- a/boards/views.py +++ b/boards/views.py @@ -19,8 +19,8 @@ import boards from boards import utils from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm -from boards.models.post import Post, Tag, Ban, User, RANK_USER, \ - SETTING_MODERATE, REGEX_REPLY +from boards.models import Post, Tag, Ban, User +from boards.models.post import RANK_USER, SETTING_MODERATE, REGEX_REPLY from boards import authors from boards.utils import get_client_ip import neboard