##// END OF EJS Templates
Added tag 4.0.0 for changeset 3da1a2d02072
Added tag 4.0.0 for changeset 3da1a2d02072

File last commit:

r1753:22f62124 default
r1778:85b563f9 default
Show More
manager.py
190 lines | 5.9 KiB | text/x-python | PythonLexer
neko259
Moved post manager in a separate module
r1359 import logging
from datetime import datetime, timedelta, date
from datetime import time as dtime
neko259
Moved exceptions to a separate module
r1602 from boards.abstracts.exceptions import BannedException, ArchiveException
neko259
Moved post manager in a separate module
r1359 from django.db import models, transaction
from django.utils import timezone
neko259
Added support for partial import: when the reply is imported but the original post is not, the reply will be connected after the original post is ready
r1588 from django.dispatch import Signal
neko259
Moved post manager in a separate module
r1359
import boards
from boards.models.user import Ban
from boards.mdx_neboard import Parser
neko259
Store images as regular attachments instead of separate model
r1590 from boards.models import Attachment
neko259
Moved post manager in a separate module
r1359 from boards import utils
__author__ = 'neko259'
POSTS_PER_DAY_RANGE = 7
NO_IP = '0.0.0.0'
neko259
Added support for partial import: when the reply is imported but the original post is not, the reply will be connected after the original post is ready
r1588 post_import_deps = Signal()
neko259
Moved post manager in a separate module
r1359 class PostManager(models.Manager):
@transaction.atomic
neko259
Add any number of files and URLs to post, not just one
r1753 def create_post(self, title: str, text: str, files=[], thread=None,
neko259
Removed multitread posts 'feature'
r1704 ip=NO_IP, tags: list=None,
neko259
Load URL if the file could not be loaded
r1660 tripcode='', monochrome=False, images=[],
neko259
Add any number of files and URLs to post, not just one
r1753 file_urls=[]):
neko259
Moved post manager in a separate module
r1359 """
Creates new post
"""
neko259
Prevent posting into an archived thread
r1449 if thread is not None and thread.is_archived():
neko259
Moved exceptions to a separate module
r1602 raise ArchiveException('Cannot post into an archived thread')
neko259
Prevent posting into an archived thread
r1449
neko259
Added anonymous mode in which the board does not save poster IP addresses
r1362 if not utils.is_anonymous_mode():
is_banned = Ban.objects.filter(ip=ip).exists()
neko259
Added ability to create monochrome threads
r1434 else:
is_banned = False
neko259
Moved post manager in a separate module
r1359
if is_banned:
neko259
Moved exceptions to a separate module
r1602 raise BannedException("This user is banned")
neko259
Moved post manager in a separate module
r1359
if not tags:
tags = []
posting_time = timezone.now()
new_thread = False
if not thread:
thread = boards.models.thread.Thread.objects.create(
neko259
Added ability to create monochrome threads
r1434 bump_time=posting_time, last_edit_time=posting_time,
neko259
Removed multitread posts 'feature'
r1704 monochrome=monochrome)
neko259
Moved post manager in a separate module
r1359 list(map(thread.tags.add, tags))
new_thread = True
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,
tripcode=tripcode,
opening=new_thread)
logger = logging.getLogger('boards.post.create')
neko259
Cosmetic changes to post creation log
r1380 logger.info('Created post [{}] with text [{}] by {}'.format(post,
neko259
Deduplicated upload_to method for images and file attachments
r1368 post.get_text(),post.poster_ip))
neko259
Moved post manager in a separate module
r1359
neko259
Add any number of files and URLs to post, not just one
r1753 for file in files:
neko259
Download attached filed to the post during sync
r1511 self._add_file_to_post(file, post)
neko259
Added image aliases to upload the same images (like "fake" or "gtfo")
r1500 for image in images:
neko259
Fixed updating post
r1604 post.attachments.add(image)
neko259
Add any number of files and URLs to post, not just one
r1753 for file_url in file_urls:
neko259
Load URL if the file could not be loaded
r1660 post.attachments.add(Attachment.objects.create_from_url(file_url))
neko259
Moved post manager in a separate module
r1359
neko259
Merged with default
r1320 post.set_global_id()
neko259
Moved post manager in a separate module
r1359
# Thread needs to be bumped only when the post is already created
if not new_thread:
thread.last_edit_time = posting_time
thread.bump()
thread.save()
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
neko259
Messages statistics for day/week/month
r1618 def get_post_per_days(self, days) -> int:
day_end = date.today() + timedelta(1)
day_start = day_end - timedelta(days)
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())
return self.filter(
pub_time__lte=day_time_end,
pub_time__gte=day_time_start).count()
neko259
Merged with default
r1320 @transaction.atomic
def import_post(self, title: str, text: str, pub_time: str, global_id,
neko259
Parse tripcode for post sync
r1557 opening_post=None, tags=list(), files=list(),
neko259
Process updated posts from sync server
r1586 tripcode=None, version=1):
neko259
Merged with default branch
r1360 is_opening = opening_post is None
if is_opening:
neko259
Merged with default
r1320 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()
neko259
Process updated posts from sync server
r1586 post = self.create(title=title,
text=text,
neko259
Merged with default
r1320 pub_time=pub_time,
poster_ip=NO_IP,
last_edit_time=pub_time,
neko259
Merged with default branch
r1360 global_id=global_id,
neko259
Sync fixes
r1386 opening=is_opening,
neko259
Process updated posts from sync server
r1586 thread=thread,
tripcode=tripcode,
version=version)
neko259
Merged with default
r1320
neko259
Download attached filed to the post during sync
r1511 for file in files:
self._add_file_to_post(file, post)
neko259
Added support for partial import: when the reply is imported but the original post is not, the reply will be connected after the original post is ready
r1588 url_to_post = '[post]{}[/post]'.format(str(global_id))
replies = self.filter(text__contains=url_to_post)
for reply in replies:
neko259
Fix #3
r1613 post_import_deps.send(reply)
neko259
Added support for partial import: when the reply is imported but the original post is not, the reply will be connected after the original post is ready
r1588
neko259
Process updated posts from sync server
r1586 @transaction.atomic
def update_post(self, post, title: str, text: str, pub_time: str,
tags=list(), files=list(), tripcode=None, version=1):
post.title = title
post.text = text
post.pub_time = pub_time
post.tripcode = tripcode
post.version = version
post.save()
post.clear_cache()
post.attachments.clear()
for file in files:
self._add_file_to_post(file, post)
thread = post.get_thread()
thread.tags.clear()
list(map(thread.tags.add, tags))
neko259
Download attached filed to the post during sync
r1511 def _add_file_to_post(self, file, post):
neko259
Store images as regular attachments instead of separate model
r1590 post.attachments.add(Attachment.objects.create_with_hash(file))