Show More
@@ -1,62 +1,81 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 |
|
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 |
|
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): | |||
|
32 | for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()): | |||
|
33 | key_type = reply_number.group(1) | |||
|
34 | key = reply_number.group(2) | |||
|
35 | local_id = reply_number.group(3) | |||
|
36 | ||||
|
37 | try: | |||
|
38 | global_id = GlobalId.objects.get(key_type=key_type, key=key, | |||
|
39 | local_id=local_id) | |||
|
40 | referenced_post = Post.objects.get(global_id=global_id) | |||
|
41 | referenced_post.referenced_posts.add(instance) | |||
|
42 | referenced_post.last_edit_time = instance.pub_time | |||
|
43 | referenced_post.build_refmap() | |||
|
44 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) | |||
|
45 | except (GlobalId.ObjectDoesNotExist, Post.ObjectDoesNotExist): | |||
|
46 | pass | |||
|
47 | ||||
|
48 | ||||
|
49 | @receiver(post_save, sender=Post) | |||
31 | def connect_notifications(instance, **kwargs): |
|
50 | def connect_notifications(instance, **kwargs): | |
32 | 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()): | |
33 | user_name = reply_number.group(1).lower() |
|
52 | user_name = reply_number.group(1).lower() | |
34 | Notification.objects.get_or_create(name=user_name, post=instance) |
|
53 | Notification.objects.get_or_create(name=user_name, post=instance) | |
35 |
|
54 | |||
36 |
|
55 | |||
37 | @receiver(pre_save, sender=Post) |
|
56 | @receiver(pre_save, sender=Post) | |
38 | def preparse_text(instance, **kwargs): |
|
57 | def preparse_text(instance, **kwargs): | |
39 | instance._text_rendered = get_parser().parse(instance.get_raw_text()) |
|
58 | instance._text_rendered = get_parser().parse(instance.get_raw_text()) | |
40 |
|
59 | |||
41 |
|
60 | |||
42 | @receiver(pre_delete, sender=Post) |
|
61 | @receiver(pre_delete, sender=Post) | |
43 | def delete_images(instance, **kwargs): |
|
62 | def delete_images(instance, **kwargs): | |
44 | for image in instance.images.all(): |
|
63 | for image in instance.images.all(): | |
45 | image_refs_count = image.post_images.count() |
|
64 | image_refs_count = image.post_images.count() | |
46 | if image_refs_count == 1: |
|
65 | if image_refs_count == 1: | |
47 | image.delete() |
|
66 | image.delete() | |
48 |
|
67 | |||
49 |
|
68 | |||
50 | @receiver(pre_delete, sender=Post) |
|
69 | @receiver(pre_delete, sender=Post) | |
51 | def delete_attachments(instance, **kwargs): |
|
70 | def delete_attachments(instance, **kwargs): | |
52 | for attachment in instance.attachments.all(): |
|
71 | for attachment in instance.attachments.all(): | |
53 | attachment_refs_count = attachment.attachment_posts.count() |
|
72 | attachment_refs_count = attachment.attachment_posts.count() | |
54 | if attachment_refs_count == 1: |
|
73 | if attachment_refs_count == 1: | |
55 | attachment.delete() |
|
74 | attachment.delete() | |
56 |
|
75 | |||
57 |
|
76 | |||
58 | @receiver(post_delete, sender=Post) |
|
77 | @receiver(post_delete, sender=Post) | |
59 | def update_thread_on_delete(instance, **kwargs): |
|
78 | def update_thread_on_delete(instance, **kwargs): | |
60 | thread = instance.get_thread() |
|
79 | thread = instance.get_thread() | |
61 | thread.last_edit_time = timezone.now() |
|
80 | thread.last_edit_time = timezone.now() | |
62 | thread.save() |
|
81 | thread.save() |
General Comments 0
You need to be logged in to leave comments.
Login now