# HG changeset patch # User neko259 # Date 2013-05-28 17:47:16 # Node ID e922d63e5cd418a74b76140599931eb4ff6a556b # Parent dcbc67f33dc0956f0c6e3d054cc8634c2b6f09a0 Optimized some post count calls. diff --git a/boards/models.py b/boards/models.py --- a/boards/models.py +++ b/boards/models.py @@ -60,7 +60,7 @@ class PostManager(models.Manager): threads = self.filter(parent=NO_PARENT, tags=tag) else: threads = self.filter(parent=NO_PARENT) - threads = list(threads.order_by('-last_edit_time')) + threads = threads.order_by('-last_edit_time') if page != self.ALL_PAGES: thread_count = len(threads) @@ -87,7 +87,7 @@ class PostManager(models.Manager): def exists(self, post_id): posts = self.filter(id=post_id) - return len(posts) > 0 + return posts.count() > 0 def get_thread_page_count(self, tag=None): if tag: @@ -95,7 +95,8 @@ class PostManager(models.Manager): else: threads = self.filter(parent=NO_PARENT) - return int(math.ceil(len(threads) / float(settings.THREADS_PER_PAGE))) + return int(math.ceil(threads.count() / float( + settings.THREADS_PER_PAGE))) def _delete_old_threads(self): """ @@ -103,8 +104,6 @@ class PostManager(models.Manager): delete the old ones. """ - # TODO Try to find a better way to get the active thread count. - # TODO Move old threads to the archive instead of deleting them. # Maybe make some 'old' field in the model to indicate the thread # must not be shown and be able for replying. @@ -114,7 +113,7 @@ class PostManager(models.Manager): if thread_count > settings.MAX_THREAD_COUNT: num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT - old_threads = threads[-num_threads_to_delete:] + old_threads = threads[thread_count - num_threads_to_delete:] for thread in old_threads: self.delete_post(thread) @@ -152,6 +151,8 @@ class Tag(models.Model): section. There can be multiple tags for each message """ + OPENING_POST_WEIGHT = 5 + objects = TagManager() name = models.CharField(max_length=100) @@ -166,13 +167,14 @@ class Tag(models.Model): def get_post_count(self): posts_with_tag = Post.objects.get_threads(tag=self) - return len(posts_with_tag) + return posts_with_tag.count() def get_popularity(self): posts_with_tag = Post.objects.get_threads(tag=self) reply_count = 0 for post in posts_with_tag: reply_count += post.get_reply_count() + reply_count += self.OPENING_POST_WEIGHT return reply_count @@ -217,7 +219,7 @@ class Post(models.Model): return Post.objects.filter(parent=self.id) def get_reply_count(self): - return len(self._get_replies()) + return self._get_replies().count() def get_images_count(self): images_count = 1 if self.image else 0 diff --git a/neboard/settings.py b/neboard/settings.py --- a/neboard/settings.py +++ b/neboard/settings.py @@ -183,4 +183,4 @@ THEMES = [ ('sw', 'Snow White') ] DEFAULT_THEME = 'md' -POPULAR_TAGS=10 \ No newline at end of file +POPULAR_TAGS = 10 \ No newline at end of file