# HG changeset patch # User neko259 # Date 2015-07-12 11:42:00 # Node ID 730ce4d205c7202ac953c016c221112d71822c0a # Parent 20bc0916007b7fc4f821f5316ed6d2f74e160077 Split post module into post and manager diff --git a/boards/management/commands/enforce_privacy.py b/boards/management/commands/enforce_privacy.py --- a/boards/management/commands/enforce_privacy.py +++ b/boards/management/commands/enforce_privacy.py @@ -2,8 +2,7 @@ from django.core.management import BaseC from django.db import transaction from boards.models import Post -from boards.models.post import NO_IP - +from boards.models.post.manager import NO_IP __author__ = 'neko259' diff --git a/boards/models/post/__init__.py b/boards/models/post/__init__.py --- a/boards/models/post/__init__.py +++ b/boards/models/post/__init__.py @@ -1,14 +1,14 @@ -from datetime import datetime, timedelta, date -from datetime import time as dtime import logging import re import uuid from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse -from django.db import models, transaction +from django.db import models from django.db.models import TextField, QuerySet + from django.template.loader import render_to_string + from django.utils import timezone from boards.mdx_neboard import Parser @@ -16,10 +16,9 @@ from boards.models import KeyPair, Globa from boards import settings from boards.models import PostImage from boards.models.base import Viewable -from boards import utils from boards.models.post.export import get_exporter, DIFF_TYPE_JSON -from boards.models.user import Notification, Ban -import boards.models.thread +from boards.models.post.manager import PostManager +from boards.models.user import Notification WS_NOTIFICATION_TYPE_NEW_POST = 'new_post' WS_NOTIFICATION_TYPE = 'notification_type' @@ -28,16 +27,12 @@ WS_CHANNEL_THREAD = "thread:" APP_LABEL_BOARDS = 'boards' -POSTS_PER_DAY_RANGE = 7 - BAN_REASON_AUTO = 'Auto' IMAGE_THUMB_SIZE = (200, 150) TITLE_MAX_LENGTH = 200 -NO_IP = '0.0.0.0' - REGEX_REPLY = re.compile(r'\[post\](\d+)\[/post\]') REGEX_GLOBAL_REPLY = re.compile(r'\[post\](\w+)::([^:]+)::(\d+)\[/post\]') REGEX_URL = re.compile(r'https?\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?') @@ -69,116 +64,6 @@ POST_VIEW_PARAMS = ( REFMAP_STR = '>>{}' -class PostManager(models.Manager): - @transaction.atomic - def create_post(self, title: str, text: str, image=None, thread=None, - ip=NO_IP, tags: list=None, opening_posts: list=None): - """ - Creates new post - """ - - is_banned = Ban.objects.filter(ip=ip).exists() - - # TODO Raise specific exception and catch it in the views - if is_banned: - raise Exception("This user is banned") - - if not tags: - tags = [] - if not opening_posts: - opening_posts = [] - - posting_time = timezone.now() - if not thread: - thread = boards.models.thread.Thread.objects.create( - bump_time=posting_time, last_edit_time=posting_time) - list(map(thread.tags.add, tags)) - boards.models.thread.Thread.objects.process_oldest_threads() - else: - thread.last_edit_time = posting_time - thread.bump() - thread.save() - - pre_text = Parser().preparse(text) - - post = self.create(title=title, - text=pre_text, - pub_time=posting_time, - poster_ip=ip, - thread=thread, - last_edit_time=posting_time) - post.threads.add(thread) - - post.set_global_id() - - logger = logging.getLogger('boards.post.create') - - logger.info('Created post {} by {}'.format(post, post.poster_ip)) - - if image: - post.images.add(PostImage.objects.create_with_hash(image)) - - post.build_url() - post.connect_replies() - post.connect_threads(opening_posts) - post.connect_notifications() - - return post - - @transaction.atomic - def import_post(self, title: str, text:str, pub_time: str, - opening_post=None, tags=list()): - if opening_post is None: - thread = boards.models.thread.Thread.objects.create( - bump_time=pub_time, last_edit_time=pub_time) - list(map(thread.tags.add, tags)) - else: - thread = opening_post.get_thread() - - post = Post.objects.create(title=title, text=text, - pub_time=pub_time, - poster_ip=NO_IP, - last_edit_time=pub_time, - thread_id=thread.id) - - post.build_url() - post.connect_replies() - post.connect_notifications() - - return post - - def delete_posts_by_ip(self, ip): - """ - Deletes all posts of the author with same IP - """ - - posts = self.filter(poster_ip=ip) - for post in posts: - post.delete() - - @utils.cached_result() - def get_posts_per_day(self) -> float: - """ - Gets average count of posts per day for the last 7 days - """ - - day_end = date.today() - day_start = day_end - timedelta(POSTS_PER_DAY_RANGE) - - day_time_start = timezone.make_aware(datetime.combine( - day_start, dtime()), timezone.get_current_timezone()) - day_time_end = timezone.make_aware(datetime.combine( - day_end, dtime()), timezone.get_current_timezone()) - - posts_per_period = float(self.filter( - pub_time__lte=day_time_end, - pub_time__gte=day_time_start).count()) - - ppd = posts_per_period / POSTS_PER_DAY_RANGE - - return ppd - - class Post(models.Model, Viewable): """A post is a message.""" diff --git a/boards/models/post/manager.py b/boards/models/post/manager.py new file mode 100644 --- /dev/null +++ b/boards/models/post/manager.py @@ -0,0 +1,125 @@ +from datetime import datetime, timedelta, date +from datetime import time as dtime +import logging +from django.db import models, transaction +from django.utils import timezone +from boards import utils +from boards.mdx_neboard import Parser +from boards.models import PostImage +import boards.models + +__author__ = 'vurdalak' + + +NO_IP = '0.0.0.0' +POSTS_PER_DAY_RANGE = 7 + + +class PostManager(models.Manager): + @transaction.atomic + def create_post(self, title: str, text: str, image=None, thread=None, + ip=NO_IP, tags: list=None, opening_posts: list=None): + """ + Creates new post + """ + + is_banned = boards.models.Ban.objects.filter(ip=ip).exists() + + # TODO Raise specific exception and catch it in the views + if is_banned: + raise Exception("This user is banned") + + if not tags: + tags = [] + if not opening_posts: + opening_posts = [] + + posting_time = timezone.now() + if not thread: + thread = boards.models.thread.Thread.objects.create( + bump_time=posting_time, last_edit_time=posting_time) + list(map(thread.tags.add, tags)) + boards.models.thread.Thread.objects.process_oldest_threads() + else: + thread.last_edit_time = posting_time + thread.bump() + thread.save() + + pre_text = Parser().preparse(text) + + post = self.create(title=title, + text=pre_text, + pub_time=posting_time, + poster_ip=ip, + thread=thread, + last_edit_time=posting_time) + post.threads.add(thread) + + post.set_global_id() + + logger = logging.getLogger('boards.post.create') + + logger.info('Created post {} by {}'.format(post, post.poster_ip)) + + if image: + post.images.add(PostImage.objects.create_with_hash(image)) + + post.build_url() + post.connect_replies() + post.connect_threads(opening_posts) + post.connect_notifications() + + return post + + @transaction.atomic + def import_post(self, title: str, text: str, pub_time: str, + opening_post=None, tags=list()): + if opening_post is None: + thread = boards.models.thread.Thread.objects.create( + bump_time=pub_time, last_edit_time=pub_time) + list(map(thread.tags.add, tags)) + else: + thread = opening_post.get_thread() + + post = self.create(title=title, text=text, + pub_time=pub_time, + poster_ip=NO_IP, + last_edit_time=pub_time, + thread_id=thread.id) + + post.build_url() + post.connect_replies() + post.connect_notifications() + + return post + + def delete_posts_by_ip(self, ip): + """ + Deletes all posts of the author with same IP + """ + + posts = self.filter(poster_ip=ip) + for post in posts: + post.delete() + + @utils.cached_result() + def get_posts_per_day(self) -> float: + """ + Gets average count of posts per day for the last 7 days + """ + + day_end = date.today() + day_start = day_end - timedelta(POSTS_PER_DAY_RANGE) + + day_time_start = timezone.make_aware(datetime.combine( + day_start, dtime()), timezone.get_current_timezone()) + day_time_end = timezone.make_aware(datetime.combine( + day_end, dtime()), timezone.get_current_timezone()) + + posts_per_period = float(self.filter( + pub_time__lte=day_time_end, + pub_time__gte=day_time_start).count()) + + ppd = posts_per_period / POSTS_PER_DAY_RANGE + + return ppd diff --git a/boards/models/user.py b/boards/models/user.py --- a/boards/models/user.py +++ b/boards/models/user.py @@ -1,6 +1,5 @@ from django.db import models - -import boards.models.post +import boards __author__ = 'neko259'