from typing import Dict, Any from pydantic import ValidationError from app.dtos.exams.level import ( MultipleChoiceExercise, FillBlanksExercise, Part, Exam, Text ) from app.dtos.sheet import Sheet, Option, MultipleChoiceQuestion, FillBlanksWord class LevelMapper: @staticmethod def map_to_exam_model(response: Dict[str, Any]) -> Exam: parts = [] for part in response['parts']: part_exercises = part['exercises'] text = part.get('text', 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 text is not None and text.get('content', None): title = text.get('title', 'Untitled') if title == '': title = 'Untitled' part_kwargs["text"] = Text(title=title, content=text['content']) else: part_kwargs["text"] = None 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)