Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions helios/stats_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from django.core.paginator import Paginator
from django.urls import reverse
from django.db.models import Max, Count
from django.db.models import Max, Count, Q
from django.http import HttpResponseRedirect
from django.views.decorators.http import require_http_methods

Expand Down Expand Up @@ -56,10 +56,16 @@ def elections(request):

def recent_votes(request):
user = require_admin(request)

# elections with a vote in the last 24 hours, ordered by most recent cast vote time
# also annotated with number of votes cast in last 24 hours
elections_with_votes_in_24hours = Election.objects.filter(voter__castvote__cast_at__gt= datetime.datetime.utcnow() - datetime.timedelta(days=1)).annotate(last_cast_vote = Max('voter__castvote__cast_at'), num_recent_cast_votes = Count('voter__castvote')).order_by('-last_cast_vote')
cutoff = datetime.datetime.utcnow() - datetime.timedelta(days=1)
elections_with_votes_in_24hours = Election.objects.filter(
voter__castvote__cast_at__gt=cutoff
).distinct().annotate(
last_cast_vote=Max('voter__castvote__cast_at', filter=Q(voter__castvote__cast_at__gt=cutoff)),
num_recent_cast_votes=Count('voter__castvote', filter=Q(voter__castvote__cast_at__gt=cutoff))
).order_by('-last_cast_vote')

return render_template(request, "stats_recent_votes", {'elections' : elections_with_votes_in_24hours})

Expand Down
Loading