diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -57,6 +57,10 @@ class PostManager(models.Manager): def create_post(self, title, text, image=None, thread=None, ip=NO_IP, tags=None, user=None): + """ + Create new post + """ + self.clear_ppd_cache_if_old() posting_time = timezone.now() @@ -95,6 +99,10 @@ class PostManager(models.Manager): return post def delete_post(self, post): + """ + Delete post and update its thread + """ + thread = post.thread_new thread.last_edit_time = timezone.now() thread.save() @@ -102,6 +110,10 @@ class PostManager(models.Manager): post.delete() def delete_posts_by_ip(self, ip): + """ + Delete all posts of the author with same IP + """ + posts = self.filter(poster_ip=ip) map(self.delete_post, posts) @@ -159,7 +171,9 @@ class PostManager(models.Manager): map(Thread.delete_with_posts, old_threads) def connect_replies(self, post): - """Connect replies to a post to show them as a refmap""" + """ + Connect replies to a post to show them as a reflink map + """ for reply_number in re.finditer(REGEX_REPLY, post.text.raw): post_id = reply_number.group(1) @@ -171,10 +185,16 @@ class PostManager(models.Manager): referenced_post.save() def _get_page_count(self, thread_count): + """ + Get number of pages that will be needed for all threads + """ + return int(math.ceil(thread_count / float(settings.THREADS_PER_PAGE))) def get_posts_per_day(self): - """Get average count of posts per day for the last 7 days""" + """ + Get average count of posts per day for the last 7 days + """ ppd = cache.get(CACHE_KEY_PPD) if ppd: @@ -210,6 +230,7 @@ class Post(models.Model): class Meta: app_label = APP_LABEL_BOARDS + # TODO Save original file name to some field def _update_image_filename(self, filename): """Get unique image filename""" @@ -279,12 +300,16 @@ class Thread(models.Model): blank=True, related_name='tre+') def get_tags(self): - """Get a sorted tag list""" + """ + Get a sorted tag list + """ return self.tags.order_by('name') def bump(self): - """Bump (move to up) thread""" + """ + Bump (move to up) thread + """ if self.can_bump(): self.bump_time = timezone.now() @@ -296,14 +321,18 @@ class Thread(models.Model): return self.replies.filter(image_width__gt=0).count() def can_bump(self): - """Check if the thread can be bumped by replying""" + """ + Check if the thread can be bumped by replying + """ post_count = self.get_reply_count() return post_count <= settings.MAX_POSTS_PER_THREAD def delete_with_posts(self): - """Completely delete thread""" + """ + Completely delete thread and all its posts + """ if self.replies.count() > 0: map(Post.objects.delete_post, self.replies.all()) @@ -311,7 +340,9 @@ class Thread(models.Model): self.delete() def get_last_replies(self): - """Get last replies, not including opening post""" + """ + Get last replies, not including opening post + """ if settings.LAST_REPLIES_COUNT > 0: reply_count = self.get_reply_count() @@ -325,21 +356,34 @@ class Thread(models.Model): return last_replies def get_replies(self): - """Get sorted thread posts""" + """ + Get sorted thread posts + """ return self.replies.all().order_by('pub_time') def add_tag(self, tag): - """Connect thread to a tag and tag to a thread""" + """ + Connect thread to a tag and tag to a thread + """ self.tags.add(tag) tag.threads.add(self) def get_opening_post(self): + """ + Get first post of the thread + """ + return self.get_replies()[0] def __unicode__(self): return str(self.get_replies()[0].id) def get_pub_time(self): + """ + Thread does not have its own pub time, so we need to get it from + the opening post + """ + return self.get_opening_post().pub_time