Skip to content

Commit

Permalink
refactor: use updated questionpy-common API
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Mar 13, 2024
1 parent 8307672 commit b9fd619
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
18 changes: 9 additions & 9 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ python = "^3.9"
aiohttp = "^3.8.1"
# 2.6.0 breaks us because of <https://github.com/pydantic/pydantic/issues/8697>.
pydantic = "^2.4, !=2.6.0"
questionpy-common = { git = "https://github.com/questionpy-org/questionpy-common.git", rev = "cdc3bad516df137651e14d81fb6d1bfde11ceaaf" }
questionpy-common = { git = "https://github.com/questionpy-org/questionpy-common.git", rev = "6f47fc205c767564baaee914e064f9ba152bae09" }
polyfactory = "^2.7.2"
pydantic-settings = "^2.0.2"
watchdog = "^3.0.0"
Expand Down
30 changes: 23 additions & 7 deletions questionpy_server/worker/runtime/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dataclasses import dataclass
from typing import Optional, Callable, Union, Literal, Any, Generator

from questionpy_common.api.attempt import AttemptScoredModel
from questionpy_common.api.qtype import BaseQuestionType
from questionpy_common.environment import Environment, RequestUser, WorkerResourceLimits, OnRequestCallback, \
get_qpy_environment
Expand Down Expand Up @@ -114,7 +115,10 @@ def on_msg_get_options_form_definition(self, msg: GetOptionsForm) -> MessageToSe
assert self.question_type

with self._with_request_user(msg.request_user):
definition, form_data = self.question_type.get_options_form(msg.question_state)
old_question = None
if msg.question_state:
old_question = self.question_type.create_question_from_state(msg.question_state)
definition, form_data = self.question_type.get_options_form(old_question)

return GetOptionsForm.Response(definition=definition, form_data=form_data)

Expand All @@ -124,10 +128,15 @@ def on_msg_create_question_from_options(self, msg: CreateQuestionFromOptions) ->
assert self.question_type

with self._with_request_user(msg.request_user):
question = self.question_type.create_question_from_options(msg.question_state, msg.form_data)
old_question = None
if msg.question_state:
old_question = self.question_type.create_question_from_state(msg.question_state)
question = self.question_type.create_question_from_options(msg.form_data, old_question)

return CreateQuestionFromOptions.Response(question_state=question.export_question_state(),
question_model=question.export())
return CreateQuestionFromOptions.Response(
question_state=question.export_question_state(),
question_model=question.export()
)

def on_msg_start_attempt(self, msg: StartAttempt) -> StartAttempt.Response:
self._require_init(msg)
Expand All @@ -137,7 +146,8 @@ def on_msg_start_attempt(self, msg: StartAttempt) -> StartAttempt.Response:
with self._with_request_user(msg.request_user):
question = self.question_type.create_question_from_state(msg.question_state)
attempt = question.start_attempt(msg.variant)
return StartAttempt.Response(attempt_state=attempt.export_attempt_state(), attempt_model=attempt.export())
return StartAttempt.Response(attempt_state=attempt.export_attempt_state(),
attempt_model=attempt.export())

def on_msg_view_attempt(self, msg: ViewAttempt) -> ViewAttempt.Response:
self._require_init(msg)
Expand All @@ -163,8 +173,14 @@ def on_msg_score_attempt(self, msg: ScoreAttempt) -> ScoreAttempt.Response:
question = self.question_type.create_question_from_state(msg.question_state)
attempt = question.get_attempt(msg.attempt_state, msg.scoring_state, msg.response,
compute_score=True, generate_hint=False)
scored_model = attempt.export_scored_attempt()
return ScoreAttempt.Response(attempt_scored_model=scored_model)
score = attempt.score_response()
scoring_state = score.export_scoring_state()
model = AttemptScoredModel(
scoring_state=scoring_state,
scoring_code=score.code, score=score.fraction, classification=score.classification,
**attempt.export().model_dump()
)
return ScoreAttempt.Response(attempt_scored_model=model)

def _require_init(self, msg: MessageToWorker) -> None:
if not self.worker_type:
Expand Down
14 changes: 13 additions & 1 deletion questionpy_server/worker/worker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,16 @@ async def get_attempt(self, *, request_user: RequestUser,
async def score_attempt(self, *, request_user: RequestUser,
question_state: str, attempt_state: str, scoring_state: Optional[str] = None,
response: dict) -> AttemptScoredModel:
""""""
"""Score an attempt, i.e. score the given response.
Args:
request_user: Information on the user this request is for.
question_state: The question the attempt belongs to.
attempt_state: The `attempt_state` attribute of an attempt which was previously returned by
:meth:`start_attempt`.
scoring_state: If the attempt has already been scored before, that scoring state.
response: The response currently entered by the student.
Returns:
Metadata of the scored attempt.
"""
Binary file modified tests/test_data/package/package_1.qpy
Binary file not shown.
Binary file modified tests/test_data/package/package_2.qpy
Binary file not shown.

0 comments on commit b9fd619

Please sign in to comment.