|
|
import re
|
|
|
from boards.mdx_neboard import get_parser
|
|
|
|
|
|
from boards.models import Post
|
|
|
from boards.models.post import REGEX_NOTIFICATION
|
|
|
from boards.models.post import REGEX_REPLY
|
|
|
from boards.models.user import Notification
|
|
|
from django.db.models.signals import post_save, pre_save, pre_delete, \
|
|
|
post_delete
|
|
|
from django.dispatch import receiver
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
|
@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 Post.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 = get_parser().parse(instance.get_raw_text())
|
|
|
|
|
|
|
|
|
@receiver(pre_delete, sender=Post)
|
|
|
def delete_images(instance, **kwargs):
|
|
|
for image in instance.images.all():
|
|
|
image_refs_count = image.post_images.count()
|
|
|
if image_refs_count == 1:
|
|
|
image.delete()
|
|
|
|
|
|
|
|
|
@receiver(pre_delete, sender=Post)
|
|
|
def delete_attachments(instance, **kwargs):
|
|
|
for attachment in instance.attachments.all():
|
|
|
attachment_refs_count = attachment.attachment_posts.count()
|
|
|
if attachment_refs_count == 1:
|
|
|
attachment.delete()
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Post)
|
|
|
def update_thread_on_delete(instance, **kwargs):
|
|
|
thread = instance.get_thread()
|
|
|
thread.last_edit_time = timezone.now()
|
|
|
thread.save()
|
|
|
|