# HG changeset patch # User neko259 # Date 2015-03-28 09:23:23 # Node ID 3274ab5df6015a0220d9cd6c28cd6be42f76958f # Parent 0cd7f7d0a95818e1b743c2a236c02875849626c3 Refactoring diff --git a/boards/mdx_neboard.py b/boards/mdx_neboard.py --- a/boards/mdx_neboard.py +++ b/boards/mdx_neboard.py @@ -2,6 +2,7 @@ import re import bbcode +from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse import boards @@ -116,18 +117,19 @@ class CodePattern(TextFormatter): def render_reflink(tag_name, value, options, parent, context): - if not REFLINK_PATTERN.match(value): - return '>>%s' % value + result = '>>%s' % value - post_id = int(value) + if REFLINK_PATTERN.match(value): + post_id = int(value) - posts = boards.models.Post.objects.filter(id=post_id) - if posts.exists(): - post = posts[0] + try: + post = boards.models.Post.objects.get(id=post_id) - return '>>%s' % (post.get_url(), post_id) - else: - return '>>%s' % value + result = '>>%s' % (post.get_url(), post_id) + except ObjectDoesNotExist: + pass + + return result def render_multithread(tag_name, value, options, parent, context): @@ -136,12 +138,13 @@ def render_multithread(tag_name, value, if REFLINK_PATTERN.match(value): post_id = int(value) - posts = boards.models.Post.objects.filter(id=post_id) - if posts.exists(): - post = posts[0] + try: + post = boards.models.Post.objects.get(id=post_id) if post.is_opening(): result = '>>>%s' % (post.get_url(), post_id) + except ObjectDoesNotExist: + pass return result diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -6,6 +6,7 @@ import re from urllib.parse import unquote from adjacent import Client +from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse from django.db import models, transaction from django.db.models import TextField @@ -395,12 +396,16 @@ class Post(models.Model, Viewable): if recursive: for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()): post_id = reply_number.group(1) - ref_post = Post.objects.filter(id=post_id)[0] + + try: + ref_post = Post.objects.get(id=post_id) - # If post is in this thread, its thread was already notified. - # Otherwise, notify its thread separately. - if ref_post.get_thread().id != thread_id: - ref_post.send_to_websocket(request, recursive=False) + # If post is in this thread, its thread was already notified. + # Otherwise, notify its thread separately. + if ref_post.get_thread().id != thread_id: + ref_post.send_to_websocket(request, recursive=False) + except ObjectDoesNotExist: + pass def save(self, force_insert=False, force_update=False, using=None, update_fields=None): @@ -437,9 +442,10 @@ class Post(models.Model, Viewable): for reply_number in re.finditer(REGEX_REPLY, self.get_raw_text()): post_id = reply_number.group(1) - ref_post = Post.objects.filter(id=post_id) - if ref_post.count() > 0: - referenced_post = ref_post[0] + + try: + referenced_post = Post.objects.get(id=post_id) + referenced_post.referenced_posts.add(self) referenced_post.last_edit_time = self.pub_time referenced_post.build_refmap() @@ -452,13 +458,20 @@ class Post(models.Model, Viewable): thread.last_edit_time = self.pub_time thread.save(update_fields=['last_edit_time', 'bumpable']) + except ObjectDoesNotExist: + pass def connect_threads(self): + """ + If the referenced post is an OP in another thread, + make this post multi-thread. + """ + for reply_number in re.finditer(REGEX_MULTI_THREAD, self.get_raw_text()): post_id = reply_number.group(1) - ref_post = Post.objects.filter(id=post_id) - if ref_post.count() > 0: - referenced_post = ref_post[0] + + try: + referenced_post = Post.objects.get(id=post_id) if referenced_post.is_opening(): referenced_threads = referenced_post.get_threads().all() @@ -470,3 +483,5 @@ class Post(models.Model, Viewable): thread.save(update_fields=['last_edit_time', 'bumpable']) self.threads.add(thread) + except ObjectDoesNotExist: + pass