# HG changeset patch # User neko259 # Date 2016-04-20 14:04:11 # Node ID 62571a33209d50e273e1a286a9e4f72dd5905271 # Parent d75e94e073710c4bcf12dc9f84f6ba39ff34fed4 Use signals for pre- and post- post actions 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 @@ -9,6 +9,8 @@ from django.db.models import TextField, from django.template.defaultfilters import striptags, truncatewords from django.template.loader import render_to_string from django.utils import timezone +from django.db.models.signals import post_save, pre_save +from django.dispatch import receiver from boards import settings from boards.abstracts.tripcode import Tripcode @@ -283,8 +285,6 @@ class Post(models.Model, Viewable): update_fields=None): new_post = self.id is None - self._text_rendered = Parser().parse(self.get_raw_text()) - self.uid = str(uuid.uuid4()) if update_fields is not None and 'uid' not in update_fields: update_fields += ['uid'] @@ -300,9 +300,6 @@ class Post(models.Model, Viewable): if self.url is None: self.build_url() - self._connect_replies() - self._connect_notifications() - def get_text(self) -> str: return self._text_rendered @@ -320,29 +317,6 @@ class Post(models.Model, Viewable): else: return str(self.id) - def _connect_notifications(self): - for reply_number in re.finditer(REGEX_NOTIFICATION, self.get_raw_text()): - user_name = reply_number.group(1).lower() - Notification.objects.get_or_create(name=user_name, post=self) - - def _connect_replies(self): - """ - Connects replies to a post to show them as a reflink map - """ - - for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()): - post_id = reply_number.group(1) - - try: - referenced_post = Post.objects.get(id=post_id) - - referenced_post.referenced_posts.add(self) - referenced_post.last_edit_time = self.pub_time - referenced_post.build_refmap() - referenced_post.save(update_fields=['refmap', 'last_edit_time']) - except ObjectDoesNotExist: - pass - def connect_threads(self, opening_posts): for opening_post in opening_posts: threads = opening_post.get_threads().all() @@ -374,3 +348,32 @@ class Post(models.Model, Viewable): def set_hidden(self, hidden): self.hidden = hidden + + +# SIGNALS (Maybe move to other module?) +@receiver(post_save, sender=Post) +def connect_replies(instance, **kwargs): + for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()): + post_id = reply_number.group(1) + + try: + referenced_post = Post.objects.get(id=post_id) + + referenced_post.referenced_posts.add(instance) + referenced_post.last_edit_time = instance.pub_time + referenced_post.build_refmap() + referenced_post.save(update_fields=['refmap', 'last_edit_time']) + except ObjectDoesNotExist: + pass + + +@receiver(post_save, sender=Post) +def connect_notifications(instance, **kwargs): + for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()): + user_name = reply_number.group(1).lower() + Notification.objects.get_or_create(name=user_name, post=instance) + + +@receiver(pre_save, sender=Post) +def preparse_text(instance, **kwargs): + instance._text_rendered = Parser().parse(instance.get_raw_text()) diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -magic +python-magic pytube requests adjacent