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