Removes a block of egregious stupidity from update_vote and replaces with something that actually works

This commit is contained in:
Christopher Neugebauer 2016-06-19 18:37:54 +10:00
parent 08698e5326
commit 57acd04852

View file

@ -8,6 +8,7 @@ from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.db.models import Q, F from django.db.models import Q, F
from django.db.models import Case, When, Value from django.db.models import Case, When, Value
from django.db.models import Count
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -268,57 +269,30 @@ class ProposalResult(models.Model):
def full_calculate(cls): def full_calculate(cls):
for proposal in ProposalBase.objects.all(): for proposal in ProposalBase.objects.all():
result, created = cls._default_manager.get_or_create(proposal=proposal) result, created = cls._default_manager.get_or_create(proposal=proposal)
result.comment_count = Review.objects.filter(proposal=proposal).count() result.update_vote()
result.vote_count = LatestVote.objects.filter(proposal=proposal).count()
result.abstain = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.ABSTAIN,
).count()
result.plus_two = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.PLUS_TWO
).count()
result.plus_one = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.PLUS_ONE
).count()
result.minus_one = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.MINUS_ONE
).count()
result.minus_two = LatestVote.objects.filter(
proposal=proposal,
vote=VOTES.MINUS_TWO
).count()
result.save()
cls._default_manager.filter(pk=result.pk).update(score=score_expression())
def update_vote(self, vote, previous=None, removal=False): def update_vote(self, *a, **k):
mapping = { proposal = self.proposal
VOTES.ABSTAIN: "abstain", self.comment_count = Review.objects.filter(proposal=proposal).count()
VOTES.PLUS_TWO: "plus_two", agg = LatestVote.objects.filter(proposal=proposal).values(
VOTES.PLUS_ONE: "plus_one", "vote"
VOTES.MINUS_ONE: "minus_one", ).annotate(
VOTES.MINUS_TWO: "minus_two", count=Count("vote")
} )
if previous: vote_count = {}
if previous == vote: # Set the defaults
return for option in VOTES.CHOICES:
if removal: vote_count[option[0]] = 0
setattr(self, mapping[previous], models.F(mapping[previous]) + 1) # Set the actual values if present
else: for d in agg:
setattr(self, mapping[previous], models.F(mapping[previous]) - 1) vote_count[d["vote"]] = d["count"]
else:
if removal: self.abstain = vote_count[VOTES.ABSTAIN]
self.vote_count = models.F("vote_count") - 1 self.plus_two = vote_count[VOTES.PLUS_TWO]
else: self.plus_one = vote_count[VOTES.PLUS_ONE]
self.vote_count = models.F("vote_count") + 1 self.minus_one = vote_count[VOTES.MINUS_ONE]
if removal: self.minus_two = vote_count[VOTES.MINUS_TWO]
setattr(self, mapping[vote], models.F(mapping[vote]) - 1) self.vote_count = sum(i[1] for i in vote_count.items())
self.comment_count = models.F("comment_count") - 1
else:
setattr(self, mapping[vote], models.F(mapping[vote]) + 1)
self.comment_count = models.F("comment_count") + 1
self.save() self.save()
model = self.__class__ model = self.__class__
model._default_manager.filter(pk=self.pk).update(score=score_expression()) model._default_manager.filter(pk=self.pk).update(score=score_expression())