67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
from typing import Dict, Any
|
|
|
|
from pydantic import ValidationError
|
|
|
|
from app.dtos.exam import (
|
|
MultipleChoiceExercise,
|
|
FillBlanksExercise,
|
|
Part, Exam
|
|
)
|
|
from app.dtos.sheet import Sheet, Option, MultipleChoiceQuestion, FillBlanksWord
|
|
|
|
|
|
class ExamMapper:
|
|
|
|
@staticmethod
|
|
def map_to_exam_model(response: Dict[str, Any]) -> Exam:
|
|
parts = []
|
|
for part in response['parts']:
|
|
part_exercises = part['exercises']
|
|
context = part.get('context', None)
|
|
|
|
exercises = []
|
|
for exercise in part_exercises:
|
|
exercise_type = exercise['type']
|
|
if exercise_type == 'multipleChoice':
|
|
exercise_model = MultipleChoiceExercise(**exercise)
|
|
elif exercise_type == 'fillBlanks':
|
|
exercise_model = FillBlanksExercise(**exercise)
|
|
else:
|
|
raise ValidationError(f"Unknown exercise type: {exercise_type}")
|
|
|
|
exercises.append(exercise_model)
|
|
|
|
part_kwargs = {"exercises": exercises}
|
|
if context is not None:
|
|
part_kwargs["context"] = context
|
|
|
|
part_model = Part(**part_kwargs)
|
|
parts.append(part_model)
|
|
|
|
return Exam(parts=parts)
|
|
|
|
@staticmethod
|
|
def map_to_sheet(response: Dict[str, Any]) -> Sheet:
|
|
components = []
|
|
|
|
for item in response["components"]:
|
|
component_type = item["type"]
|
|
|
|
if component_type == "multipleChoice":
|
|
options = [Option(id=opt["id"], text=opt["text"]) for opt in item["options"]]
|
|
components.append(MultipleChoiceQuestion(
|
|
id=item["id"],
|
|
prompt=item["prompt"],
|
|
variant=item.get("variant", "text"),
|
|
options=options
|
|
))
|
|
elif component_type == "fillBlanks":
|
|
components.append(FillBlanksWord(
|
|
id=item["id"],
|
|
options=item["options"]
|
|
))
|
|
else:
|
|
components.append(item)
|
|
|
|
return Sheet(components=components)
|