Show More
@@ -5,7 +5,6 b' import re' | |||||
5 |
|
5 | |||
6 | from adjacent import Client |
|
6 | from adjacent import Client | |
7 | from django.core.cache import cache |
|
7 | from django.core.cache import cache | |
8 | from django.utils.functional import cached_property |
|
|||
9 | from django.core.urlresolvers import reverse |
|
8 | from django.core.urlresolvers import reverse | |
10 | from django.db import models, transaction |
|
9 | from django.db import models, transaction | |
11 | from django.db.models import TextField |
|
10 | from django.db.models import TextField | |
@@ -154,7 +153,7 b' class PostManager(models.Manager):' | |||||
154 | referenced_post.build_refmap() |
|
153 | referenced_post.build_refmap() | |
155 | referenced_post.save(update_fields=['refmap', 'last_edit_time']) |
|
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 | referenced_thread.last_edit_time = post.pub_time |
|
157 | referenced_thread.last_edit_time = post.pub_time | |
159 | referenced_thread.save(update_fields=['last_edit_time']) |
|
158 | referenced_thread.save(update_fields=['last_edit_time']) | |
160 |
|
159 | |||
@@ -272,13 +271,13 b' class Post(models.Model, Viewable):' | |||||
272 | Checks if this is an opening post or just a reply. |
|
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 | @transaction.atomic |
|
276 | @transaction.atomic | |
278 | def add_tag(self, tag): |
|
277 | def add_tag(self, tag): | |
279 | edit_time = timezone.now() |
|
278 | edit_time = timezone.now() | |
280 |
|
279 | |||
281 | thread = get_thread |
|
280 | thread = self.get_thread() | |
282 | thread.add_tag(tag) |
|
281 | thread.add_tag(tag) | |
283 | self.last_edit_time = edit_time |
|
282 | self.last_edit_time = edit_time | |
284 | self.save(update_fields=['last_edit_time']) |
|
283 | self.save(update_fields=['last_edit_time']) | |
@@ -296,7 +295,7 b' class Post(models.Model, Viewable):' | |||||
296 |
|
295 | |||
297 | if not link: |
|
296 | if not link: | |
298 | if not thread: |
|
297 | if not thread: | |
299 | thread = self.thread |
|
298 | thread = self.get_thread() | |
300 |
|
299 | |||
301 | opening_id = thread.get_opening_post_id() |
|
300 | opening_id = thread.get_opening_post_id() | |
302 |
|
301 | |||
@@ -310,11 +309,6 b' class Post(models.Model, Viewable):' | |||||
310 |
|
309 | |||
311 | return link |
|
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 | def get_thread(self) -> Thread: |
|
312 | def get_thread(self) -> Thread: | |
319 | """ |
|
313 | """ | |
320 | Gets post's thread. |
|
314 | Gets post's thread. | |
@@ -334,7 +328,7 b' class Post(models.Model, Viewable):' | |||||
334 | """ |
|
328 | """ | |
335 |
|
329 | |||
336 | is_opening = kwargs.get(PARAMETER_IS_OPENING, self.is_opening()) |
|
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 | can_bump = kwargs.get(PARAMETER_BUMPABLE, thread.can_bump()) |
|
332 | can_bump = kwargs.get(PARAMETER_BUMPABLE, thread.can_bump()) | |
339 |
|
333 | |||
340 | if is_opening: |
|
334 | if is_opening: | |
@@ -371,9 +365,9 b' class Post(models.Model, Viewable):' | |||||
371 | image.delete() |
|
365 | image.delete() | |
372 |
|
366 | |||
373 | if self.is_opening(): |
|
367 | if self.is_opening(): | |
374 | self.thread.delete() |
|
368 | self.get_thread().delete() | |
375 | else: |
|
369 | else: | |
376 | thread = self.thread |
|
370 | thread = self.get_thread() | |
377 | thread.last_edit_time = timezone.now() |
|
371 | thread.last_edit_time = timezone.now() | |
378 | thread.save() |
|
372 | thread.save() | |
379 |
|
373 | |||
@@ -421,7 +415,7 b' class Post(models.Model, Viewable):' | |||||
421 |
|
415 | |||
422 | client = Client() |
|
416 | client = Client() | |
423 |
|
417 | |||
424 | thread = self.thread |
|
418 | thread = self.get_thread() | |
425 | thread_id = thread.id |
|
419 | thread_id = thread.id | |
426 | channel_name = WS_CHANNEL_THREAD + str(thread.get_opening_post_id()) |
|
420 | channel_name = WS_CHANNEL_THREAD + str(thread.get_opening_post_id()) | |
427 | client.publish(channel_name, { |
|
421 | client.publish(channel_name, { |
@@ -1,8 +1,8 b'' | |||||
1 | import logging |
|
1 | import logging | |
2 | from django.db.models import Count, Sum |
|
2 | from django.db.models import Count, Sum | |
3 | from django.utils import timezone |
|
3 | from django.utils import timezone | |
|
4 | from django.core.cache import cache | |||
4 | from django.db import models |
|
5 | from django.db import models | |
5 | from django.utils.functional import cached_property |
|
|||
6 | from boards import settings |
|
6 | from boards import settings | |
7 |
|
7 | |||
8 | __author__ = 'neko259' |
|
8 | __author__ = 'neko259' | |
@@ -11,6 +11,9 b' from boards import settings' | |||||
11 | logger = logging.getLogger(__name__) |
|
11 | logger = logging.getLogger(__name__) | |
12 |
|
12 | |||
13 |
|
13 | |||
|
14 | CACHE_KEY_OPENING_POST = 'opening_post_id' | |||
|
15 | ||||
|
16 | ||||
14 | class ThreadManager(models.Manager): |
|
17 | class ThreadManager(models.Manager): | |
15 | def process_oldest_threads(self): |
|
18 | def process_oldest_threads(self): | |
16 | """ |
|
19 | """ | |
@@ -134,24 +137,30 b' class Thread(models.Model):' | |||||
134 |
|
137 | |||
135 | self.tags.add(tag) |
|
138 | self.tags.add(tag) | |
136 |
|
139 | |||
137 | @cached_property |
|
140 | def get_opening_post(self, only_id=False): | |
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): |
|
|||
143 | """ |
|
141 | """ | |
144 | Gets the first post of the thread |
|
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 | def get_opening_post_id(self): |
|
152 | def get_opening_post_id(self): | |
150 | """ |
|
153 | """ | |
151 | Gets ID of the first thread post. |
|
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 | def __unicode__(self): |
|
165 | def __unicode__(self): | |
157 | return str(self.id) |
|
166 | return str(self.id) | |
@@ -161,7 +170,7 b' class Thread(models.Model):' | |||||
161 | Gets opening post's pub time because thread does not have its own one. |
|
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 | def delete(self, using=None): |
|
175 | def delete(self, using=None): | |
167 | if self.replies.exists(): |
|
176 | if self.replies.exists(): | |
@@ -170,4 +179,4 b' class Thread(models.Model):' | |||||
170 | super(Thread, self).delete(using) |
|
179 | super(Thread, self).delete(using) | |
171 |
|
180 | |||
172 | def __str__(self): |
|
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 |
@@ -70,7 +70,10 b' def post_view(post, moderator=False, nee' | |||||
70 | else: |
|
70 | else: | |
71 | thread = post.get_thread() |
|
71 | thread = post.get_thread() | |
72 |
|
72 | |||
73 | can_bump = thread.can_bump() |
|
73 | if 'can_bump' in kwargs: | |
|
74 | can_bump = kwargs['can_bump'] | |||
|
75 | else: | |||
|
76 | can_bump = thread.can_bump() | |||
74 |
|
77 | |||
75 | opening_post_id = thread.get_opening_post_id() |
|
78 | opening_post_id = thread.get_opening_post_id() | |
76 |
|
79 |
General Comments 0
You need to be logged in to leave comments.
Login now