Removes div-by-zero error if the first vote is an abstention

This commit is contained in:
Christopher Neugebauer 2016-06-18 17:01:56 +10:00
parent 47a6f212f2
commit 6e133970d9

View file

@ -5,6 +5,7 @@ from decimal import Decimal
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.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
@ -16,13 +17,18 @@ from symposion.schedule.models import Presentation
def score_expression(): def score_expression():
return ( score = (
(2 * F("plus_two") + F("plus_one")) - (2 * F("plus_two") + F("plus_one")) -
(F("minus_one") + 2 * F("minus_two")) (F("minus_one") + 2 * F("minus_two"))
) / ( ) / (
F("vote_count") - F("abstain") * 1.0 F("vote_count") - F("abstain") * 1.0
) )
return Case(
When(vote_count=F("abstain"), then=Value("0")), # no divide by zero
default=score,
)
class Votes(object): class Votes(object):
ABSTAIN = "0" ABSTAIN = "0"