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