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