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