Show More
@@ -1,410 +1,381 b'' | |||
|
1 | 1 | import uuid |
|
2 | 2 | |
|
3 | 3 | import re |
|
4 | 4 | from boards import settings |
|
5 | 5 | from boards.abstracts.tripcode import Tripcode |
|
6 | 6 | from boards.models import PostImage, Attachment, KeyPair, GlobalId |
|
7 | 7 | from boards.models.base import Viewable |
|
8 | 8 | from boards.models.post.export import get_exporter, DIFF_TYPE_JSON |
|
9 | 9 | from boards.models.post.manager import PostManager |
|
10 | 10 | from boards.utils import datetime_to_epoch |
|
11 | 11 | from django.core.exceptions import ObjectDoesNotExist |
|
12 | 12 | from django.core.urlresolvers import reverse |
|
13 | 13 | from django.db import models |
|
14 | 14 | from django.db.models import TextField, QuerySet |
|
15 | 15 | from django.template.defaultfilters import truncatewords, striptags |
|
16 | 16 | from django.template.loader import render_to_string |
|
17 | 17 | |
|
18 | 18 | CSS_CLS_HIDDEN_POST = 'hidden_post' |
|
19 | 19 | CSS_CLS_DEAD_POST = 'dead_post' |
|
20 | 20 | CSS_CLS_ARCHIVE_POST = 'archive_post' |
|
21 | 21 | CSS_CLS_POST = 'post' |
|
22 | 22 | CSS_CLS_MONOCHROME = 'monochrome' |
|
23 | 23 | |
|
24 | 24 | TITLE_MAX_WORDS = 10 |
|
25 | 25 | |
|
26 | 26 | APP_LABEL_BOARDS = 'boards' |
|
27 | 27 | |
|
28 | 28 | BAN_REASON_AUTO = 'Auto' |
|
29 | 29 | |
|
30 | 30 | IMAGE_THUMB_SIZE = (200, 150) |
|
31 | 31 | |
|
32 | 32 | TITLE_MAX_LENGTH = 200 |
|
33 | 33 | |
|
34 | 34 | REGEX_REPLY = re.compile(r'\[post\](\d+)\[/post\]') |
|
35 | 35 | REGEX_GLOBAL_REPLY = re.compile(r'\[post\](\w+)::([^:]+)::(\d+)\[/post\]') |
|
36 | 36 | REGEX_URL = re.compile(r'https?\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?') |
|
37 | 37 | REGEX_NOTIFICATION = re.compile(r'\[user\](\w+)\[/user\]') |
|
38 | 38 | |
|
39 | 39 | PARAMETER_TRUNCATED = 'truncated' |
|
40 | 40 | PARAMETER_TAG = 'tag' |
|
41 | 41 | PARAMETER_OFFSET = 'offset' |
|
42 | 42 | PARAMETER_DIFF_TYPE = 'type' |
|
43 | 43 | PARAMETER_CSS_CLASS = 'css_class' |
|
44 | 44 | PARAMETER_THREAD = 'thread' |
|
45 | 45 | PARAMETER_IS_OPENING = 'is_opening' |
|
46 | 46 | PARAMETER_POST = 'post' |
|
47 | 47 | PARAMETER_OP_ID = 'opening_post_id' |
|
48 | 48 | PARAMETER_NEED_OPEN_LINK = 'need_open_link' |
|
49 | 49 | PARAMETER_REPLY_LINK = 'reply_link' |
|
50 | 50 | PARAMETER_NEED_OP_DATA = 'need_op_data' |
|
51 | 51 | |
|
52 | 52 | POST_VIEW_PARAMS = ( |
|
53 | 53 | 'need_op_data', |
|
54 | 54 | 'reply_link', |
|
55 | 55 | 'need_open_link', |
|
56 | 56 | 'truncated', |
|
57 | 57 | 'mode_tree', |
|
58 | 58 | 'perms', |
|
59 | 59 | 'tree_depth', |
|
60 | 60 | ) |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | class Post(models.Model, Viewable): |
|
64 | 64 | """A post is a message.""" |
|
65 | 65 | |
|
66 | 66 | objects = PostManager() |
|
67 | 67 | |
|
68 | 68 | class Meta: |
|
69 | 69 | app_label = APP_LABEL_BOARDS |
|
70 | 70 | ordering = ('id',) |
|
71 | 71 | |
|
72 | 72 | title = models.CharField(max_length=TITLE_MAX_LENGTH, null=True, blank=True) |
|
73 | 73 | pub_time = models.DateTimeField() |
|
74 | 74 | text = TextField(blank=True, null=True) |
|
75 | 75 | _text_rendered = TextField(blank=True, null=True, editable=False) |
|
76 | 76 | |
|
77 | 77 | images = models.ManyToManyField(PostImage, null=True, blank=True, |
|
78 | 78 | related_name='post_images', db_index=True) |
|
79 | 79 | attachments = models.ManyToManyField(Attachment, null=True, blank=True, |
|
80 | 80 | related_name='attachment_posts') |
|
81 | 81 | |
|
82 | 82 | poster_ip = models.GenericIPAddressField() |
|
83 | 83 | |
|
84 | 84 | # TODO This field can be removed cause UID is used for update now |
|
85 | 85 | last_edit_time = models.DateTimeField() |
|
86 | 86 | |
|
87 | 87 | referenced_posts = models.ManyToManyField('Post', symmetrical=False, |
|
88 | 88 | null=True, |
|
89 | 89 | blank=True, related_name='refposts', |
|
90 | 90 | db_index=True) |
|
91 | 91 | refmap = models.TextField(null=True, blank=True) |
|
92 | 92 | threads = models.ManyToManyField('Thread', db_index=True, |
|
93 | 93 | related_name='multi_replies') |
|
94 | 94 | thread = models.ForeignKey('Thread', db_index=True, related_name='pt+') |
|
95 | 95 | |
|
96 | 96 | url = models.TextField() |
|
97 | 97 | uid = models.TextField(db_index=True) |
|
98 | 98 | |
|
99 | 99 | # Global ID with author key. If the message was downloaded from another |
|
100 | 100 | # server, this indicates the server. |
|
101 | 101 | global_id = models.OneToOneField(GlobalId, null=True, blank=True, |
|
102 | 102 | on_delete=models.CASCADE) |
|
103 | 103 | |
|
104 | 104 | tripcode = models.CharField(max_length=50, blank=True, default='') |
|
105 | 105 | opening = models.BooleanField(db_index=True) |
|
106 | 106 | hidden = models.BooleanField(default=False) |
|
107 | 107 | |
|
108 | 108 | def __str__(self): |
|
109 | 109 | return 'P#{}/{}'.format(self.id, self.get_title()) |
|
110 | 110 | |
|
111 | 111 | def get_title(self) -> str: |
|
112 | 112 | return self.title |
|
113 | 113 | |
|
114 | 114 | def get_title_or_text(self): |
|
115 | 115 | title = self.get_title() |
|
116 | 116 | if not title: |
|
117 | 117 | title = truncatewords(striptags(self.get_text()), TITLE_MAX_WORDS) |
|
118 | 118 | |
|
119 | 119 | return title |
|
120 | 120 | |
|
121 | 121 | def build_refmap(self) -> None: |
|
122 | 122 | """ |
|
123 | 123 | Builds a replies map string from replies list. This is a cache to stop |
|
124 | 124 | the server from recalculating the map on every post show. |
|
125 | 125 | """ |
|
126 | 126 | |
|
127 | 127 | post_urls = [refpost.get_link_view() |
|
128 | 128 | for refpost in self.referenced_posts.all()] |
|
129 | 129 | |
|
130 | 130 | self.refmap = ', '.join(post_urls) |
|
131 | 131 | |
|
132 | 132 | def is_referenced(self) -> bool: |
|
133 | 133 | return self.refmap and len(self.refmap) > 0 |
|
134 | 134 | |
|
135 | 135 | def is_opening(self) -> bool: |
|
136 | 136 | """ |
|
137 | 137 | Checks if this is an opening post or just a reply. |
|
138 | 138 | """ |
|
139 | 139 | |
|
140 | 140 | return self.opening |
|
141 | 141 | |
|
142 | 142 | def get_absolute_url(self, thread=None): |
|
143 | 143 | url = None |
|
144 | 144 | |
|
145 | 145 | if thread is None: |
|
146 | 146 | thread = self.get_thread() |
|
147 | 147 | |
|
148 | 148 | # Url is cached only for the "main" thread. When getting url |
|
149 | 149 | # for other threads, do it manually. |
|
150 | 150 | if self.url: |
|
151 | 151 | url = self.url |
|
152 | 152 | |
|
153 | 153 | if url is None: |
|
154 | 154 | opening = self.is_opening() |
|
155 | 155 | opening_id = self.id if opening else thread.get_opening_post_id() |
|
156 | 156 | url = reverse('thread', kwargs={'post_id': opening_id}) |
|
157 | 157 | if not opening: |
|
158 | 158 | url += '#' + str(self.id) |
|
159 | 159 | |
|
160 | 160 | return url |
|
161 | 161 | |
|
162 | 162 | def get_thread(self): |
|
163 | 163 | return self.thread |
|
164 | 164 | |
|
165 | 165 | def get_thread_id(self): |
|
166 | 166 | return self.thread_id |
|
167 | 167 | |
|
168 | 168 | def get_threads(self) -> QuerySet: |
|
169 | 169 | """ |
|
170 | 170 | Gets post's thread. |
|
171 | 171 | """ |
|
172 | 172 | |
|
173 | 173 | return self.threads |
|
174 | 174 | |
|
175 | 175 | def _get_cache_key(self): |
|
176 | 176 | return [datetime_to_epoch(self.last_edit_time)] |
|
177 | 177 | |
|
178 | 178 | def get_view(self, *args, **kwargs) -> str: |
|
179 | 179 | """ |
|
180 | 180 | Renders post's HTML view. Some of the post params can be passed over |
|
181 | 181 | kwargs for the means of caching (if we view the thread, some params |
|
182 | 182 | are same for every post and don't need to be computed over and over. |
|
183 | 183 | """ |
|
184 | 184 | |
|
185 | 185 | thread = self.get_thread() |
|
186 | 186 | |
|
187 | 187 | css_classes = [CSS_CLS_POST] |
|
188 | 188 | if thread.is_archived(): |
|
189 | 189 | css_classes.append(CSS_CLS_ARCHIVE_POST) |
|
190 | 190 | elif not thread.can_bump(): |
|
191 | 191 | css_classes.append(CSS_CLS_DEAD_POST) |
|
192 | 192 | if self.is_hidden(): |
|
193 | 193 | css_classes.append(CSS_CLS_HIDDEN_POST) |
|
194 | 194 | if thread.is_monochrome(): |
|
195 | 195 | css_classes.append(CSS_CLS_MONOCHROME) |
|
196 | 196 | |
|
197 | 197 | params = dict() |
|
198 | 198 | for param in POST_VIEW_PARAMS: |
|
199 | 199 | if param in kwargs: |
|
200 | 200 | params[param] = kwargs[param] |
|
201 | 201 | |
|
202 | 202 | params.update({ |
|
203 | 203 | PARAMETER_POST: self, |
|
204 | 204 | PARAMETER_IS_OPENING: self.is_opening(), |
|
205 | 205 | PARAMETER_THREAD: thread, |
|
206 | 206 | PARAMETER_CSS_CLASS: ' '.join(css_classes), |
|
207 | 207 | }) |
|
208 | 208 | |
|
209 | 209 | return render_to_string('boards/post.html', params) |
|
210 | 210 | |
|
211 | 211 | def get_search_view(self, *args, **kwargs): |
|
212 | 212 | return self.get_view(need_op_data=True, *args, **kwargs) |
|
213 | 213 | |
|
214 | 214 | def get_first_image(self) -> PostImage: |
|
215 | 215 | return self.images.earliest('id') |
|
216 | 216 | |
|
217 | 217 | def set_global_id(self, key_pair=None): |
|
218 | 218 | """ |
|
219 | 219 | Sets global id based on the given key pair. If no key pair is given, |
|
220 | 220 | default one is used. |
|
221 | 221 | """ |
|
222 | 222 | |
|
223 | 223 | if key_pair: |
|
224 | 224 | key = key_pair |
|
225 | 225 | else: |
|
226 | 226 | try: |
|
227 | 227 | key = KeyPair.objects.get(primary=True) |
|
228 | 228 | except KeyPair.DoesNotExist: |
|
229 | 229 | # Do not update the global id because there is no key defined |
|
230 | 230 | return |
|
231 | 231 | global_id = GlobalId(key_type=key.key_type, |
|
232 | 232 | key=key.public_key, |
|
233 | 233 | local_id=self.id) |
|
234 | 234 | global_id.save() |
|
235 | 235 | |
|
236 | 236 | self.global_id = global_id |
|
237 | 237 | |
|
238 | 238 | self.save(update_fields=['global_id']) |
|
239 | 239 | |
|
240 | 240 | def get_pub_time_str(self): |
|
241 | 241 | return str(self.pub_time) |
|
242 | 242 | |
|
243 | 243 | def get_replied_ids(self): |
|
244 | 244 | """ |
|
245 | 245 | Gets ID list of the posts that this post replies. |
|
246 | 246 | """ |
|
247 | 247 | |
|
248 | 248 | raw_text = self.get_raw_text() |
|
249 | 249 | |
|
250 | 250 | local_replied = REGEX_REPLY.findall(raw_text) |
|
251 | 251 | global_replied = [] |
|
252 | 252 | for match in REGEX_GLOBAL_REPLY.findall(raw_text): |
|
253 | 253 | key_type = match[0] |
|
254 | 254 | key = match[1] |
|
255 | 255 | local_id = match[2] |
|
256 | 256 | |
|
257 | 257 | try: |
|
258 | 258 | global_id = GlobalId.objects.get(key_type=key_type, |
|
259 | 259 | key=key, local_id=local_id) |
|
260 | 260 | for post in Post.objects.filter(global_id=global_id).only('id'): |
|
261 | 261 | global_replied.append(post.id) |
|
262 | 262 | except GlobalId.DoesNotExist: |
|
263 | 263 | pass |
|
264 | 264 | return local_replied + global_replied |
|
265 | 265 | |
|
266 | 266 | def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None, |
|
267 | 267 | include_last_update=False) -> str: |
|
268 | 268 | """ |
|
269 | 269 | Gets post HTML or JSON data that can be rendered on a page or used by |
|
270 | 270 | API. |
|
271 | 271 | """ |
|
272 | 272 | |
|
273 | 273 | return get_exporter(format_type).export(self, request, |
|
274 | 274 | include_last_update) |
|
275 | 275 | |
|
276 | 276 | def notify_clients(self, recursive=True): |
|
277 | 277 | """ |
|
278 | 278 | Sends post HTML data to the thread web socket. |
|
279 | 279 | """ |
|
280 | 280 | |
|
281 | 281 | if not settings.get_bool('External', 'WebsocketsEnabled'): |
|
282 | 282 | return |
|
283 | 283 | |
|
284 | 284 | thread_ids = list() |
|
285 | 285 | for thread in self.get_threads().all(): |
|
286 | 286 | thread_ids.append(thread.id) |
|
287 | 287 | |
|
288 | 288 | thread.notify_clients() |
|
289 | 289 | |
|
290 | 290 | if recursive: |
|
291 | 291 | for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()): |
|
292 | 292 | post_id = reply_number.group(1) |
|
293 | 293 | |
|
294 | 294 | try: |
|
295 | 295 | ref_post = Post.objects.get(id=post_id) |
|
296 | 296 | |
|
297 | 297 | if ref_post.get_threads().exclude(id__in=thread_ids).exists(): |
|
298 | 298 | # If post is in this thread, its thread was already notified. |
|
299 | 299 | # Otherwise, notify its thread separately. |
|
300 | 300 | ref_post.notify_clients(recursive=False) |
|
301 | 301 | except ObjectDoesNotExist: |
|
302 | 302 | pass |
|
303 | 303 | |
|
304 | 304 | def build_url(self): |
|
305 | 305 | self.url = self.get_absolute_url() |
|
306 | 306 | self.save(update_fields=['url']) |
|
307 | 307 | |
|
308 | 308 | def save(self, force_insert=False, force_update=False, using=None, |
|
309 | 309 | update_fields=None): |
|
310 | 310 | new_post = self.id is None |
|
311 | 311 | |
|
312 | 312 | self.uid = str(uuid.uuid4()) |
|
313 | 313 | if update_fields is not None and 'uid' not in update_fields: |
|
314 | 314 | update_fields += ['uid'] |
|
315 | 315 | |
|
316 | 316 | if not new_post: |
|
317 | 317 | for thread in self.get_threads().all(): |
|
318 | 318 | thread.last_edit_time = self.last_edit_time |
|
319 | 319 | |
|
320 | 320 | thread.save(update_fields=['last_edit_time', 'status']) |
|
321 | 321 | |
|
322 | 322 | super().save(force_insert, force_update, using, update_fields) |
|
323 | 323 | |
|
324 | 324 | if self.url is None: |
|
325 | 325 | self.build_url() |
|
326 | 326 | |
|
327 | 327 | def get_text(self) -> str: |
|
328 | 328 | return self._text_rendered |
|
329 | 329 | |
|
330 | 330 | def get_raw_text(self) -> str: |
|
331 | 331 | return self.text |
|
332 | 332 | |
|
333 | 333 | def get_sync_text(self) -> str: |
|
334 | 334 | """ |
|
335 | 335 | Returns text applicable for sync. It has absolute post reflinks. |
|
336 | 336 | """ |
|
337 | 337 | |
|
338 | 338 | replacements = dict() |
|
339 | 339 | for post_id in REGEX_REPLY.findall(self.get_raw_text()): |
|
340 | 340 | absolute_post_id = str(Post.objects.get(id=post_id).global_id) |
|
341 | 341 | replacements[post_id] = absolute_post_id |
|
342 | 342 | |
|
343 | 343 | text = self.get_raw_text() or '' |
|
344 | 344 | for key in replacements: |
|
345 | 345 | text = text.replace('[post]{}[/post]'.format(key), |
|
346 | 346 | '[post]{}[/post]'.format(replacements[key])) |
|
347 | 347 | text = text.replace('\r\n', '\n').replace('\r', '\n') |
|
348 | 348 | |
|
349 | 349 | return text |
|
350 | 350 | |
|
351 | 351 | def connect_threads(self, opening_posts): |
|
352 | 352 | for opening_post in opening_posts: |
|
353 | 353 | threads = opening_post.get_threads().all() |
|
354 | 354 | for thread in threads: |
|
355 | 355 | if thread.can_bump(): |
|
356 | 356 | thread.update_bump_status() |
|
357 | 357 | |
|
358 | 358 | thread.last_edit_time = self.last_edit_time |
|
359 | 359 | thread.save(update_fields=['last_edit_time', 'status']) |
|
360 | 360 | self.threads.add(opening_post.get_thread()) |
|
361 | 361 | |
|
362 | 362 | def get_tripcode(self): |
|
363 | 363 | if self.tripcode: |
|
364 | 364 | return Tripcode(self.tripcode) |
|
365 | 365 | |
|
366 | 366 | def get_link_view(self): |
|
367 | 367 | """ |
|
368 | 368 | Gets view of a reflink to the post. |
|
369 | 369 | """ |
|
370 | 370 | result = '<a href="{}">>>{}</a>'.format(self.get_absolute_url(), |
|
371 | 371 | self.id) |
|
372 | 372 | if self.is_opening(): |
|
373 | 373 | result = '<b>{}</b>'.format(result) |
|
374 | 374 | |
|
375 | 375 | return result |
|
376 | 376 | |
|
377 | 377 | def is_hidden(self) -> bool: |
|
378 | 378 | return self.hidden |
|
379 | 379 | |
|
380 | 380 | def set_hidden(self, hidden): |
|
381 | 381 | self.hidden = hidden |
|
382 | ||
|
383 | ||
|
384 | # SIGNALS (Maybe move to other module?) | |
|
385 | @receiver(post_save, sender=Post) | |
|
386 | def connect_replies(instance, **kwargs): | |
|
387 | for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()): | |
|
388 | post_id = reply_number.group(1) | |
|
389 | ||
|
390 | try: | |
|
391 | referenced_post = Post.objects.get(id=post_id) | |
|
392 | ||
|
393 | referenced_post.referenced_posts.add(instance) | |
|
394 | referenced_post.last_edit_time = instance.pub_time | |
|
395 | referenced_post.build_refmap() | |
|
396 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) | |
|
397 | except ObjectDoesNotExist: | |
|
398 | pass | |
|
399 | ||
|
400 | ||
|
401 | @receiver(post_save, sender=Post) | |
|
402 | def connect_notifications(instance, **kwargs): | |
|
403 | for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()): | |
|
404 | user_name = reply_number.group(1).lower() | |
|
405 | Notification.objects.get_or_create(name=user_name, post=instance) | |
|
406 | ||
|
407 | ||
|
408 | @receiver(pre_save, sender=Post) | |
|
409 | def preparse_text(instance, **kwargs): | |
|
410 | instance._text_rendered = get_parser().parse(instance.get_raw_text()) |
@@ -1,88 +1,89 b'' | |||
|
1 | 1 | import re |
|
2 | 2 | from boards.mdx_neboard import get_parser |
|
3 | 3 | |
|
4 | 4 | from boards.models import Post, GlobalId |
|
5 | 5 | from boards.models.post import REGEX_NOTIFICATION |
|
6 | 6 | from boards.models.post import REGEX_REPLY, REGEX_GLOBAL_REPLY |
|
7 | 7 | from boards.models.user import Notification |
|
8 | 8 | from django.db.models.signals import post_save, pre_save, pre_delete, \ |
|
9 | 9 | post_delete |
|
10 | 10 | from django.dispatch import receiver |
|
11 | 11 | from django.utils import timezone |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | @receiver(post_save, sender=Post) |
|
15 | 15 | def connect_replies(instance, **kwargs): |
|
16 | 16 | for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()): |
|
17 | 17 | post_id = reply_number.group(1) |
|
18 | 18 | |
|
19 | 19 | try: |
|
20 | 20 | referenced_post = Post.objects.get(id=post_id) |
|
21 | 21 | |
|
22 |
if not referenced_post.referenced_posts.filter( |
|
|
22 | if not referenced_post.referenced_posts.filter( | |
|
23 | id=instance.id).exists(): | |
|
23 | 24 | referenced_post.referenced_posts.add(instance) |
|
24 | 25 | referenced_post.last_edit_time = instance.pub_time |
|
25 | 26 | referenced_post.build_refmap() |
|
26 | 27 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
27 |
except Post. |
|
|
28 | except Post.DoesNotExist: | |
|
28 | 29 | pass |
|
29 | 30 | |
|
30 | 31 | |
|
31 | 32 | @receiver(post_save, sender=Post) |
|
32 | 33 | def connect_global_replies(instance, **kwargs): |
|
33 | 34 | for reply_number in re.finditer(REGEX_GLOBAL_REPLY, instance.get_raw_text()): |
|
34 | 35 | key_type = reply_number.group(1) |
|
35 | 36 | key = reply_number.group(2) |
|
36 | 37 | local_id = reply_number.group(3) |
|
37 | 38 | |
|
38 | 39 | try: |
|
39 | 40 | global_id = GlobalId.objects.get(key_type=key_type, key=key, |
|
40 | 41 | local_id=local_id) |
|
41 | 42 | referenced_post = Post.objects.get(global_id=global_id) |
|
42 | 43 | referenced_post.referenced_posts.add(instance) |
|
43 | 44 | referenced_post.last_edit_time = instance.pub_time |
|
44 | 45 | referenced_post.build_refmap() |
|
45 | 46 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
46 | 47 | except (GlobalId.DoesNotExist, Post.DoesNotExist): |
|
47 | 48 | pass |
|
48 | 49 | |
|
49 | 50 | |
|
50 | 51 | @receiver(post_save, sender=Post) |
|
51 | 52 | def connect_notifications(instance, **kwargs): |
|
52 | 53 | for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()): |
|
53 | 54 | user_name = reply_number.group(1).lower() |
|
54 | 55 | Notification.objects.get_or_create(name=user_name, post=instance) |
|
55 | 56 | |
|
56 | 57 | |
|
57 | 58 | @receiver(pre_save, sender=Post) |
|
58 | 59 | def preparse_text(instance, **kwargs): |
|
59 | 60 | instance._text_rendered = get_parser().parse(instance.get_raw_text()) |
|
60 | 61 | |
|
61 | 62 | |
|
62 | 63 | @receiver(pre_delete, sender=Post) |
|
63 | 64 | def delete_images(instance, **kwargs): |
|
64 | 65 | for image in instance.images.all(): |
|
65 | 66 | image_refs_count = image.post_images.count() |
|
66 | 67 | if image_refs_count == 1: |
|
67 | 68 | image.delete() |
|
68 | 69 | |
|
69 | 70 | |
|
70 | 71 | @receiver(pre_delete, sender=Post) |
|
71 | 72 | def delete_attachments(instance, **kwargs): |
|
72 | 73 | for attachment in instance.attachments.all(): |
|
73 | 74 | attachment_refs_count = attachment.attachment_posts.count() |
|
74 | 75 | if attachment_refs_count == 1: |
|
75 | 76 | attachment.delete() |
|
76 | 77 | |
|
77 | 78 | |
|
78 | 79 | @receiver(post_delete, sender=Post) |
|
79 | 80 | def update_thread_on_delete(instance, **kwargs): |
|
80 | 81 | thread = instance.get_thread() |
|
81 | 82 | thread.last_edit_time = timezone.now() |
|
82 | 83 | thread.save() |
|
83 | 84 | |
|
84 | 85 | |
|
85 | 86 | @receiver(post_delete, sender=Post) |
|
86 | 87 | def delete_global_id(instance, **kwargs): |
|
87 | 88 | if instance.global_id and instance.global_id.id: |
|
88 | 89 | instance.global_id.delete() |
General Comments 0
You need to be logged in to leave comments.
Login now