From f5d5b299b88391edddce403496a3b3e8b775f54f Mon Sep 17 00:00:00 2001 From: InitialMoon Date: Sat, 24 Aug 2024 13:27:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E6=93=8D=E4=BD=9C=E6=97=B6=E4=BA=A7=E7=94=9F?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=A9=BA=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/quiz/views.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/contest/quiz/views.py b/contest/quiz/views.py index 17956b1..612cdfe 100644 --- a/contest/quiz/views.py +++ b/contest/quiz/views.py @@ -255,28 +255,30 @@ def contest_submit(request: AuthenticatedHttpRequest) -> HttpResponse: # 从 Redis 获取现有的答案缓存 cached_answers = cache.get(cache_key, {}) - for question_id, choice_id in cached_answers.items(): - # Filter out tokens - if not question_id.startswith("question-"): - continue - - if not isinstance(choice_id, str) or not choice_id.startswith("choice-"): - return HttpResponseBadRequest( - f"Invalid choice ID “{choice_id}” for “{question_id}”." - ) + if cached_answers is not None: - answer: DraftAnswer = get_object_or_404( - draft_response.answer_set, - question_id=int(question_id.removeprefix("question-")), - ) + for question_id, choice_id in cached_answers.items(): + # Filter out tokens + if not question_id.startswith("question-"): + continue - answer.choice = get_object_or_404( - Choice.objects, - pk=int(choice_id.removeprefix("choice-")), - question=answer.question, - ) + if not isinstance(choice_id, str) or not choice_id.startswith("choice-"): + return HttpResponseBadRequest( + f"Invalid choice ID “{choice_id}” for “{question_id}”." + ) + + answer: DraftAnswer = get_object_or_404( + draft_response.answer_set, + question_id=int(question_id.removeprefix("question-")), + ) + + answer.choice = get_object_or_404( + Choice.objects, + pk=int(choice_id.removeprefix("choice-")), + question=answer.question, + ) - answer.save() + answer.save() # 1. Convert from draft response, answers = student.draft_response.finalize(submit_at=timezone.now()) From 3059cf418e3842b61e2d24d0b8f6b361421f5915 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 24 Aug 2024 05:28:35 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- contest/quiz/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/contest/quiz/views.py b/contest/quiz/views.py index 612cdfe..5fe7b0f 100644 --- a/contest/quiz/views.py +++ b/contest/quiz/views.py @@ -256,7 +256,6 @@ def contest_submit(request: AuthenticatedHttpRequest) -> HttpResponse: cached_answers = cache.get(cache_key, {}) if cached_answers is not None: - for question_id, choice_id in cached_answers.items(): # Filter out tokens if not question_id.startswith("question-"):