aboutsummaryrefslogtreecommitdiffstats
path: root/backend/models/question.py
blob: 9829843e646158a3021bf9a962ee701164db9d29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import typing as t

from pydantic import BaseModel, Field, root_validator, validator

from backend.constants import QUESTION_TYPES, REQUIRED_QUESTION_TYPE_DATA

_TESTS_TYPE = t.Union[t.Dict[str, str], int]


class Unittests(BaseModel):
    allow_failure: bool = False
    tests: _TESTS_TYPE

    @validator("tests")
    def validate_tests(cls, value: _TESTS_TYPE) -> _TESTS_TYPE:
        if isinstance(value, dict) and not len(value.keys()):
            raise ValueError("Must have at least one test in a test suite.")

        return value


class CodeQuestion(BaseModel):
    language: str
    unittests: t.Optional[Unittests]


class Question(BaseModel):
    """Schema model for form question."""

    id: str = Field(alias="_id")
    name: str
    type: str
    data: dict[str, t.Any]
    required: bool

    class Config:
        allow_population_by_field_name = True

    @validator("type", pre=True)
    def validate_question_type(cls, value: str) -> t.Optional[str]:
        """Checks if question type in currently allowed types list."""
        value = value.lower()
        if value not in QUESTION_TYPES:
            raise ValueError(
                f"{value} is not valid question type. "
                f"Allowed question types: {QUESTION_TYPES}."
            )

        return value

    @root_validator
    def validate_question_data(
            cls,
            value: dict[str, t.Any]
    ) -> t.Optional[dict[str, t.Any]]:
        """Check does required data exists for question type and remove other data."""
        # When question type don't need data, don't add anything to keep DB clean.
        if value.get("type") not in REQUIRED_QUESTION_TYPE_DATA:
            return value

        for key, data_type in REQUIRED_QUESTION_TYPE_DATA[value["type"]].items():
            if key not in value.get("data", {}):
                raise ValueError(f"Required question data key '{key}' not provided.")

            if not isinstance(value["data"][key], data_type):
                raise ValueError(
                    f"Question data key '{key}' expects {data_type.__name__}, "
                    f"got {type(value['data'][key]).__name__} instead."
                )

            # Validate unittest options
            if value.get("type").lower() == "code":
                value["data"] = CodeQuestion(**value.get("data")).dict()

        return value