From 6e133970d9587c4fe3cb91ef6c06519bbcd98dd9 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 18 Jun 2016 17:01:56 +1000 Subject: [PATCH 1/2] Removes div-by-zero error if the first vote is an abstention --- symposion/reviews/models.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/symposion/reviews/models.py b/symposion/reviews/models.py index f6234d51..3b248328 100644 --- a/symposion/reviews/models.py +++ b/symposion/reviews/models.py @@ -5,6 +5,7 @@ from decimal import Decimal from django.db import models 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.contrib.auth.models import User @@ -16,13 +17,18 @@ from symposion.schedule.models import Presentation def score_expression(): - return ( + score = ( (2 * F("plus_two") + F("plus_one")) - (F("minus_one") + 2 * F("minus_two")) ) / ( 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): ABSTAIN = "0" From d305cd8c13d7b7cc32d13009db26158f9d923def Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 18 Jun 2016 17:07:21 +1000 Subject: [PATCH 2/2] Requires comments for non-abstain votes only --- symposion/reviews/models.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/symposion/reviews/models.py b/symposion/reviews/models.py index 3b248328..c48acf12 100644 --- a/symposion/reviews/models.py +++ b/symposion/reviews/models.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals from datetime import datetime from decimal import Decimal +from django.core.exceptions import ValidationError + from django.db import models from django.db.models import Q, F from django.db.models import Case, When, Value @@ -126,10 +128,21 @@ class Review(models.Model): # No way to encode "-0" vs. "+0" into an IntegerField, and I don't feel # like some complicated encoding system. vote = models.CharField(max_length=2, blank=True, choices=VOTES.CHOICES, verbose_name=_("Vote")) - comment = models.TextField(verbose_name=_("Comment")) + comment = models.TextField( + blank=True, + verbose_name=_("Comment") + ) comment_html = models.TextField(blank=True) submitted_at = models.DateTimeField(default=datetime.now, editable=False, verbose_name=_("Submitted at")) + def clean(self): + err = {} + if self.vote != VOTES.ABSTAIN and not self.comment.strip(): + err["comment"] = ValidationError(_("You must provide a comment")) + + if err: + raise ValidationError(err) + def save(self, **kwargs): self.comment_html = parse(self.comment) if self.vote: