##// END OF EJS Templates
Define signals outside of the post model
neko259 -
r1512:2edb321f decentral
parent child Browse files
Show More
@@ -0,0 +1,11 b''
1 from django.apps import AppConfig
2
3
4 class BoardsAppConfig(AppConfig):
5 name = 'boards'
6 verbose_name = 'Boards'
7
8 def ready(self):
9 super().ready()
10
11 import boards.signals No newline at end of file
@@ -0,0 +1,62 b''
1 import re
2 from boards.mdx_neboard import get_parser
3
4 from boards.models import Post
5 from boards.models.post import REGEX_NOTIFICATION
6 from boards.models.post import REGEX_REPLY
7 from boards.models.user import Notification
8 from django.db.models.signals import post_save, pre_save, pre_delete, \
9 post_delete
10 from django.dispatch import receiver
11 from django.utils import timezone
12
13
14 @receiver(post_save, sender=Post)
15 def connect_replies(instance, **kwargs):
16 for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()):
17 post_id = reply_number.group(1)
18
19 try:
20 referenced_post = Post.objects.get(id=post_id)
21
22 referenced_post.referenced_posts.add(instance)
23 referenced_post.last_edit_time = instance.pub_time
24 referenced_post.build_refmap()
25 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
26 except Post.ObjectDoesNotExist:
27 pass
28
29
30 @receiver(post_save, sender=Post)
31 def connect_notifications(instance, **kwargs):
32 for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()):
33 user_name = reply_number.group(1).lower()
34 Notification.objects.get_or_create(name=user_name, post=instance)
35
36
37 @receiver(pre_save, sender=Post)
38 def preparse_text(instance, **kwargs):
39 instance._text_rendered = get_parser().parse(instance.get_raw_text())
40
41
42 @receiver(pre_delete, sender=Post)
43 def delete_images(instance, **kwargs):
44 for image in instance.images.all():
45 image_refs_count = image.post_images.count()
46 if image_refs_count == 1:
47 image.delete()
48
49
50 @receiver(pre_delete, sender=Post)
51 def delete_attachments(instance, **kwargs):
52 for attachment in instance.attachments.all():
53 attachment_refs_count = attachment.attachment_posts.count()
54 if attachment_refs_count == 1:
55 attachment.delete()
56
57
58 @receiver(post_delete, sender=Post)
59 def update_thread_on_delete(instance, **kwargs):
60 thread = instance.get_thread()
61 thread.last_edit_time = timezone.now()
62 thread.save()
@@ -0,0 +1,1 b''
1 default_app_config = 'boards.apps.BoardsAppConfig' No newline at end of file
@@ -399,55 +399,3 b' class Post(models.Model, Viewable):'
399
399
400 def set_hidden(self, hidden):
400 def set_hidden(self, hidden):
401 self.hidden = hidden
401 self.hidden = hidden
402
403
404 # SIGNALS (Maybe move to other module?)
405 @receiver(post_save, sender=Post)
406 def connect_replies(instance, **kwargs):
407 for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()):
408 post_id = reply_number.group(1)
409
410 try:
411 referenced_post = Post.objects.get(id=post_id)
412
413 referenced_post.referenced_posts.add(instance)
414 referenced_post.last_edit_time = instance.pub_time
415 referenced_post.build_refmap()
416 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
417 except ObjectDoesNotExist:
418 pass
419
420
421 @receiver(post_save, sender=Post)
422 def connect_notifications(instance, **kwargs):
423 for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()):
424 user_name = reply_number.group(1).lower()
425 Notification.objects.get_or_create(name=user_name, post=instance)
426
427
428 @receiver(pre_save, sender=Post)
429 def preparse_text(instance, **kwargs):
430 instance._text_rendered = get_parser().parse(instance.get_raw_text())
431
432
433 @receiver(pre_delete, sender=Post)
434 def delete_images(instance, **kwargs):
435 for image in instance.images.all():
436 image_refs_count = image.post_images.count()
437 if image_refs_count == 1:
438 image.delete()
439
440
441 @receiver(pre_delete, sender=Post)
442 def delete_attachments(instance, **kwargs):
443 for attachment in instance.attachments.all():
444 attachment_refs_count = attachment.attachment_posts.count()
445 if attachment_refs_count == 1:
446 attachment.delete()
447
448
449 @receiver(post_delete, sender=Post)
450 def update_thread_on_delete(instance, **kwargs):
451 thread = instance.get_thread()
452 thread.last_edit_time = timezone.now()
453 thread.save()
General Comments 0
You need to be logged in to leave comments. Login now