Show More
@@ -9,6 +9,8 b' from django.db.models import TextField, ' | |||||
9 | from django.template.defaultfilters import striptags, truncatewords |
|
9 | from django.template.defaultfilters import striptags, truncatewords | |
10 | from django.template.loader import render_to_string |
|
10 | from django.template.loader import render_to_string | |
11 | from django.utils import timezone |
|
11 | from django.utils import timezone | |
|
12 | from django.db.models.signals import post_save, pre_save | |||
|
13 | from django.dispatch import receiver | |||
12 |
|
14 | |||
13 | from boards import settings |
|
15 | from boards import settings | |
14 | from boards.abstracts.tripcode import Tripcode |
|
16 | from boards.abstracts.tripcode import Tripcode | |
@@ -283,8 +285,6 b' class Post(models.Model, Viewable):' | |||||
283 | update_fields=None): |
|
285 | update_fields=None): | |
284 | new_post = self.id is None |
|
286 | new_post = self.id is None | |
285 |
|
287 | |||
286 | self._text_rendered = Parser().parse(self.get_raw_text()) |
|
|||
287 |
|
||||
288 | self.uid = str(uuid.uuid4()) |
|
288 | self.uid = str(uuid.uuid4()) | |
289 | if update_fields is not None and 'uid' not in update_fields: |
|
289 | if update_fields is not None and 'uid' not in update_fields: | |
290 | update_fields += ['uid'] |
|
290 | update_fields += ['uid'] | |
@@ -300,9 +300,6 b' class Post(models.Model, Viewable):' | |||||
300 | if self.url is None: |
|
300 | if self.url is None: | |
301 | self.build_url() |
|
301 | self.build_url() | |
302 |
|
302 | |||
303 | self._connect_replies() |
|
|||
304 | self._connect_notifications() |
|
|||
305 |
|
||||
306 | def get_text(self) -> str: |
|
303 | def get_text(self) -> str: | |
307 | return self._text_rendered |
|
304 | return self._text_rendered | |
308 |
|
305 | |||
@@ -320,29 +317,6 b' class Post(models.Model, Viewable):' | |||||
320 | else: |
|
317 | else: | |
321 | return str(self.id) |
|
318 | return str(self.id) | |
322 |
|
319 | |||
323 | def _connect_notifications(self): |
|
|||
324 | for reply_number in re.finditer(REGEX_NOTIFICATION, self.get_raw_text()): |
|
|||
325 | user_name = reply_number.group(1).lower() |
|
|||
326 | Notification.objects.get_or_create(name=user_name, post=self) |
|
|||
327 |
|
||||
328 | def _connect_replies(self): |
|
|||
329 | """ |
|
|||
330 | Connects replies to a post to show them as a reflink map |
|
|||
331 | """ |
|
|||
332 |
|
||||
333 | for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()): |
|
|||
334 | post_id = reply_number.group(1) |
|
|||
335 |
|
||||
336 | try: |
|
|||
337 | referenced_post = Post.objects.get(id=post_id) |
|
|||
338 |
|
||||
339 | referenced_post.referenced_posts.add(self) |
|
|||
340 | referenced_post.last_edit_time = self.pub_time |
|
|||
341 | referenced_post.build_refmap() |
|
|||
342 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
|||
343 | except ObjectDoesNotExist: |
|
|||
344 | pass |
|
|||
345 |
|
||||
346 | def connect_threads(self, opening_posts): |
|
320 | def connect_threads(self, opening_posts): | |
347 | for opening_post in opening_posts: |
|
321 | for opening_post in opening_posts: | |
348 | threads = opening_post.get_threads().all() |
|
322 | threads = opening_post.get_threads().all() | |
@@ -374,3 +348,32 b' class Post(models.Model, Viewable):' | |||||
374 |
|
348 | |||
375 | def set_hidden(self, hidden): |
|
349 | def set_hidden(self, hidden): | |
376 | self.hidden = hidden |
|
350 | self.hidden = hidden | |
|
351 | ||||
|
352 | ||||
|
353 | # SIGNALS (Maybe move to other module?) | |||
|
354 | @receiver(post_save, sender=Post) | |||
|
355 | def connect_replies(instance, **kwargs): | |||
|
356 | for reply_number in re.finditer(REGEX_REPLY, instance.get_raw_text()): | |||
|
357 | post_id = reply_number.group(1) | |||
|
358 | ||||
|
359 | try: | |||
|
360 | referenced_post = Post.objects.get(id=post_id) | |||
|
361 | ||||
|
362 | referenced_post.referenced_posts.add(instance) | |||
|
363 | referenced_post.last_edit_time = instance.pub_time | |||
|
364 | referenced_post.build_refmap() | |||
|
365 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) | |||
|
366 | except ObjectDoesNotExist: | |||
|
367 | pass | |||
|
368 | ||||
|
369 | ||||
|
370 | @receiver(post_save, sender=Post) | |||
|
371 | def connect_notifications(instance, **kwargs): | |||
|
372 | for reply_number in re.finditer(REGEX_NOTIFICATION, instance.get_raw_text()): | |||
|
373 | user_name = reply_number.group(1).lower() | |||
|
374 | Notification.objects.get_or_create(name=user_name, post=instance) | |||
|
375 | ||||
|
376 | ||||
|
377 | @receiver(pre_save, sender=Post) | |||
|
378 | def preparse_text(instance, **kwargs): | |||
|
379 | instance._text_rendered = Parser().parse(instance.get_raw_text()) |
General Comments 0
You need to be logged in to leave comments.
Login now