##// END OF EJS Templates
Connect replies only if this is a generic save, not some specific fields update
neko259 -
r1837:772d4b19 default
parent child Browse files
Show More
@@ -1,132 +1,135
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 if not kwargs['update_fields']:
26 for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()):
27 for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()):
27 post_id = reply_number.group(1)
28 post_id = reply_number.group(1)
28
29
29 try:
30 try:
30 referenced_post = Post.objects.get(id=post_id)
31 referenced_post = Post.objects.get(id=post_id)
31
32
32 if not referenced_post.referenced_posts.filter(
33 if not referenced_post.referenced_posts.filter(
33 id=instance.id).exists():
34 id=instance.id).exists():
34 referenced_post.referenced_posts.add(instance)
35 referenced_post.referenced_posts.add(instance)
35 referenced_post.last_edit_time = instance.pub_time
36 referenced_post.last_edit_time = instance.pub_time
36 referenced_post.build_refmap()
37 referenced_post.build_refmap()
37 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
38 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
38 except Post.DoesNotExist:
39 except Post.DoesNotExist:
39 pass
40 pass
40
41
41
42
42 @receiver(post_save, sender=Post)
43 @receiver(post_save, sender=Post)
43 @receiver(post_import_deps, sender=Post)
44 @receiver(post_import_deps, sender=Post)
44 def connect_global_replies(instance, **kwargs):
45 def connect_global_replies(instance, **kwargs):
46 if not kwargs['update_fields']:
45 for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()):
47 for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()):
46 key_type = reply_number.group(1)
48 key_type = reply_number.group(1)
47 key = reply_number.group(2)
49 key = reply_number.group(2)
48 local_id = reply_number.group(3)
50 local_id = reply_number.group(3)
49
51
50 try:
52 try:
51 global_id = GlobalId.objects.get(key_type=key_type, key=key,
53 global_id = GlobalId.objects.get(key_type=key_type, key=key,
52 local_id=local_id)
54 local_id=local_id)
53 referenced_post = Post.objects.get(global_id=global_id)
55 referenced_post = Post.objects.get(global_id=global_id)
54 referenced_post.referenced_posts.add(instance)
56 referenced_post.referenced_posts.add(instance)
55 referenced_post.last_edit_time = instance.pub_time
57 referenced_post.last_edit_time = instance.pub_time
56 referenced_post.build_refmap()
58 referenced_post.build_refmap()
57 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
59 referenced_post.save(update_fields=['refmap', 'last_edit_time'])
58 except (GlobalId.DoesNotExist, Post.DoesNotExist):
60 except (GlobalId.DoesNotExist, Post.DoesNotExist):
59 pass
61 pass
60
62
61
63
62 @receiver(post_save, sender=Post)
64 @receiver(post_save, sender=Post)
63 def connect_notifications(instance, **kwargs):
65 def connect_notifications(instance, **kwargs):
66 if not kwargs['update_fields']:
64 for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()):
67 for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()):
65 user_name = reply_number.group(1).lower()
68 user_name = reply_number.group(1).lower()
66 Notification.objects.get_or_create(name=user_name, post=instance)
69 Notification.objects.get_or_create(name=user_name, post=instance)
67
70
68
71
69 @receiver(pre_save, sender=Post)
72 @receiver(pre_save, sender=Post)
70 @receiver(post_import_deps, sender=Post)
73 @receiver(post_import_deps, sender=Post)
71 def parse_text(instance, **kwargs):
74 def parse_text(instance, **kwargs):
72 instance._text_rendered = get_parser().parse(instance.get_raw_text())
75 instance._text_rendered = get_parser().parse(instance.get_raw_text())
73
76
74
77
75 @receiver(pre_delete, sender=Post)
78 @receiver(pre_delete, sender=Post)
76 def delete_attachments(instance, **kwargs):
79 def delete_attachments(instance, **kwargs):
77 for attachment in instance.attachments.all():
80 for attachment in instance.attachments.all():
78 attachment_refs_count = attachment.attachment_posts.count()
81 attachment_refs_count = attachment.attachment_posts.count()
79 if attachment_refs_count == 1:
82 if attachment_refs_count == 1:
80 attachment.delete()
83 attachment.delete()
81
84
82
85
83 @receiver(post_delete, sender=Post)
86 @receiver(post_delete, sender=Post)
84 def update_thread_on_delete(instance, **kwargs):
87 def update_thread_on_delete(instance, **kwargs):
85 thread = instance.get_thread()
88 thread = instance.get_thread()
86 thread.last_edit_time = timezone.now()
89 thread.last_edit_time = timezone.now()
87 thread.save()
90 thread.save()
88
91
89
92
90 @receiver(post_delete, sender=Post)
93 @receiver(post_delete, sender=Post)
91 def delete_global_id(instance, **kwargs):
94 def delete_global_id(instance, **kwargs):
92 if instance.global_id and instance.global_id.id:
95 if instance.global_id and instance.global_id.id:
93 instance.global_id.delete()
96 instance.global_id.delete()
94
97
95
98
96 @receiver(post_save, sender=Attachment)
99 @receiver(post_save, sender=Attachment)
97 def generate_thumb(instance, **kwargs):
100 def generate_thumb(instance, **kwargs):
98 if instance.mimetype in FILE_TYPES_IMAGE:
101 if instance.mimetype in FILE_TYPES_IMAGE:
99 for size in THUMB_SIZES:
102 for size in THUMB_SIZES:
100 (w, h) = size
103 (w, h) = size
101 split = instance.file.name.rsplit('.', 1)
104 split = instance.file.name.rsplit('.', 1)
102 thumb_name = '%s.%sx%s.%s' % (split[0], w, h, split[1])
105 thumb_name = '%s.%sx%s.%s' % (split[0], w, h, split[1])
103
106
104 if not instance.file.storage.exists(thumb_name):
107 if not instance.file.storage.exists(thumb_name):
105 # you can use another thumbnailing function if you like
108 # you can use another thumbnailing function if you like
106 thumb_content = thumbs.generate_thumb(instance.file, size, split[1])
109 thumb_content = thumbs.generate_thumb(instance.file, size, split[1])
107
110
108 thumb_name_ = instance.file.storage.save(thumb_name, thumb_content)
111 thumb_name_ = instance.file.storage.save(thumb_name, thumb_content)
109
112
110 if not thumb_name == thumb_name_:
113 if not thumb_name == thumb_name_:
111 raise ValueError(
114 raise ValueError(
112 'There is already a file named %s' % thumb_name_)
115 'There is already a file named %s' % thumb_name_)
113
116
114
117
115 @receiver(pre_delete, sender=Post)
118 @receiver(pre_delete, sender=Post)
116 def rebuild_refmap(instance, **kwargs):
119 def rebuild_refmap(instance, **kwargs):
117 for referenced_post in instance.refposts.all():
120 for referenced_post in instance.refposts.all():
118 referenced_post.build_refmap(excluded_ids=[instance.id])
121 referenced_post.build_refmap(excluded_ids=[instance.id])
119 referenced_post.save(update_fields=['refmap'])
122 referenced_post.save(update_fields=['refmap'])
120
123
121
124
122 @receiver(post_delete, sender=Attachment)
125 @receiver(post_delete, sender=Attachment)
123 def delete_file(instance, **kwargs):
126 def delete_file(instance, **kwargs):
124 if instance.is_internal():
127 if instance.is_internal():
125 file = MEDIA_ROOT + instance.file.name
128 file = MEDIA_ROOT + instance.file.name
126 os.remove(file)
129 os.remove(file)
127 if instance.mimetype in FILE_TYPES_IMAGE:
130 if instance.mimetype in FILE_TYPES_IMAGE:
128 for size in THUMB_SIZES:
131 for size in THUMB_SIZES:
129 file_name_parts = instance.file.name.split('.')
132 file_name_parts = instance.file.name.split('.')
130 thumb_file = MEDIA_ROOT + '{}.{}x{}.{}'.format(file_name_parts[0], size[0], size[1], file_name_parts[1])
133 thumb_file = MEDIA_ROOT + '{}.{}x{}.{}'.format(file_name_parts[0], size[0], size[1], file_name_parts[1])
131 os.remove(thumb_file)
134 os.remove(thumb_file)
132
135
General Comments 0
You need to be logged in to leave comments. Login now