##// END OF EJS Templates
Optimized some post count calls.
neko259 -
r58:e922d63e default
parent child Browse files
Show More
@@ -60,7 +60,7 b' class PostManager(models.Manager):'
60 threads = self.filter(parent=NO_PARENT, tags=tag)
60 threads = self.filter(parent=NO_PARENT, tags=tag)
61 else:
61 else:
62 threads = self.filter(parent=NO_PARENT)
62 threads = self.filter(parent=NO_PARENT)
63 threads = list(threads.order_by('-last_edit_time'))
63 threads = threads.order_by('-last_edit_time')
64
64
65 if page != self.ALL_PAGES:
65 if page != self.ALL_PAGES:
66 thread_count = len(threads)
66 thread_count = len(threads)
@@ -87,7 +87,7 b' class PostManager(models.Manager):'
87 def exists(self, post_id):
87 def exists(self, post_id):
88 posts = self.filter(id=post_id)
88 posts = self.filter(id=post_id)
89
89
90 return len(posts) > 0
90 return posts.count() > 0
91
91
92 def get_thread_page_count(self, tag=None):
92 def get_thread_page_count(self, tag=None):
93 if tag:
93 if tag:
@@ -95,7 +95,8 b' class PostManager(models.Manager):'
95 else:
95 else:
96 threads = self.filter(parent=NO_PARENT)
96 threads = self.filter(parent=NO_PARENT)
97
97
98 return int(math.ceil(len(threads) / float(settings.THREADS_PER_PAGE)))
98 return int(math.ceil(threads.count() / float(
99 settings.THREADS_PER_PAGE)))
99
100
100 def _delete_old_threads(self):
101 def _delete_old_threads(self):
101 """
102 """
@@ -103,8 +104,6 b' class PostManager(models.Manager):'
103 delete the old ones.
104 delete the old ones.
104 """
105 """
105
106
106 # TODO Try to find a better way to get the active thread count.
107
108 # TODO Move old threads to the archive instead of deleting them.
107 # TODO Move old threads to the archive instead of deleting them.
109 # Maybe make some 'old' field in the model to indicate the thread
108 # Maybe make some 'old' field in the model to indicate the thread
110 # must not be shown and be able for replying.
109 # must not be shown and be able for replying.
@@ -114,7 +113,7 b' class PostManager(models.Manager):'
114
113
115 if thread_count > settings.MAX_THREAD_COUNT:
114 if thread_count > settings.MAX_THREAD_COUNT:
116 num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT
115 num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT
117 old_threads = threads[-num_threads_to_delete:]
116 old_threads = threads[thread_count - num_threads_to_delete:]
118
117
119 for thread in old_threads:
118 for thread in old_threads:
120 self.delete_post(thread)
119 self.delete_post(thread)
@@ -152,6 +151,8 b' class Tag(models.Model):'
152 section. There can be multiple tags for each message
151 section. There can be multiple tags for each message
153 """
152 """
154
153
154 OPENING_POST_WEIGHT = 5
155
155 objects = TagManager()
156 objects = TagManager()
156
157
157 name = models.CharField(max_length=100)
158 name = models.CharField(max_length=100)
@@ -166,13 +167,14 b' class Tag(models.Model):'
166
167
167 def get_post_count(self):
168 def get_post_count(self):
168 posts_with_tag = Post.objects.get_threads(tag=self)
169 posts_with_tag = Post.objects.get_threads(tag=self)
169 return len(posts_with_tag)
170 return posts_with_tag.count()
170
171
171 def get_popularity(self):
172 def get_popularity(self):
172 posts_with_tag = Post.objects.get_threads(tag=self)
173 posts_with_tag = Post.objects.get_threads(tag=self)
173 reply_count = 0
174 reply_count = 0
174 for post in posts_with_tag:
175 for post in posts_with_tag:
175 reply_count += post.get_reply_count()
176 reply_count += post.get_reply_count()
177 reply_count += self.OPENING_POST_WEIGHT
176
178
177 return reply_count
179 return reply_count
178
180
@@ -217,7 +219,7 b' class Post(models.Model):'
217 return Post.objects.filter(parent=self.id)
219 return Post.objects.filter(parent=self.id)
218
220
219 def get_reply_count(self):
221 def get_reply_count(self):
220 return len(self._get_replies())
222 return self._get_replies().count()
221
223
222 def get_images_count(self):
224 def get_images_count(self):
223 images_count = 1 if self.image else 0
225 images_count = 1 if self.image else 0
General Comments 0
You need to be logged in to leave comments. Login now