From 1a45f11fc2ee45343980329309edc4be4265d58f Mon Sep 17 00:00:00 2001 From: Ross Date: Sat, 8 Nov 2025 21:48:27 +0000 Subject: [PATCH] test updating view --- generic/views.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/generic/views.py b/generic/views.py index f1ea4cfe..92c8fc2d 100644 --- a/generic/views.py +++ b/generic/views.py @@ -2816,16 +2816,22 @@ class ExamViews(View, LoginRequiredMixin): if self.app_name in ("physics", "sbas", "longs", "shorts"): cached_scores = False - questions = exam.get_questions() + questions_qs = exam.get_questions() else: - questions = exam.get_questions().prefetch_related("answers") + questions_qs = exam.get_questions().prefetch_related("answers") - # We could prefect the UserAnswers.answers here (if we didn't cache them) - cid_user_answers = self.UserAnswer.objects.select_related("question").filter( - question__in=questions, exam__id=pk + # Materialize questions into a list so we can index and build a question->index map + questions = list(questions_qs) + question_index_map = {q: i for i, q in enumerate(questions)} + + # We could prefetch the UserAnswers.answers here (if we didn't cache them) + # Also select_related the user to avoid per-answer user lookups in the loop + cid_user_answers = ( + self.UserAnswer.objects.select_related("question", "user") + .filter(question__in=questions, exam__id=pk) ) - question_number = questions.count() + question_number = len(questions) cids = set() @@ -2869,8 +2875,10 @@ class ExamViews(View, LoginRequiredMixin): answer_score = s.get_answer_score() if answer_score == "unmarked": - index = exam.get_question_index(q) - unmarked.add(index) + # Use precomputed map to avoid repeated scanning/indexing of questions + index = question_index_map.get(q) + if index is not None: + unmarked.add(index) # user_answers[cid].append(ans) user_answers_marks[cid].append(answer_score)