diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -4,7 +4,8 @@ from django import forms from django.forms.util import ErrorList from django.utils.translation import ugettext_lazy as _ import time -from boards.models.post import TITLE_MAX_LENGTH, User +from boards.models.post import TITLE_MAX_LENGTH +from boards.models import User from neboard import settings from boards import utils import boards.settings as board_settings diff --git a/boards/models/__init__.py b/boards/models/__init__.py --- a/boards/models/__init__.py +++ b/boards/models/__init__.py @@ -2,6 +2,6 @@ from boards.models.post import Post from boards.models.tag import Tag -from boards.models.post import Ban -from boards.models.post import Setting -from boards.models.post import User +from boards.models.user import Ban +from boards.models.user import Setting +from boards.models.user import User diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -2,22 +2,16 @@ import os from random import random import time import math -from django.core.cache import cache +import re from django.db import models -from django.db.models import Count from django.http import Http404 from django.utils import timezone from markupfield.fields import MarkupField -from boards import settings as board_settings from neboard import settings from boards import thumbs -import re - -BAN_REASON_MAX_LENGTH = 200 - BAN_REASON_AUTO = 'Auto' IMAGE_THUMB_SIZE = (200, 150) @@ -33,10 +27,6 @@ ALL_PAGES = -1 IMAGES_DIRECTORY = 'images/' FILE_EXTENSION_DELIMITER = '.' -RANK_ADMIN = 0 -RANK_MODERATOR = 10 -RANK_USER = 100 - SETTING_MODERATE = "moderate" REGEX_REPLY = re.compile('>>(\d+)') @@ -303,95 +293,3 @@ class Post(models.Model): return self.referenced_posts.count() > 0 -class User(models.Model): - - class Meta: - app_label = 'boards' - - user_id = models.CharField(max_length=50) - rank = models.IntegerField() - - registration_time = models.DateTimeField() - - fav_tags = models.ManyToManyField('Tag', null=True, blank=True) - fav_threads = models.ManyToManyField(Post, related_name='+', null=True, - blank=True) - - def save_setting(self, name, value): - setting, created = Setting.objects.get_or_create(name=name, user=self) - setting.value = str(value) - setting.save() - - return setting - - def get_setting(self, name): - if Setting.objects.filter(name=name, user=self).exists(): - setting = Setting.objects.get(name=name, user=self) - setting_value = setting.value - else: - setting_value = None - - return setting_value - - def is_moderator(self): - return RANK_MODERATOR >= self.rank - - def get_sorted_fav_tags(self): - cache_key = self._get_tag_cache_key() - fav_tags = cache.get(cache_key) - if fav_tags: - return fav_tags - - tags = self.fav_tags.annotate(Count('threads')) \ - .filter(threads__count__gt=0).order_by('name') - - if tags: - cache.set(cache_key, tags, board_settings.CACHE_TIMEOUT) - - return tags - - def get_post_count(self): - return Post.objects.filter(user=self).count() - - def __unicode__(self): - return self.user_id + '(' + str(self.rank) + ')' - - def get_last_access_time(self): - posts = Post.objects.filter(user=self) - if posts.count() > 0: - return posts.latest('pub_time').pub_time - - def add_tag(self, tag): - self.fav_tags.add(tag) - cache.delete(self._get_tag_cache_key()) - - def remove_tag(self, tag): - self.fav_tags.remove(tag) - cache.delete(self._get_tag_cache_key()) - - def _get_tag_cache_key(self): - return self.user_id + '_tags' - - -class Setting(models.Model): - - class Meta: - app_label = 'boards' - - name = models.CharField(max_length=50) - value = models.CharField(max_length=50) - user = models.ForeignKey(User) - - -class Ban(models.Model): - - class Meta: - app_label = 'boards' - - ip = models.GenericIPAddressField() - reason = models.CharField(default=BAN_REASON_AUTO, - max_length=BAN_REASON_MAX_LENGTH) - can_read = models.BooleanField(default=True) - - def __unicode__(self): - return self.ip diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -1,11 +1,9 @@ from boards.models import Post +from django.db import models +from django.db.models import Count __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 diff --git a/boards/models/user.py b/boards/models/user.py new file mode 100644 --- /dev/null +++ b/boards/models/user.py @@ -0,0 +1,108 @@ +from django.db import models +from django.db.models import Count +from boards import settings +from boards.models import Post +from django.core.cache import cache + +__author__ = 'neko259' + +RANK_ADMIN = 0 +RANK_MODERATOR = 10 +RANK_USER = 100 + +BAN_REASON_AUTO = 'Auto' +BAN_REASON_MAX_LENGTH = 200 + + +class User(models.Model): + + class Meta: + app_label = 'boards' + + user_id = models.CharField(max_length=50) + rank = models.IntegerField() + + registration_time = models.DateTimeField() + + fav_tags = models.ManyToManyField('Tag', null=True, blank=True) + fav_threads = models.ManyToManyField(Post, related_name='+', null=True, + blank=True) + + def save_setting(self, name, value): + setting, created = Setting.objects.get_or_create(name=name, user=self) + setting.value = str(value) + setting.save() + + return setting + + def get_setting(self, name): + if Setting.objects.filter(name=name, user=self).exists(): + setting = Setting.objects.get(name=name, user=self) + setting_value = setting.value + else: + setting_value = None + + return setting_value + + def is_moderator(self): + return RANK_MODERATOR >= self.rank + + def get_sorted_fav_tags(self): + cache_key = self._get_tag_cache_key() + fav_tags = cache.get(cache_key) + if fav_tags: + return fav_tags + + tags = self.fav_tags.annotate(Count('threads')) \ + .filter(threads__count__gt=0).order_by('name') + + if tags: + cache.set(cache_key, tags, settings.CACHE_TIMEOUT) + + return tags + + def get_post_count(self): + return Post.objects.filter(user=self).count() + + def __unicode__(self): + return self.user_id + '(' + str(self.rank) + ')' + + def get_last_access_time(self): + posts = Post.objects.filter(user=self) + if posts.count() > 0: + return posts.latest('pub_time').pub_time + + def add_tag(self, tag): + self.fav_tags.add(tag) + cache.delete(self._get_tag_cache_key()) + + def remove_tag(self, tag): + self.fav_tags.remove(tag) + cache.delete(self._get_tag_cache_key()) + + def _get_tag_cache_key(self): + return self.user_id + '_tags' + + +class Setting(models.Model): + + class Meta: + app_label = 'boards' + + name = models.CharField(max_length=50) + value = models.CharField(max_length=50) + user = models.ForeignKey(User) + + +class Ban(models.Model): + + class Meta: + app_label = 'boards' + + ip = models.GenericIPAddressField() + reason = models.CharField(default=BAN_REASON_AUTO, + max_length=BAN_REASON_MAX_LENGTH) + can_read = models.BooleanField(default=True) + + def __unicode__(self): + return self.ip diff --git a/boards/views.py b/boards/views.py --- a/boards/views.py +++ b/boards/views.py @@ -20,7 +20,8 @@ from boards import utils from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm from boards.models import Post, Tag, Ban, User -from boards.models.post import RANK_USER, SETTING_MODERATE, REGEX_REPLY +from boards.models.post import SETTING_MODERATE, REGEX_REPLY +from boards.models.user import RANK_USER from boards import authors from boards.utils import get_client_ip import neboard