aboutsummaryrefslogtreecommitdiff
path: root/generalresearch/models/thl/profiling/question.py
blob: 6f6b2704c3483368b2167d5c653ec1e97dce9763 (plain)
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
from __future__ import annotations

from typing import Optional, Dict, Tuple

from pydantic import (
    BaseModel,
    Field,
    ConfigDict,
    computed_field,
)

from generalresearch.models.custom_types import (
    UUIDStr,
    AwareDatetimeISO,
    CountryISOLike,
    LanguageISOLike,
)
from generalresearch.models.thl.profiling.upk_question import UpkQuestion


class Question(BaseModel):
    model_config = ConfigDict(validate_assignment=True)

    id: Optional[UUIDStr] = Field(default=None, alias="question_id")
    # ISO 3166-1 alpha-2 (two-letter codes, lowercase)
    country_iso: CountryISOLike = Field()
    # 3-char ISO 639-2/B, lowercase
    language_iso: LanguageISOLike = Field()

    property_code: Optional[str] = Field(
        default=None,
        description="What marketplace question this question links to",
        pattern=r"^[a-z]{1,2}\:.*",
    )
    data: UpkQuestion = Field()
    is_live: bool = Field()
    custom: Dict = Field(default_factory=dict)
    last_updated: AwareDatetimeISO = Field()

    @computed_field
    @property
    def md5sum(self) -> str:
        return self.data.md5sum

    def validate_question_answer(self, answer: Tuple[str, ...]) -> Tuple[bool, str]:
        return self.data.validate_question_answer(answer=answer)