From 2c46e56b353c96796f0a71e149175d11e8c4693e Mon Sep 17 00:00:00 2001 From: Brian Rosner Date: Wed, 3 Oct 2012 12:17:03 -0600 Subject: [PATCH] Fixed bug in review deletion When a review was not the latest it would cause a bug on deletion of the latest. We changed the logic to work with the latest vote versus the previous to fix this issue. Now all review deletion ensure a consistent latest vote state. --- symposion/reviews/models.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/symposion/reviews/models.py b/symposion/reviews/models.py index 1cc5042e..a6ac5657 100644 --- a/symposion/reviews/models.py +++ b/symposion/reviews/models.py @@ -130,21 +130,22 @@ class Review(models.Model): proposal=self.proposal, user=self.user, ) - # find previous vote before self try: - previous = user_reviews.filter(submitted_at__lt=self.submitted_at).order_by("-submitted_at")[0] + # find the latest review + latest = user_reviews.exclude(pk=self.pk).order_by("-submitted_at")[0] except IndexError: - # did not find a previous which means this must be the only one. + # did not find a latest which means this must be the only one. # treat it as a last, but delete the latest vote. self.proposal.result.update_vote(self.vote, removal=True) lv = LatestVote.objects.filter(proposal=self.proposal, user=self.user) lv.delete() else: - # handle that we've found a previous vote - # check if self is the last vote - if self == user_reviews.order_by("-submitted_at")[0]: - # self is the latest which means we need to treat as last. - # revert the latest vote to previous vote. + # handle that we've found a latest vote + # check if self is the lastest vote + if self == latest: + # self is the latest review; revert the latest vote to the + # previous vote + previous = user_reviews.filter(submitted_at__lt=self.submitted_at).order_by("-submitted_at")[0] self.proposal.result.update_vote(self.vote, previous=previous.vote, removal=True) lv = LatestVote.objects.filter(proposal=self.proposal, user=self.user) lv.update( @@ -152,8 +153,8 @@ class Review(models.Model): submitted_at=previous.submitted_at, ) else: - # self is not the latest so we just need to decrement the - # comment count + # self is not the latest review so we just need to decrement + # the comment count self.proposal.result.comment_count = models.F("comment_count") - 1 self.proposal.result.save() # in all cases we need to delete the review; let's do it!