-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathattempt.py
84 lines (70 loc) · 2.98 KB
/
attempt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This file is part of the QuestionPy SDK. (https://questionpy.org)
# The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md.
# (c) Technische Universität Berlin, innoCampus <[email protected]>
from typing import Literal, TypedDict
from questionpy_common.api.attempt import AttemptModel, AttemptScoredModel, AttemptStartedModel
from questionpy_sdk.webserver.question_ui import (
QuestionDisplayOptions,
QuestionFormulationUIRenderer,
QuestionUIRenderer,
)
from questionpy_sdk.webserver.question_ui.errors import RenderErrorCollections, log_render_errors
class _AttemptRenderContext(TypedDict):
attempt_status: Literal["Started", "In progress", "Scored"]
attempt: AttemptModel
attempt_state: str
options: dict
form_disabled: bool
formulation: str
general_feedback: str | None
specific_feedback: str | None
right_answer: str | None
render_errors: RenderErrorCollections
def get_attempt_render_context(
attempt: AttemptModel,
attempt_state: str,
*,
last_attempt_data: dict,
display_options: QuestionDisplayOptions,
seed: int,
disabled: bool,
) -> _AttemptRenderContext:
renderer_args = (attempt.ui.placeholders, display_options, seed, last_attempt_data)
html, errors = QuestionFormulationUIRenderer(attempt.ui.formulation, *renderer_args).render()
context: _AttemptRenderContext = {
"attempt_status": (
"Started"
if isinstance(attempt, AttemptStartedModel)
else "Scored"
if isinstance(attempt, AttemptScoredModel)
else "In progress"
),
"attempt_state": attempt_state,
"options": display_options.model_dump(include={"general_feedback", "feedback", "right_answer"}),
"form_disabled": disabled,
"formulation": html,
"attempt": attempt,
"general_feedback": None,
"specific_feedback": None,
"right_answer": None,
"render_errors": {},
}
if errors:
context["render_errors"]["Formulation"] = errors
if display_options.general_feedback and attempt.ui.general_feedback:
html, errors = QuestionUIRenderer(attempt.ui.general_feedback, *renderer_args).render()
context["general_feedback"] = html
if errors:
context["render_errors"]["General Feedback"] = errors
if display_options.specific_feedback and attempt.ui.specific_feedback:
html, errors = QuestionUIRenderer(attempt.ui.specific_feedback, *renderer_args).render()
context["specific_feedback"] = html
if errors:
context["render_errors"]["Specific Feedback"] = errors
if display_options.right_answer and attempt.ui.right_answer:
html, errors = QuestionUIRenderer(attempt.ui.right_answer, *renderer_args).render()
context["right_answer"] = html
if errors:
context["render_errors"]["Right Answer"] = errors
log_render_errors(context["render_errors"])
return context