Fix interactive voting result when using olympic averages
authorMagnus Hagander <magnus@hagander.net>
Mon, 6 Jan 2025 12:55:47 +0000 (13:55 +0100)
committerMagnus Hagander <magnus@hagander.net>
Mon, 6 Jan 2025 12:55:47 +0000 (13:55 +0100)
Commit a2d79c1e missed that there is a separate codepath that updates
the average values during voting (for just the specific session that's
being voted on), so the value listed could become wrong until the page
was reloaded (the stored value was always correct and a page reload
would refresh all numbers).

postgresqleu/confreg/views.py

index f0b198102296d2203623df377f8156ea78390314..17b04033023ede5a339860def626ee672c345c0b 100644 (file)
@@ -10,7 +10,7 @@ from django.contrib.auth.models import User
 from django.contrib import messages
 from django.conf import settings
 from django.db import transaction, connection
-from django.db.models import Q, Count, Avg, Prefetch
+from django.db.models import Q, Count, Avg, Sum, Min, Max, Prefetch
 from django.db.models.expressions import F
 from django.forms import ValidationError
 from django.utils import timezone
@@ -3020,7 +3020,19 @@ def talkvote_vote(request, confname):
         ConferenceSessionVote.objects.filter(session=session, voter=request.user).delete()
 
     # Re-calculate the average
-    avg = session.conferencesessionvote_set.filter(vote__gt=0).aggregate(Avg('vote'))['vote__avg']
+    if conference.scoring_method == 0:
+        # Normal average
+        avg = session.conferencesessionvote_set.filter(vote__gt=0).aggregate(Avg('vote'))['vote__avg']
+    elif conference.scoring_method == 1:
+        # Olympic average
+        vals = session.conferencesessionvote_set.filter(vote__gt=0).aggregate(Avg('vote'), Min('vote'), Max('vote'), Sum('vote'), Count('vote'))
+        if vals['vote__count'] > 2:
+            avg = (vals['vote__sum'] - vals['vote__min'] - vals['vote__max']) / (vals['vote__count'] - 2)
+        else:
+            avg = vals['vote__avg']
+    else:
+        return HttpResponse("Invalid scoring method", status=500)
+
     if avg is None:
         return HttpResponse("", content_type='text/plain')
     return HttpResponse("{0:.2f}".format(avg), content_type='text/plain')