# HG changeset patch # User neko259 # Date 2015-01-21 16:17:29 # Node ID a37f5ca1da4356ce3c4c7de8a7684e98f8c046a9 # Parent 0e9773cea89e4f3f010ce97132af455046272fa4 Start using cached properties (not everywhere yet, still) diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -5,6 +5,7 @@ import re from adjacent import Client from django.core.cache import cache +from django.utils.functional import cached_property from django.core.urlresolvers import reverse from django.db import models, transaction from django.db.models import TextField @@ -153,7 +154,7 @@ class PostManager(models.Manager): referenced_post.build_refmap() referenced_post.save(update_fields=['refmap', 'last_edit_time']) - referenced_thread = referenced_post.get_thread() + referenced_thread = referenced_post.thread referenced_thread.last_edit_time = post.pub_time referenced_thread.save(update_fields=['last_edit_time']) @@ -271,13 +272,13 @@ class Post(models.Model, Viewable): Checks if this is an opening post or just a reply. """ - return self.get_thread().get_opening_post_id() == self.id + return self.thread.get_opening_post_id() == self.id @transaction.atomic def add_tag(self, tag): edit_time = timezone.now() - thread = self.get_thread() + thread = get_thread thread.add_tag(tag) self.last_edit_time = edit_time self.save(update_fields=['last_edit_time']) @@ -295,7 +296,7 @@ class Post(models.Model, Viewable): if not link: if not thread: - thread = self.get_thread() + thread = self.thread opening_id = thread.get_opening_post_id() @@ -309,6 +310,11 @@ class Post(models.Model, Viewable): return link + @cached_property + def thread(self): + return self.thread_new + + # TODO Deprecated, remove this and use cached property def get_thread(self) -> Thread: """ Gets post's thread. @@ -328,7 +334,7 @@ class Post(models.Model, Viewable): """ is_opening = kwargs.get(PARAMETER_IS_OPENING, self.is_opening()) - thread = kwargs.get(PARAMETER_THREAD, self.get_thread()) + thread = kwargs.get(PARAMETER_THREAD, self.thread) can_bump = kwargs.get(PARAMETER_BUMPABLE, thread.can_bump()) if is_opening: @@ -365,9 +371,9 @@ class Post(models.Model, Viewable): image.delete() if self.is_opening(): - self.get_thread().delete() + self.thread.delete() else: - thread = self.get_thread() + thread = self.thread thread.last_edit_time = timezone.now() thread.save() @@ -415,7 +421,7 @@ class Post(models.Model, Viewable): client = Client() - thread = self.get_thread() + thread = self.thread thread_id = thread.id channel_name = WS_CHANNEL_THREAD + str(thread.get_opening_post_id()) client.publish(channel_name, { diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -1,8 +1,8 @@ import logging from django.db.models import Count, Sum from django.utils import timezone -from django.core.cache import cache from django.db import models +from django.utils.functional import cached_property from boards import settings __author__ = 'neko259' @@ -11,9 +11,6 @@ from boards import settings logger = logging.getLogger(__name__) -CACHE_KEY_OPENING_POST = 'opening_post_id' - - class ThreadManager(models.Manager): def process_oldest_threads(self): """ @@ -137,30 +134,24 @@ class Thread(models.Model): self.tags.add(tag) - def get_opening_post(self, only_id=False): + @cached_property + def opening_post(self): + return self.get_opening_post() + + # TODO Remove this and use cached property + def get_opening_post(self): """ Gets the first post of the thread """ - query = self.replies.order_by('pub_time') - if only_id: - query = query.only('id') - opening_post = query.first() - - return opening_post + return self.replies.order_by('pub_time').first() def get_opening_post_id(self): """ Gets ID of the first thread post. """ - cache_key = CACHE_KEY_OPENING_POST + str(self.id) - opening_post_id = cache.get(cache_key) - if not opening_post_id: - opening_post_id = self.get_opening_post(only_id=True).id - cache.set(cache_key, opening_post_id) - - return opening_post_id + return self.opening_post.id def __unicode__(self): return str(self.id) @@ -170,7 +161,7 @@ class Thread(models.Model): Gets opening post's pub time because thread does not have its own one. """ - return self.get_opening_post().pub_time + return self.opening_post.pub_time def delete(self, using=None): if self.replies.exists(): @@ -179,4 +170,4 @@ class Thread(models.Model): super(Thread, self).delete(using) def __str__(self): - return 'T#{}/{}'.format(self.id, self.get_opening_post_id()) \ No newline at end of file + return 'T#{}/{}'.format(self.id, self.get_opening_post_id()) diff --git a/boards/templatetags/board.py b/boards/templatetags/board.py --- a/boards/templatetags/board.py +++ b/boards/templatetags/board.py @@ -70,10 +70,7 @@ def post_view(post, moderator=False, nee else: thread = post.get_thread() - if 'can_bump' in kwargs: - can_bump = kwargs['can_bump'] - else: - can_bump = thread.can_bump() + can_bump = thread.can_bump() opening_post_id = thread.get_opening_post_id()