From fd78ec40113f291652a2443590ad0cb6e06ab408 Mon Sep 17 00:00:00 2001 From: InitialMoon Date: Sat, 24 Aug 2024 13:27:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B2=A1=E6=9C=89=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E6=93=8D=E4=BD=9C=E6=97=B6=E4=BA=A7=E7=94=9F=E7=BC=93?= =?UTF-8?q?=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/contest/settings.py | 3 ++- contest/quiz/tests.py | 1 + contest/quiz/views.py | 39 +++++++++++++++++++------------------ 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/contest/contest/settings.py b/contest/contest/settings.py index 0350292..aba3542 100644 --- a/contest/contest/settings.py +++ b/contest/contest/settings.py @@ -256,7 +256,8 @@ def _debug_only(*args) -> tuple: } } -# 防止本地注册表出现损坏导致的MIME类型解析错误,导致后端无法处理JS文件,有MIME报错的时候可以解除注释然后强制刷新前端运行看看 +# 防止本地注册表出现损坏导致的MIME类型解析错误,导致后端无法处理JS文件 +# 有MIME报错的时候可以解除注释然后强制刷新前端运行看看 # 这个用法已经被取消,建议不要使用,而是更改注册表.js的属性 # import mimetypes diff --git a/contest/quiz/tests.py b/contest/quiz/tests.py index e1ec1f5..61e9a86 100644 --- a/contest/quiz/tests.py +++ b/contest/quiz/tests.py @@ -243,6 +243,7 @@ def test_contest_update_view(self): def test_bad_contest_update(self): """暂存非法数据 + 这里每次更新的时候没有写入后端,所以测试的话获取的应该是不对的,这里就先不要测了 """ self.client.force_login(self.user) diff --git a/contest/quiz/views.py b/contest/quiz/views.py index 17956b1..5fe7b0f 100644 --- a/contest/quiz/views.py +++ b/contest/quiz/views.py @@ -255,28 +255,29 @@ 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: + for question_id, choice_id in cached_answers.items(): + # Filter out tokens + if not question_id.startswith("question-"): + continue - answer: DraftAnswer = get_object_or_404( - draft_response.answer_set, - question_id=int(question_id.removeprefix("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.choice = get_object_or_404( - Choice.objects, - pk=int(choice_id.removeprefix("choice-")), - question=answer.question, - ) + answer: DraftAnswer = get_object_or_404( + draft_response.answer_set, + question_id=int(question_id.removeprefix("question-")), + ) - answer.save() + answer.choice = get_object_or_404( + Choice.objects, + pk=int(choice_id.removeprefix("choice-")), + question=answer.question, + ) + + answer.save() # 1. Convert from draft response, answers = student.draft_response.finalize(submit_at=timezone.now())