Show More
@@ -1,115 +1,132 b'' | |||||
1 | import re |
|
1 | import re | |
|
2 | import os | |||
|
3 | ||||
|
4 | from django.db.models.signals import post_save, pre_save, pre_delete, \ | |||
|
5 | post_delete | |||
|
6 | from django.dispatch import receiver | |||
|
7 | from django.utils import timezone | |||
|
8 | ||||
2 | from boards import thumbs |
|
9 | from boards import thumbs | |
3 | from boards.mdx_neboard import get_parser |
|
10 | from boards.mdx_neboard import get_parser | |
4 |
|
11 | |||
5 | from boards.models import Post, GlobalId, Attachment |
|
12 | from boards.models import Post, GlobalId, Attachment | |
6 | from boards.models.attachment.viewers import FILE_TYPES_IMAGE |
|
13 | from boards.models.attachment.viewers import FILE_TYPES_IMAGE | |
7 | from boards.models.post import REGEX_NOTIFICATION, REGEX_REPLY,\ |
|
14 | from boards.models.post import REGEX_NOTIFICATION, REGEX_REPLY,\ | |
8 | REGEX_GLOBAL_REPLY |
|
15 | REGEX_GLOBAL_REPLY | |
9 | from boards.models.post.manager import post_import_deps |
|
16 | from boards.models.post.manager import post_import_deps | |
10 | from boards.models.user import Notification |
|
17 | from boards.models.user import Notification | |
11 | from django.db.models.signals import post_save, pre_save, pre_delete, \ |
|
18 | from neboard.settings import MEDIA_ROOT | |
12 | post_delete |
|
|||
13 | from django.dispatch import receiver |
|
|||
14 | from django.utils import timezone |
|
|||
15 |
|
19 | |||
16 |
|
20 | |||
17 | THUMB_SIZES = ((200, 150),) |
|
21 | THUMB_SIZES = ((200, 150),) | |
18 |
|
22 | |||
19 |
|
23 | |||
20 | @receiver(post_save, sender=Post) |
|
24 | @receiver(post_save, sender=Post) | |
21 | def connect_replies(instance, **kwargs): |
|
25 | def connect_replies(instance, **kwargs): | |
22 | for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()): |
|
26 | for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()): | |
23 | post_id = reply_number.group(1) |
|
27 | post_id = reply_number.group(1) | |
24 |
|
28 | |||
25 | try: |
|
29 | try: | |
26 | referenced_post = Post.objects.get(id=post_id) |
|
30 | referenced_post = Post.objects.get(id=post_id) | |
27 |
|
31 | |||
28 | if not referenced_post.referenced_posts.filter( |
|
32 | if not referenced_post.referenced_posts.filter( | |
29 | id=instance.id).exists(): |
|
33 | id=instance.id).exists(): | |
30 | referenced_post.referenced_posts.add(instance) |
|
34 | referenced_post.referenced_posts.add(instance) | |
31 | referenced_post.last_edit_time = instance.pub_time |
|
35 | referenced_post.last_edit_time = instance.pub_time | |
32 | referenced_post.build_refmap() |
|
36 | referenced_post.build_refmap() | |
33 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
37 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) | |
34 | except Post.DoesNotExist: |
|
38 | except Post.DoesNotExist: | |
35 | pass |
|
39 | pass | |
36 |
|
40 | |||
37 |
|
41 | |||
38 | @receiver(post_save, sender=Post) |
|
42 | @receiver(post_save, sender=Post) | |
39 | @receiver(post_import_deps, sender=Post) |
|
43 | @receiver(post_import_deps, sender=Post) | |
40 | def connect_global_replies(instance, **kwargs): |
|
44 | def connect_global_replies(instance, **kwargs): | |
41 | for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()): |
|
45 | for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()): | |
42 | key_type = reply_number.group(1) |
|
46 | key_type = reply_number.group(1) | |
43 | key = reply_number.group(2) |
|
47 | key = reply_number.group(2) | |
44 | local_id = reply_number.group(3) |
|
48 | local_id = reply_number.group(3) | |
45 |
|
49 | |||
46 | try: |
|
50 | try: | |
47 | global_id = GlobalId.objects.get(key_type=key_type, key=key, |
|
51 | global_id = GlobalId.objects.get(key_type=key_type, key=key, | |
48 | local_id=local_id) |
|
52 | local_id=local_id) | |
49 | referenced_post = Post.objects.get(global_id=global_id) |
|
53 | referenced_post = Post.objects.get(global_id=global_id) | |
50 | referenced_post.referenced_posts.add(instance) |
|
54 | referenced_post.referenced_posts.add(instance) | |
51 | referenced_post.last_edit_time = instance.pub_time |
|
55 | referenced_post.last_edit_time = instance.pub_time | |
52 | referenced_post.build_refmap() |
|
56 | referenced_post.build_refmap() | |
53 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
57 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) | |
54 | except (GlobalId.DoesNotExist, Post.DoesNotExist): |
|
58 | except (GlobalId.DoesNotExist, Post.DoesNotExist): | |
55 | pass |
|
59 | pass | |
56 |
|
60 | |||
57 |
|
61 | |||
58 | @receiver(post_save, sender=Post) |
|
62 | @receiver(post_save, sender=Post) | |
59 | def connect_notifications(instance, **kwargs): |
|
63 | def connect_notifications(instance, **kwargs): | |
60 | for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()): |
|
64 | for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()): | |
61 | user_name = reply_number.group(1).lower() |
|
65 | user_name = reply_number.group(1).lower() | |
62 | Notification.objects.get_or_create(name=user_name, post=instance) |
|
66 | Notification.objects.get_or_create(name=user_name, post=instance) | |
63 |
|
67 | |||
64 |
|
68 | |||
65 | @receiver(pre_save, sender=Post) |
|
69 | @receiver(pre_save, sender=Post) | |
66 | @receiver(post_import_deps, sender=Post) |
|
70 | @receiver(post_import_deps, sender=Post) | |
67 | def parse_text(instance, **kwargs): |
|
71 | def parse_text(instance, **kwargs): | |
68 | instance._text_rendered = get_parser().parse(instance.get_raw_text()) |
|
72 | instance._text_rendered = get_parser().parse(instance.get_raw_text()) | |
69 |
|
73 | |||
70 |
|
74 | |||
71 | @receiver(pre_delete, sender=Post) |
|
75 | @receiver(pre_delete, sender=Post) | |
72 | def delete_attachments(instance, **kwargs): |
|
76 | def delete_attachments(instance, **kwargs): | |
73 | for attachment in instance.attachments.all(): |
|
77 | for attachment in instance.attachments.all(): | |
74 | attachment_refs_count = attachment.attachment_posts.count() |
|
78 | attachment_refs_count = attachment.attachment_posts.count() | |
75 | if attachment_refs_count == 1: |
|
79 | if attachment_refs_count == 1: | |
76 | attachment.delete() |
|
80 | attachment.delete() | |
77 |
|
81 | |||
78 |
|
82 | |||
79 | @receiver(post_delete, sender=Post) |
|
83 | @receiver(post_delete, sender=Post) | |
80 | def update_thread_on_delete(instance, **kwargs): |
|
84 | def update_thread_on_delete(instance, **kwargs): | |
81 | thread = instance.get_thread() |
|
85 | thread = instance.get_thread() | |
82 | thread.last_edit_time = timezone.now() |
|
86 | thread.last_edit_time = timezone.now() | |
83 | thread.save() |
|
87 | thread.save() | |
84 |
|
88 | |||
85 |
|
89 | |||
86 | @receiver(post_delete, sender=Post) |
|
90 | @receiver(post_delete, sender=Post) | |
87 | def delete_global_id(instance, **kwargs): |
|
91 | def delete_global_id(instance, **kwargs): | |
88 | if instance.global_id and instance.global_id.id: |
|
92 | if instance.global_id and instance.global_id.id: | |
89 | instance.global_id.delete() |
|
93 | instance.global_id.delete() | |
90 |
|
94 | |||
91 |
|
95 | |||
92 | @receiver(post_save, sender=Attachment) |
|
96 | @receiver(post_save, sender=Attachment) | |
93 | def generate_thumb(instance, **kwargs): |
|
97 | def generate_thumb(instance, **kwargs): | |
94 | if instance.mimetype in FILE_TYPES_IMAGE: |
|
98 | if instance.mimetype in FILE_TYPES_IMAGE: | |
95 | for size in THUMB_SIZES: |
|
99 | for size in THUMB_SIZES: | |
96 | (w, h) = size |
|
100 | (w, h) = size | |
97 | split = instance.file.name.rsplit('.', 1) |
|
101 | split = instance.file.name.rsplit('.', 1) | |
98 | thumb_name = '%s.%sx%s.%s' % (split[0], w, h, split[1]) |
|
102 | thumb_name = '%s.%sx%s.%s' % (split[0], w, h, split[1]) | |
99 |
|
103 | |||
100 | if not instance.file.storage.exists(thumb_name): |
|
104 | if not instance.file.storage.exists(thumb_name): | |
101 | # you can use another thumbnailing function if you like |
|
105 | # you can use another thumbnailing function if you like | |
102 | thumb_content = thumbs.generate_thumb(instance.file, size, split[1]) |
|
106 | thumb_content = thumbs.generate_thumb(instance.file, size, split[1]) | |
103 |
|
107 | |||
104 | thumb_name_ = instance.file.storage.save(thumb_name, thumb_content) |
|
108 | thumb_name_ = instance.file.storage.save(thumb_name, thumb_content) | |
105 |
|
109 | |||
106 | if not thumb_name == thumb_name_: |
|
110 | if not thumb_name == thumb_name_: | |
107 | raise ValueError( |
|
111 | raise ValueError( | |
108 | 'There is already a file named %s' % thumb_name_) |
|
112 | 'There is already a file named %s' % thumb_name_) | |
109 |
|
113 | |||
110 |
|
114 | |||
111 | @receiver(pre_delete, sender=Post) |
|
115 | @receiver(pre_delete, sender=Post) | |
112 | def rebuild_refmap(instance, **kwargs): |
|
116 | def rebuild_refmap(instance, **kwargs): | |
113 | for referenced_post in instance.refposts.all(): |
|
117 | for referenced_post in instance.refposts.all(): | |
114 | referenced_post.build_refmap(excluded_ids=[instance.id]) |
|
118 | referenced_post.build_refmap(excluded_ids=[instance.id]) | |
115 | referenced_post.save(update_fields=['refmap']) |
|
119 | referenced_post.save(update_fields=['refmap']) | |
|
120 | ||||
|
121 | ||||
|
122 | @receiver(post_delete, sender=Attachment) | |||
|
123 | def delete_file(instance, **kwargs): | |||
|
124 | if instance.is_internal(): | |||
|
125 | file = MEDIA_ROOT + instance.file.name | |||
|
126 | os.remove(file) | |||
|
127 | if instance.mimetype in FILE_TYPES_IMAGE: | |||
|
128 | for size in THUMB_SIZES: | |||
|
129 | file_name_parts = instance.file.name.split('.') | |||
|
130 | thumb_file = MEDIA_ROOT + '{}.{}x{}.{}'.format(file_name_parts[0], *size, file_name_parts[1]) | |||
|
131 | os.remove(thumb_file) | |||
|
132 |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now