45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
from typing import Dict, Any
|
|
|
|
from ielts_be.dtos.exams.reading import (
|
|
Part, Exam, Context, FillBlanksExercise,
|
|
TrueFalseExercise, MatchSentencesExercise,
|
|
WriteBlanksExercise, MultipleChoice
|
|
)
|
|
|
|
|
|
class ReadingMapper:
|
|
|
|
@staticmethod
|
|
def map_to_exam_model(response: Dict[str, Any]) -> Exam:
|
|
parts = []
|
|
for part in response['parts']:
|
|
part_exercises = part['exercises']
|
|
context = Context(**part['text'])
|
|
|
|
model_map = {
|
|
'fillBlanks': FillBlanksExercise,
|
|
'trueFalse': TrueFalseExercise,
|
|
'matchSentences': MatchSentencesExercise,
|
|
'writeBlanks': WriteBlanksExercise,
|
|
'multipleChoice': MultipleChoice,
|
|
}
|
|
|
|
exercises = []
|
|
for exercise in part_exercises:
|
|
exercise_type = exercise['type']
|
|
if exercise_type in model_map:
|
|
model_class = model_map[exercise_type]
|
|
exercises.append(model_class(**exercise))
|
|
else:
|
|
raise ValueError(f"Unknown exercise type: {exercise_type}")
|
|
|
|
part_kwargs = {
|
|
"exercises": exercises,
|
|
"text": context
|
|
}
|
|
|
|
part_model = Part(**part_kwargs)
|
|
parts.append(part_model)
|
|
|
|
return Exam(parts=parts, minTimer=response["minTimer"])
|