| 1 | from typing import List, Dict, Any
|
|---|
| 2 |
|
|---|
| 3 | from .questions import (
|
|---|
| 4 | RadioQuestion,
|
|---|
| 5 | CheckboxQuestion,
|
|---|
| 6 | TextQuestion,
|
|---|
| 7 | CandidateIDQuestion,
|
|---|
| 8 | InstructionBlock,
|
|---|
| 9 | Validation,
|
|---|
| 10 | AnswerOption,
|
|---|
| 11 | )
|
|---|
| 12 | from .media_items import media_factory, NullMediaItem
|
|---|
| 13 | from .id_factory import EntityType
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | def options_factory(opt_list: List[Dict[str, Any]]) -> List[AnswerOption]:
|
|---|
| 17 | return [AnswerOption(o["id"], o["text"], o.get("points", 0.0)) for o in opt_list]
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | class QuestionTypes:
|
|---|
| 21 | @staticmethod
|
|---|
| 22 | def radio(q: dict):
|
|---|
| 23 | media = [media_factory(m) for m in q.get("media", [])] or [NullMediaItem()]
|
|---|
| 24 | return RadioQuestion(
|
|---|
| 25 | domain=q.get("domain", "GEN_GENERAL"),
|
|---|
| 26 | etype=EntityType.QUESTION,
|
|---|
| 27 | text_seed=q.get("text"),
|
|---|
| 28 | topic=q.get("topic", ""),
|
|---|
| 29 | options=options_factory(q["options"]),
|
|---|
| 30 | media=media)
|
|---|
| 31 |
|
|---|
| 32 | @staticmethod
|
|---|
| 33 | def checkbox(q: dict):
|
|---|
| 34 | media = [media_factory(m) for m in q.get("media", [])] or [NullMediaItem()]
|
|---|
| 35 | return CheckboxQuestion(
|
|---|
| 36 | domain=q.get("domain", "GEN_GENERAL"),
|
|---|
| 37 | etype=EntityType.QUESTION,
|
|---|
| 38 | text_seed=q.get("text"),
|
|---|
| 39 | topic=q.get("topic", ""),
|
|---|
| 40 | options=options_factory(q["options"]),
|
|---|
| 41 | media=media)
|
|---|
| 42 |
|
|---|
| 43 | @staticmethod
|
|---|
| 44 | def text(q: dict):
|
|---|
| 45 | media = [media_factory(m) for m in q.get("media", [])] or [NullMediaItem()]
|
|---|
| 46 | val = Validation(**q["validation"]) if "validation" in q else None
|
|---|
| 47 | return TextQuestion(
|
|---|
| 48 | domain=q.get("domain", "GEN_GENERAL"),
|
|---|
| 49 | etype=EntityType.QUESTION,
|
|---|
| 50 | text_seed=q.get("text", ""),
|
|---|
| 51 | topic=q.get("topic", ""),
|
|---|
| 52 | validation=val,
|
|---|
| 53 | media=media)
|
|---|
| 54 |
|
|---|
| 55 | @staticmethod
|
|---|
| 56 | def candidate_id(q: dict):
|
|---|
| 57 | return CandidateIDQuestion(q.get("domain", "IDENT_IDENT"), q.get("topic", ""), q["id"], q["text"], q["fields"])
|
|---|
| 58 |
|
|---|
| 59 | @staticmethod
|
|---|
| 60 | def instruction(q: dict):
|
|---|
| 61 | media = [media_factory(m) for m in q.get("media", [])] or [NullMediaItem()]
|
|---|
| 62 | return InstructionBlock(
|
|---|
| 63 | domain=q.get("domain", "INFO_INFO"),
|
|---|
| 64 | etype=EntityType.QUESTION,
|
|---|
| 65 | text_seed=q.get("text", ""),
|
|---|
| 66 | topic=q.get("topic", ""),
|
|---|
| 67 | media=media,
|
|---|
| 68 | )
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | def question_factory(q: dict):
|
|---|
| 72 | qtype = q["qtype"]
|
|---|
| 73 | try:
|
|---|
| 74 | return getattr(QuestionTypes, qtype)(q)
|
|---|
| 75 | except AttributeError as e:
|
|---|
| 76 | raise ValueError(f"Unknown question type: {qtype}") from e
|
|---|