thread.py
237 lines
| 6.9 KiB
| text/x-python
|
PythonLexer
|
r691 | import logging | ||
|
r1088 | from adjacent import Client | ||
|
r957 | |||
|
r891 | from django.db.models import Count, Sum | ||
|
r691 | from django.utils import timezone | ||
from django.db import models | ||||
|
r957 | |||
|
r716 | from boards import settings | ||
|
r1027 | import boards | ||
|
r1106 | from boards.utils import cached_result, datetime_to_epoch | ||
|
r958 | from boards.models.post import Post | ||
|
r1083 | from boards.models.tag import Tag | ||
|
r957 | |||
|
r691 | |||
__author__ = 'neko259' | ||||
logger = logging.getLogger(__name__) | ||||
|
r1088 | WS_NOTIFICATION_TYPE_NEW_POST = 'new_post' | ||
WS_NOTIFICATION_TYPE = 'notification_type' | ||||
WS_CHANNEL_THREAD = "thread:" | ||||
|
r715 | class ThreadManager(models.Manager): | ||
|
r716 | def process_oldest_threads(self): | ||
|
r715 | """ | ||
Preserves maximum thread count. If there are too many threads, | ||||
|
r716 | archive or delete the old ones. | ||
|
r715 | """ | ||
threads = Thread.objects.filter(archived=False).order_by('-bump_time') | ||||
thread_count = threads.count() | ||||
if thread_count > settings.MAX_THREAD_COUNT: | ||||
num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT | ||||
old_threads = threads[thread_count - num_threads_to_delete:] | ||||
for thread in old_threads: | ||||
|
r716 | if settings.ARCHIVE_THREADS: | ||
self._archive_thread(thread) | ||||
else: | ||||
|
r718 | thread.delete() | ||
|
r716 | |||
logger.info('Processed %d old threads' % num_threads_to_delete) | ||||
|
r715 | |||
|
r716 | def _archive_thread(self, thread): | ||
thread.archived = True | ||||
|
r863 | thread.bumpable = False | ||
|
r716 | thread.last_edit_time = timezone.now() | ||
|
r1029 | thread.update_posts_time() | ||
|
r863 | thread.save(update_fields=['archived', 'last_edit_time', 'bumpable']) | ||
|
r716 | |||
|
r715 | |||
|
r1136 | def get_thread_max_posts(): | ||
return settings.MAX_POSTS_PER_THREAD | ||||
|
r691 | class Thread(models.Model): | ||
|
r715 | objects = ThreadManager() | ||
|
r691 | |||
class Meta: | ||||
app_label = 'boards' | ||||
tags = models.ManyToManyField('Tag') | ||||
|
r966 | bump_time = models.DateTimeField(db_index=True) | ||
|
r691 | last_edit_time = models.DateTimeField() | ||
archived = models.BooleanField(default=False) | ||||
|
r863 | bumpable = models.BooleanField(default=True) | ||
|
r1136 | max_posts = models.IntegerField(default=get_thread_max_posts) | ||
|
r691 | |||
|
r1083 | def get_tags(self) -> list: | ||
|
r691 | """ | ||
Gets a sorted tag list. | ||||
""" | ||||
return self.tags.order_by('name') | ||||
def bump(self): | ||||
""" | ||||
Bumps (moves to up) thread if possible. | ||||
""" | ||||
if self.can_bump(): | ||||
|
r1029 | self.bump_time = self.last_edit_time | ||
|
r691 | |||
|
r1046 | self.update_bump_status() | ||
|
r885 | |||
|
r691 | logger.info('Bumped thread %d' % self.id) | ||
|
r1083 | def has_post_limit(self) -> bool: | ||
|
r1055 | return self.max_posts > 0 | ||
|
r1134 | def update_bump_status(self, exclude_posts=None): | ||
|
r1055 | if self.has_post_limit() and self.get_reply_count() >= self.max_posts: | ||
|
r1046 | self.bumpable = False | ||
|
r1134 | self.update_posts_time(exclude_posts=exclude_posts) | ||
|
r1046 | |||
|
r1106 | def _get_cache_key(self): | ||
return [datetime_to_epoch(self.last_edit_time)] | ||||
@cached_result(key_method=_get_cache_key) | ||||
|
r1083 | def get_reply_count(self) -> int: | ||
|
r958 | return self.get_replies().count() | ||
|
r691 | |||
|
r1106 | @cached_result(key_method=_get_cache_key) | ||
|
r1083 | def get_images_count(self) -> int: | ||
|
r958 | return self.get_replies().annotate(images_count=Count( | ||
|
r1091 | 'images')).aggregate(Sum('images_count'))['images_count__sum'] | ||
|
r691 | |||
|
r1083 | def can_bump(self) -> bool: | ||
|
r691 | """ | ||
Checks if the thread can be bumped by replying to it. | ||||
""" | ||||
|
r1026 | return self.bumpable and not self.archived | ||
|
r691 | |||
|
r1083 | def get_last_replies(self) -> list: | ||
|
r691 | """ | ||
Gets several last replies, not including opening post | ||||
""" | ||||
if settings.LAST_REPLIES_COUNT > 0: | ||||
reply_count = self.get_reply_count() | ||||
if reply_count > 0: | ||||
reply_count_to_show = min(settings.LAST_REPLIES_COUNT, | ||||
reply_count - 1) | ||||
|
r694 | replies = self.get_replies() | ||
|
r987 | last_replies = replies[reply_count - reply_count_to_show:] | ||
|
r691 | |||
return last_replies | ||||
|
r1083 | def get_skipped_replies_count(self) -> int: | ||
|
r691 | """ | ||
Gets number of posts between opening post and last replies. | ||||
""" | ||||
reply_count = self.get_reply_count() | ||||
last_replies_count = min(settings.LAST_REPLIES_COUNT, | ||||
reply_count - 1) | ||||
return reply_count - last_replies_count - 1 | ||||
|
r1083 | def get_replies(self, view_fields_only=False) -> list: | ||
|
r691 | """ | ||
Gets sorted thread posts | ||||
""" | ||||
|
r979 | query = Post.objects.filter(threads__in=[self]) | ||
|
r984 | query = query.order_by('pub_time').prefetch_related('images', 'thread', 'threads') | ||
|
r691 | if view_fields_only: | ||
|
r1079 | query = query.defer('poster_ip') | ||
|
r691 | return query.all() | ||
|
r1083 | def get_replies_with_images(self, view_fields_only=False) -> list: | ||
|
r950 | """ | ||
Gets replies that have at least one image attached | ||||
""" | ||||
|
r693 | return self.get_replies(view_fields_only).annotate(images_count=Count( | ||
'images')).filter(images_count__gt=0) | ||||
|
r1106 | # TODO Do we still need this? | ||
|
r1083 | def add_tag(self, tag: Tag): | ||
|
r691 | """ | ||
Connects thread to a tag and tag to a thread | ||||
""" | ||||
self.tags.add(tag) | ||||
|
r1083 | def get_opening_post(self, only_id=False) -> Post: | ||
|
r691 | """ | ||
Gets the first post of the thread | ||||
""" | ||||
|
r958 | query = self.get_replies().order_by('pub_time') | ||
|
r949 | if only_id: | ||
query = query.only('id') | ||||
opening_post = query.first() | ||||
return opening_post | ||||
|
r691 | |||
|
r1106 | @cached_result() | ||
|
r1083 | def get_opening_post_id(self) -> int: | ||
|
r691 | """ | ||
Gets ID of the first thread post. | ||||
""" | ||||
|
r957 | return self.get_opening_post(only_id=True).id | ||
|
r691 | |||
def get_pub_time(self): | ||||
""" | ||||
Gets opening post's pub time because thread does not have its own one. | ||||
""" | ||||
|
r949 | return self.get_opening_post().pub_time | ||
|
r718 | |||
def delete(self, using=None): | ||||
|
r950 | """ | ||
Deletes thread with all replies. | ||||
""" | ||||
|
r959 | for reply in self.get_replies().all(): | ||
|
r950 | reply.delete() | ||
|
r718 | |||
|
r863 | super(Thread, self).delete(using) | ||
|
r875 | |||
def __str__(self): | ||||
|
r950 | return 'T#{}/{}'.format(self.id, self.get_opening_post_id()) | ||
|
r1027 | |||
|
r1083 | def get_tag_url_list(self) -> list: | ||
|
r1029 | return boards.models.Tag.objects.get_tag_url_list(self.get_tags()) | ||
|
r1134 | def update_posts_time(self, exclude_posts=None): | ||
|
r1046 | for post in self.post_set.all(): | ||
|
r1134 | if exclude_posts is not None and post not in exclude_posts: | ||
# Manual update is required because uids are generated on save | ||||
post.last_edit_time = self.last_edit_time | ||||
post.save(update_fields=['last_edit_time']) | ||||
|
r1120 | |||
|
r1134 | post.threads.update(last_edit_time=self.last_edit_time) | ||
|
r1088 | |||
|
r1091 | def notify_clients(self): | ||
|
r1088 | if not settings.WEBSOCKETS_ENABLED: | ||
return | ||||
client = Client() | ||||
channel_name = WS_CHANNEL_THREAD + str(self.get_opening_post_id()) | ||||
client.publish(channel_name, { | ||||
WS_NOTIFICATION_TYPE: WS_NOTIFICATION_TYPE_NEW_POST, | ||||
}) | ||||
|
r1106 | client.send() | ||
|
r1149 | |||
def get_absolute_url(self): | ||||
return self.get_opening_post().get_absolute_url() | ||||