##// END OF EJS Templates
Fixed signals
neko259 -
r1526:936c0c67 decentral
parent child Browse files
Show More
@@ -1,87 +1,87 b''
1 import re
1 import re
2 from boards.mdx_neboard import get_parser
2 from boards.mdx_neboard import get_parser
3
3
4 from boards.models import Post, GlobalId
4 from boards.models import Post, GlobalId
5 from boards.models.post import REGEX_NOTIFICATION
5 from boards.models.post import REGEX_NOTIFICATION
6 from boards.models.post import REGEX_REPLY, REGEX_GLOBAL_REPLY
6 from boards.models.post import REGEX_REPLY, REGEX_GLOBAL_REPLY
7 from boards.models.user import Notification
7 from boards.models.user import Notification
8 from django.db.models.signals import post_save, pre_save, pre_delete, \
8 from django.db.models.signals import post_save, pre_save, pre_delete, \
9 post_delete
9 post_delete
10 from django.dispatch import receiver
10 from django.dispatch import receiver
11 from django.utils import timezone
11 from django.utils import timezone
12
12
13
13
14 @receiver(post_save, sender=Post)
14 @receiver(post_save, sender=Post)
15 def connect_replies(instance, **kwargs):
15 def connect_replies(instance, **kwargs):
16 for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()):
16 for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()):
17 post_id = reply_number.group(1)
17 post_id = reply_number.group(1)
18
18
19 try:
19 try:
20 referenced_post = Post.objects.get(id=post_id)
20 referenced_post = Post.objects.get(id=post_id)
21
21
22 referenced_post.referenced_posts.add(instance)
22 referenced_post.referenced_posts.add(instance)
23 referenced_post.last_edit_time = instance.pub_time
23 referenced_post.last_edit_time = instance.pub_time
24 referenced_post.build_refmap()
24 referenced_post.build_refmap()
25 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
25 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
26 except Post.ObjectDoesNotExist:
26 except Post.ObjectDoesNotExist:
27 pass
27 pass
28
28
29
29
30 @receiver(post_save, sender=Post)
30 @receiver(post_save, sender=Post)
31 def connect_global_replies(instance, **kwargs):
31 def connect_global_replies(instance, **kwargs):
32 for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()):
32 for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()):
33 key_type = reply_number.group(1)
33 key_type = reply_number.group(1)
34 key = reply_number.group(2)
34 key = reply_number.group(2)
35 local_id = reply_number.group(3)
35 local_id = reply_number.group(3)
36
36
37 try:
37 try:
38 global_id = GlobalId.objects.get(key_type=key_type, key=key,
38 global_id = GlobalId.objects.get(key_type=key_type, key=key,
39 local_id=local_id)
39 local_id=local_id)
40 referenced_post = Post.objects.get(global_id=global_id)
40 referenced_post = Post.objects.get(global_id=global_id)
41 referenced_post.referenced_posts.add(instance)
41 referenced_post.referenced_posts.add(instance)
42 referenced_post.last_edit_time = instance.pub_time
42 referenced_post.last_edit_time = instance.pub_time
43 referenced_post.build_refmap()
43 referenced_post.build_refmap()
44 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
44 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
45 except (GlobalId.ObjectDoesNotExist, Post.ObjectDoesNotExist):
45 except (GlobalId.DoesNotExist, Post.DoesNotExist):
46 pass
46 pass
47
47
48
48
49 @receiver(post_save, sender=Post)
49 @receiver(post_save, sender=Post)
50 def connect_notifications(instance, **kwargs):
50 def connect_notifications(instance, **kwargs):
51 for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()):
51 for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()):
52 user_name = reply_number.group(1).lower()
52 user_name = reply_number.group(1).lower()
53 Notification.objects.get_or_create(name=user_name, post=instance)
53 Notification.objects.get_or_create(name=user_name, post=instance)
54
54
55
55
56 @receiver(pre_save, sender=Post)
56 @receiver(pre_save, sender=Post)
57 def preparse_text(instance, **kwargs):
57 def preparse_text(instance, **kwargs):
58 instance._text_rendered = get_parser().parse(instance.get_raw_text())
58 instance._text_rendered = get_parser().parse(instance.get_raw_text())
59
59
60
60
61 @receiver(pre_delete, sender=Post)
61 @receiver(pre_delete, sender=Post)
62 def delete_images(instance, **kwargs):
62 def delete_images(instance, **kwargs):
63 for image in instance.images.all():
63 for image in instance.images.all():
64 image_refs_count = image.post_images.count()
64 image_refs_count = image.post_images.count()
65 if image_refs_count == 1:
65 if image_refs_count == 1:
66 image.delete()
66 image.delete()
67
67
68
68
69 @receiver(pre_delete, sender=Post)
69 @receiver(pre_delete, sender=Post)
70 def delete_attachments(instance, **kwargs):
70 def delete_attachments(instance, **kwargs):
71 for attachment in instance.attachments.all():
71 for attachment in instance.attachments.all():
72 attachment_refs_count = attachment.attachment_posts.count()
72 attachment_refs_count = attachment.attachment_posts.count()
73 if attachment_refs_count == 1:
73 if attachment_refs_count == 1:
74 attachment.delete()
74 attachment.delete()
75
75
76
76
77 @receiver(post_delete, sender=Post)
77 @receiver(post_delete, sender=Post)
78 def update_thread_on_delete(instance, **kwargs):
78 def update_thread_on_delete(instance, **kwargs):
79 thread = instance.get_thread()
79 thread = instance.get_thread()
80 thread.last_edit_time = timezone.now()
80 thread.last_edit_time = timezone.now()
81 thread.save()
81 thread.save()
82
82
83
83
84 @receiver(post_delete, sender=Post)
84 @receiver(post_delete, sender=Post)
85 def delete_global_id(instance, **kwargs):
85 def delete_global_id(instance, **kwargs):
86 if instance.global_id and instance.global_id.id:
86 if instance.global_id and instance.global_id.id:
87 instance.global_id.delete()
87 instance.global_id.delete()
General Comments 0
You need to be logged in to leave comments. Login now