From b675dbad735807156a8e1adf938f68ee51abc619 Mon Sep 17 00:00:00 2001 From: Pami Ketolainen Date: Tue, 7 Jan 2014 15:53:12 +0200 Subject: [PATCH] Fix comment count update when converting answer to comment Fixes #195 --- .../management/commands/fix_comment_counts.py | 34 +++++++++++++++++++ askbot/views/writers.py | 6 ++-- 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 askbot/management/commands/fix_comment_counts.py diff --git a/askbot/management/commands/fix_comment_counts.py b/askbot/management/commands/fix_comment_counts.py new file mode 100644 index 0000000000..3a7f16f828 --- /dev/null +++ b/askbot/management/commands/fix_comment_counts.py @@ -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() diff --git a/askbot/views/writers.py b/askbot/views/writers.py index a63d70ae00..e5068e2850 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -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) @@ -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()