From 6755a1604094e601fc6df88929841b5a06690c90 Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Thu, 1 Dec 2011 16:22:54 +0530 Subject: Clean up question template and speed up monitor For a bash question we now put a #!/bin/bash at the top. We now use annotations to speed up the monitor page rendering rather than doing the sort in Python which ends up being inefficient due to the db calls. I get a roughly 5x improvement in speed. --- exam/views.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'exam') diff --git a/exam/views.py b/exam/views.py index 892b35f..c178a0b 100644 --- a/exam/views.py +++ b/exam/views.py @@ -9,6 +9,7 @@ from django.contrib.auth import login, logout, authenticate from django.shortcuts import render_to_response, get_object_or_404, redirect from django.template import RequestContext from django.http import Http404 +from django.db.models import Sum # Local imports. from exam.models import Quiz, Question, QuestionPaper, Profile, Answer, User @@ -262,6 +263,7 @@ def complete(request, reason=None): else: return my_redirect('/exam/') + def monitor(request, quiz_id=None): """Monitor the progress of the papers taken so far.""" user = request.user @@ -282,12 +284,8 @@ def monitor(request, quiz_id=None): papers = [] quiz = None else: - papers = QuestionPaper.objects.filter(quiz=quiz, - user__profile__isnull=False) - - papers = sorted(papers, - cmp=lambda x, y: cmp(x.get_total_marks(), y.get_total_marks()), - reverse=True) + papers = QuestionPaper.objects.all().annotate( + total=Sum('answers__marks')).order_by('-total') context = {'papers': papers, 'quiz': quiz, 'quizzes': None} return my_render_to_response('exam/monitor.html', context, -- cgit