Show More
@@ -5,7 +5,6 b' import re' | |||
|
5 | 5 | |
|
6 | 6 | from adjacent import Client |
|
7 | 7 | from django.core.cache import cache |
|
8 | from django.utils.functional import cached_property | |
|
9 | 8 | from django.core.urlresolvers import reverse |
|
10 | 9 | from django.db import models, transaction |
|
11 | 10 | from django.db.models import TextField |
@@ -154,7 +153,7 b' class PostManager(models.Manager):' | |||
|
154 | 153 | referenced_post.build_refmap() |
|
155 | 154 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
156 | 155 | |
|
157 | referenced_thread = referenced_post.thread | |
|
156 | referenced_thread = referenced_post.get_thread() | |
|
158 | 157 | referenced_thread.last_edit_time = post.pub_time |
|
159 | 158 | referenced_thread.save(update_fields=['last_edit_time']) |
|
160 | 159 | |
@@ -272,13 +271,13 b' class Post(models.Model, Viewable):' | |||
|
272 | 271 | Checks if this is an opening post or just a reply. |
|
273 | 272 | """ |
|
274 | 273 | |
|
275 | return self.thread.get_opening_post_id() == self.id | |
|
274 | return self.get_thread().get_opening_post_id() == self.id | |
|
276 | 275 | |
|
277 | 276 | @transaction.atomic |
|
278 | 277 | def add_tag(self, tag): |
|
279 | 278 | edit_time = timezone.now() |
|
280 | 279 | |
|
281 | thread = get_thread | |
|
280 | thread = self.get_thread() | |
|
282 | 281 | thread.add_tag(tag) |
|
283 | 282 | self.last_edit_time = edit_time |
|
284 | 283 | self.save(update_fields=['last_edit_time']) |
@@ -296,7 +295,7 b' class Post(models.Model, Viewable):' | |||
|
296 | 295 | |
|
297 | 296 | if not link: |
|
298 | 297 | if not thread: |
|
299 | thread = self.thread | |
|
298 | thread = self.get_thread() | |
|
300 | 299 | |
|
301 | 300 | opening_id = thread.get_opening_post_id() |
|
302 | 301 | |
@@ -310,11 +309,6 b' class Post(models.Model, Viewable):' | |||
|
310 | 309 | |
|
311 | 310 | return link |
|
312 | 311 | |
|
313 | @cached_property | |
|
314 | def thread(self): | |
|
315 | return self.thread_new | |
|
316 | ||
|
317 | # TODO Deprecated, remove this and use cached property | |
|
318 | 312 | def get_thread(self) -> Thread: |
|
319 | 313 | """ |
|
320 | 314 | Gets post's thread. |
@@ -334,7 +328,7 b' class Post(models.Model, Viewable):' | |||
|
334 | 328 | """ |
|
335 | 329 | |
|
336 | 330 | is_opening = kwargs.get(PARAMETER_IS_OPENING, self.is_opening()) |
|
337 | thread = kwargs.get(PARAMETER_THREAD, self.thread) | |
|
331 | thread = kwargs.get(PARAMETER_THREAD, self.get_thread()) | |
|
338 | 332 | can_bump = kwargs.get(PARAMETER_BUMPABLE, thread.can_bump()) |
|
339 | 333 | |
|
340 | 334 | if is_opening: |
@@ -371,9 +365,9 b' class Post(models.Model, Viewable):' | |||
|
371 | 365 | image.delete() |
|
372 | 366 | |
|
373 | 367 | if self.is_opening(): |
|
374 | self.thread.delete() | |
|
368 | self.get_thread().delete() | |
|
375 | 369 | else: |
|
376 | thread = self.thread | |
|
370 | thread = self.get_thread() | |
|
377 | 371 | thread.last_edit_time = timezone.now() |
|
378 | 372 | thread.save() |
|
379 | 373 | |
@@ -421,7 +415,7 b' class Post(models.Model, Viewable):' | |||
|
421 | 415 | |
|
422 | 416 | client = Client() |
|
423 | 417 | |
|
424 | thread = self.thread | |
|
418 | thread = self.get_thread() | |
|
425 | 419 | thread_id = thread.id |
|
426 | 420 | channel_name = WS_CHANNEL_THREAD + str(thread.get_opening_post_id()) |
|
427 | 421 | client.publish(channel_name, { |
@@ -1,8 +1,8 b'' | |||
|
1 | 1 | import logging |
|
2 | 2 | from django.db.models import Count, Sum |
|
3 | 3 | from django.utils import timezone |
|
4 | from django.core.cache import cache | |
|
4 | 5 | from django.db import models |
|
5 | from django.utils.functional import cached_property | |
|
6 | 6 | from boards import settings |
|
7 | 7 | |
|
8 | 8 | __author__ = 'neko259' |
@@ -11,6 +11,9 b' from boards import settings' | |||
|
11 | 11 | logger = logging.getLogger(__name__) |
|
12 | 12 | |
|
13 | 13 | |
|
14 | CACHE_KEY_OPENING_POST = 'opening_post_id' | |
|
15 | ||
|
16 | ||
|
14 | 17 | class ThreadManager(models.Manager): |
|
15 | 18 | def process_oldest_threads(self): |
|
16 | 19 | """ |
@@ -134,24 +137,30 b' class Thread(models.Model):' | |||
|
134 | 137 | |
|
135 | 138 | self.tags.add(tag) |
|
136 | 139 | |
|
137 | @cached_property | |
|
138 | def opening_post(self): | |
|
139 | return self.get_opening_post() | |
|
140 | ||
|
141 | # TODO Remove this and use cached property | |
|
142 | def get_opening_post(self): | |
|
140 | def get_opening_post(self, only_id=False): | |
|
143 | 141 | """ |
|
144 | 142 | Gets the first post of the thread |
|
145 | 143 | """ |
|
146 | 144 | |
|
147 |
|
|
|
145 | query = self.replies.order_by('pub_time') | |
|
146 | if only_id: | |
|
147 | query = query.only('id') | |
|
148 | opening_post = query.first() | |
|
149 | ||
|
150 | return opening_post | |
|
148 | 151 | |
|
149 | 152 | def get_opening_post_id(self): |
|
150 | 153 | """ |
|
151 | 154 | Gets ID of the first thread post. |
|
152 | 155 | """ |
|
153 | 156 | |
|
154 | return self.opening_post.id | |
|
157 | cache_key = CACHE_KEY_OPENING_POST + str(self.id) | |
|
158 | opening_post_id = cache.get(cache_key) | |
|
159 | if not opening_post_id: | |
|
160 | opening_post_id = self.get_opening_post(only_id=True).id | |
|
161 | cache.set(cache_key, opening_post_id) | |
|
162 | ||
|
163 | return opening_post_id | |
|
155 | 164 | |
|
156 | 165 | def __unicode__(self): |
|
157 | 166 | return str(self.id) |
@@ -161,7 +170,7 b' class Thread(models.Model):' | |||
|
161 | 170 | Gets opening post's pub time because thread does not have its own one. |
|
162 | 171 | """ |
|
163 | 172 | |
|
164 | return self.opening_post.pub_time | |
|
173 | return self.get_opening_post().pub_time | |
|
165 | 174 | |
|
166 | 175 | def delete(self, using=None): |
|
167 | 176 | if self.replies.exists(): |
@@ -170,4 +179,4 b' class Thread(models.Model):' | |||
|
170 | 179 | super(Thread, self).delete(using) |
|
171 | 180 | |
|
172 | 181 | def __str__(self): |
|
173 |
return 'T#{}/{}'.format(self.id, self.get_opening_post_id()) |
|
|
182 | return 'T#{}/{}'.format(self.id, self.get_opening_post_id()) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now