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