Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions askbot/management/commands/fix_comment_counts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Management command for fixing comment counts in questions and answers

Bug in converting answer to comment stored wrong comment count in target
question or answer, and in some cases that makes it imposible for users to view
all the comments.
"""

from django.core.management.base import NoArgsCommand
from django.db.models import signals, Count, F
from askbot.models import Post
from askbot.utils.console import ProgressBar

class Command(NoArgsCommand):

help = "Fixes the wrong comment counts on questions and answers, "\
"where answers have been converted to comments.\n"

def remove_save_signals(self):
"""Prevent possible unvanted side effects of saving
"""
signals.pre_save.receivers = []
signals.post_save.receivers = []

def handle(self, *arguments, **options):
"""Function that handles the command job
"""
self.remove_save_signals()
posts = Post.objects.annotate(real_comment_count=Count('comments')
).exclude(real_comment_count=F('comment_count'))
count = posts.count()
message = 'Fixing comment counts'
for post in ProgressBar(posts.iterator(), count, message):
post.comment_count = post.comments.count();
post.save()
6 changes: 3 additions & 3 deletions askbot/views/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,8 @@ def repost_answer_as_comment(request, destination=None):
if len(answer.text) <= askbot_settings.MAX_COMMENT_LENGTH:
answer.post_type = 'comment'
answer.parent = destination_post
#can we trust this?
old_comment_count = answer.comment_count

new_comment_count = answer.comments.count() + 1
answer.comment_count = 0

answer_comments = models.Post.objects.get_comments().filter(parent=answer)
Expand All @@ -883,7 +883,7 @@ def repost_answer_as_comment(request, destination=None):
answer.parse_and_save(author=answer.author)
answer.thread.update_answer_count()

answer.parent.comment_count = 1 + old_comment_count
answer.parent.comment_count += new_comment_count
answer.parent.save()

answer.thread.invalidate_cached_data()
Expand Down