aboutsummaryrefslogtreecommitdiffstats
path: root/backend/models/form.py
blob: 9d8ffaa5111c1faa7725b0ba35cdc4b490c8fc2d (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
import typing as t

from pydantic import BaseModel, Field, validator

from backend.constants import FormFeatures
from .question import Question

PUBLIC_FIELDS = ["id", "features", "questions", "name", "description"]


class Form(BaseModel):
    """Schema model for form."""

    id: str = Field(alias="_id")
    features: list[str]
    questions: list[Question]
    name: str
    description: str

    class Config:
        allow_population_by_field_name = True

    @validator("features")
    def validate_features(cls, value: list[str]) -> t.Optional[list[str]]:
        """Validates is all features in allowed list."""
        # Uppercase everything to avoid mixed case in DB
        value = [v.upper() for v in value]
        allowed_values = [v.value for v in FormFeatures.__members__.values()]
        if any(v not in allowed_values for v in value):
            raise ValueError("Form features list contains one or more invalid values.")

        if FormFeatures.COLLECT_EMAIL in value and FormFeatures.REQUIRES_LOGIN not in value:  # noqa
            raise ValueError("COLLECT_EMAIL feature require REQUIRES_LOGIN feature.")

        return value

    def dict(self, admin: bool = True, **kwargs: t.Any) -> dict[str, t.Any]:
        """Wrapper for original function to exclude private data for public access."""
        data = super().dict(**kwargs)

        returned_data = {}

        if not admin:
            for field in PUBLIC_FIELDS:
                if field == "id" and kwargs.get("by_alias"):
                    fetch_field = "_id"
                else:
                    fetch_field = field

                returned_data[field] = data[fetch_field]
        else:
            returned_data = data

        return returned_data


class FormList(BaseModel):
    __root__: t.List[Form]