##// END OF EJS Templates
Moved thread model to a separate module
neko259 -
r691:86f7abf4 1.8-dev
parent child Browse files
Show More
@@ -0,0 +1,155 b''
1 import logging
2 from django.utils import timezone
3 from django.core.cache import cache
4 from django.db import models
5 from neboard import settings
6
7 __author__ = 'neko259'
8
9
10 logger = logging.getLogger(__name__)
11
12
13 CACHE_KEY_OPENING_POST = 'opening_post_id'
14
15
16 class Thread(models.Model):
17
18 class Meta:
19 app_label = 'boards'
20
21 tags = models.ManyToManyField('Tag')
22 bump_time = models.DateTimeField()
23 last_edit_time = models.DateTimeField()
24 replies = models.ManyToManyField('Post', symmetrical=False, null=True,
25 blank=True, related_name='tre+')
26 archived = models.BooleanField(default=False)
27
28 def get_tags(self):
29 """
30 Gets a sorted tag list.
31 """
32
33 return self.tags.order_by('name')
34
35 def bump(self):
36 """
37 Bumps (moves to up) thread if possible.
38 """
39
40 if self.can_bump():
41 self.bump_time = timezone.now()
42
43 logger.info('Bumped thread %d' % self.id)
44
45 def get_reply_count(self):
46 return self.replies.count()
47
48 def get_images_count(self):
49 return self.replies.filter(image_width__gt=0).count()
50
51 def can_bump(self):
52 """
53 Checks if the thread can be bumped by replying to it.
54 """
55
56 if self.archived:
57 return False
58
59 post_count = self.get_reply_count()
60
61 return post_count < settings.MAX_POSTS_PER_THREAD
62
63 def delete_with_posts(self):
64 """
65 Completely deletes thread and all its posts
66 """
67
68 if self.replies.exists():
69 self.replies.all().delete()
70
71 self.delete()
72
73 def get_last_replies(self):
74 """
75 Gets several last replies, not including opening post
76 """
77
78 if settings.LAST_REPLIES_COUNT > 0:
79 reply_count = self.get_reply_count()
80
81 if reply_count > 0:
82 reply_count_to_show = min(settings.LAST_REPLIES_COUNT,
83 reply_count - 1)
84 replies = self.replies.order_by('pub_time').defer(
85 'image_hash', 'poster_user_agent', 'text_markup_type')
86 last_replies = replies[reply_count - reply_count_to_show:]
87
88 return last_replies
89
90 def get_skipped_replies_count(self):
91 """
92 Gets number of posts between opening post and last replies.
93 """
94 reply_count = self.get_reply_count()
95 last_replies_count = min(settings.LAST_REPLIES_COUNT,
96 reply_count - 1)
97 return reply_count - last_replies_count - 1
98
99 def get_replies(self, view_fields_only=False):
100 """
101 Gets sorted thread posts
102 """
103
104 query = self.replies.order_by('pub_time')
105 if view_fields_only:
106 query = query.defer(
107 'image_hash', 'poster_user_agent', 'text_markup_type')
108 return query.all()
109
110 def add_tag(self, tag):
111 """
112 Connects thread to a tag and tag to a thread
113 """
114
115 self.tags.add(tag)
116 tag.threads.add(self)
117
118 def remove_tag(self, tag):
119 self.tags.remove(tag)
120 tag.threads.remove(self)
121
122 def get_opening_post(self, only_id=False):
123 """
124 Gets the first post of the thread
125 """
126
127 query = self.replies.order_by('pub_time')
128 if only_id:
129 query = query.only('id')
130 opening_post = query.first()
131
132 return opening_post
133
134 def get_opening_post_id(self):
135 """
136 Gets ID of the first thread post.
137 """
138
139 cache_key = CACHE_KEY_OPENING_POST + str(self.id)
140 opening_post_id = cache.get(cache_key)
141 if not opening_post_id:
142 opening_post_id = self.get_opening_post(only_id=True).id
143 cache.set(cache_key, opening_post_id)
144
145 return opening_post_id
146
147 def __unicode__(self):
148 return str(self.id)
149
150 def get_pub_time(self):
151 """
152 Gets opening post's pub time because thread does not have its own one.
153 """
154
155 return self.get_opening_post().pub_time No newline at end of file
@@ -6,3 +6,4 b' from boards.models.tag import Tag'
6 from boards.models.user import Ban
6 from boards.models.user import Ban
7 from boards.models.user import Setting
7 from boards.models.user import Setting
8 from boards.models.user import User
8 from boards.models.user import User
9 from boards.models.thread import Thread
@@ -12,6 +12,7 b' from django.core.urlresolvers import rev'
12 from django.db import models, transaction
12 from django.db import models, transaction
13 from django.utils import timezone
13 from django.utils import timezone
14 from markupfield.fields import MarkupField
14 from markupfield.fields import MarkupField
15 from boards.models.thread import Thread
15
16
16 from neboard import settings
17 from neboard import settings
17 from boards import thumbs
18 from boards import thumbs
@@ -21,7 +22,6 b" APP_LABEL_BOARDS = 'boards'"
21
22
22 CACHE_KEY_PPD = 'ppd'
23 CACHE_KEY_PPD = 'ppd'
23 CACHE_KEY_POST_URL = 'post_url'
24 CACHE_KEY_POST_URL = 'post_url'
24 CACHE_KEY_OPENING_POST = 'opening_post_id'
25
25
26 POSTS_PER_DAY_RANGE = range(7)
26 POSTS_PER_DAY_RANGE = range(7)
27
27
@@ -357,147 +357,4 b' class Post(models.Model):'
357 return self.thread_new
357 return self.thread_new
358
358
359 def get_referenced_posts(self):
359 def get_referenced_posts(self):
360 return self.referenced_posts.only('id', 'thread_new')
360 return self.referenced_posts.only('id', 'thread_new') No newline at end of file
361
362
363 class Thread(models.Model):
364
365 class Meta:
366 app_label = APP_LABEL_BOARDS
367
368 tags = models.ManyToManyField('Tag')
369 bump_time = models.DateTimeField()
370 last_edit_time = models.DateTimeField()
371 replies = models.ManyToManyField('Post', symmetrical=False, null=True,
372 blank=True, related_name='tre+')
373 archived = models.BooleanField(default=False)
374
375 def get_tags(self):
376 """
377 Gets a sorted tag list.
378 """
379
380 return self.tags.order_by('name')
381
382 def bump(self):
383 """
384 Bumps (moves to up) thread if possible.
385 """
386
387 if self.can_bump():
388 self.bump_time = timezone.now()
389
390 logger.info('Bumped thread %d' % self.id)
391
392 def get_reply_count(self):
393 return self.replies.count()
394
395 def get_images_count(self):
396 return self.replies.filter(image_width__gt=0).count()
397
398 def can_bump(self):
399 """
400 Checks if the thread can be bumped by replying to it.
401 """
402
403 if self.archived:
404 return False
405
406 post_count = self.get_reply_count()
407
408 return post_count < settings.MAX_POSTS_PER_THREAD
409
410 def delete_with_posts(self):
411 """
412 Completely deletes thread and all its posts
413 """
414
415 if self.replies.exists():
416 self.replies.all().delete()
417
418 self.delete()
419
420 def get_last_replies(self):
421 """
422 Gets several last replies, not including opening post
423 """
424
425 if settings.LAST_REPLIES_COUNT > 0:
426 reply_count = self.get_reply_count()
427
428 if reply_count > 0:
429 reply_count_to_show = min(settings.LAST_REPLIES_COUNT,
430 reply_count - 1)
431 last_replies = self.replies.order_by(
432 'pub_time').defer('image_hash', 'poster_user_agent',
433 'text_markup_type')[
434 reply_count - reply_count_to_show:]
435
436 return last_replies
437
438 def get_skipped_replies_count(self):
439 """
440 Gets number of posts between opening post and last replies.
441 """
442 reply_count = self.get_reply_count()
443 last_replies_count = min(settings.LAST_REPLIES_COUNT,
444 reply_count - 1)
445 return reply_count - last_replies_count - 1
446
447 def get_replies(self, view_fields_only=False):
448 """
449 Gets sorted thread posts
450 """
451
452 query = self.replies.order_by('pub_time')
453 if view_fields_only:
454 query = query.defer(
455 'image_hash', 'poster_user_agent', 'text_markup_type')
456 return query.all()
457
458 def add_tag(self, tag):
459 """
460 Connects thread to a tag and tag to a thread
461 """
462
463 self.tags.add(tag)
464 tag.threads.add(self)
465
466 def remove_tag(self, tag):
467 self.tags.remove(tag)
468 tag.threads.remove(self)
469
470 def get_opening_post(self, only_id=False):
471 """
472 Gets the first post of the thread
473 """
474
475 query = self.replies.order_by('pub_time')
476 if only_id:
477 query = query.only('id')
478 opening_post = query.first()
479
480 return opening_post
481
482 def get_opening_post_id(self):
483 """
484 Gets ID of the first thread post.
485 """
486
487 cache_key = CACHE_KEY_OPENING_POST + str(self.id)
488 opening_post_id = cache.get(cache_key)
489 if not opening_post_id:
490 opening_post_id = self.get_opening_post(only_id=True).id
491 cache.set(cache_key, opening_post_id)
492
493 return opening_post_id
494
495 def __unicode__(self):
496 return str(self.id)
497
498 def get_pub_time(self):
499 """
500 Gets opening post's pub time because thread does not have its own one.
501 """
502
503 return self.get_opening_post().pub_time
General Comments 0
You need to be logged in to leave comments. Login now