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.
This commit is contained in:
Brian Rosner 2012-10-03 12:17:03 -06:00
parent e5f87cce90
commit 2c46e56b35

View file

@ -130,21 +130,22 @@ class Review(models.Model):
proposal=self.proposal, proposal=self.proposal,
user=self.user, user=self.user,
) )
# find previous vote before self
try: 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: 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. # treat it as a last, but delete the latest vote.
self.proposal.result.update_vote(self.vote, removal=True) self.proposal.result.update_vote(self.vote, removal=True)
lv = LatestVote.objects.filter(proposal=self.proposal, user=self.user) lv = LatestVote.objects.filter(proposal=self.proposal, user=self.user)
lv.delete() lv.delete()
else: else:
# handle that we've found a previous vote # handle that we've found a latest vote
# check if self is the last vote # check if self is the lastest vote
if self == user_reviews.order_by("-submitted_at")[0]: if self == latest:
# self is the latest which means we need to treat as last. # self is the latest review; revert the latest vote to the
# revert the latest vote to previous vote. # 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) self.proposal.result.update_vote(self.vote, previous=previous.vote, removal=True)
lv = LatestVote.objects.filter(proposal=self.proposal, user=self.user) lv = LatestVote.objects.filter(proposal=self.proposal, user=self.user)
lv.update( lv.update(
@ -152,8 +153,8 @@ class Review(models.Model):
submitted_at=previous.submitted_at, submitted_at=previous.submitted_at,
) )
else: else:
# self is not the latest so we just need to decrement the # self is not the latest review so we just need to decrement
# comment count # the comment count
self.proposal.result.comment_count = models.F("comment_count") - 1 self.proposal.result.comment_count = models.F("comment_count") - 1
self.proposal.result.save() self.proposal.result.save()
# in all cases we need to delete the review; let's do it! # in all cases we need to delete the review; let's do it!