|
| 1 | +from enum import Enum |
| 2 | +from typing import List |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from lib.core.enums import BaseTitledEnum |
| 6 | +from lib.core.enums import ClassTypeEnum |
| 7 | +from pydantic import BaseModel as BasePydanticModel |
| 8 | +from pydantic import Extra |
| 9 | +from pydantic import StrictInt |
| 10 | +from pydantic import StrictStr |
| 11 | +from pydantic import validator |
| 12 | +from pydantic.color import Color |
| 13 | +from pydantic.color import ColorType |
| 14 | + |
| 15 | + |
| 16 | +DATE_REGEX = r"\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d{3})Z" |
| 17 | +DATE_TIME_FORMAT_ERROR_MESSAGE = ( |
| 18 | + "does not match expected format YYYY-MM-DDTHH:MM:SS.fffZ" |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class HexColor(BasePydanticModel): |
| 23 | + __root__: ColorType |
| 24 | + |
| 25 | + @validator("__root__") |
| 26 | + def validate_color(cls, v): |
| 27 | + return "#{:02X}{:02X}{:02X}".format(*Color(v).as_rgb_tuple()) |
| 28 | + |
| 29 | + |
| 30 | +class BaseModel(BasePydanticModel): |
| 31 | + class Config: |
| 32 | + extra = Extra.allow |
| 33 | + error_msg_templates = { |
| 34 | + "type_error.integer": "integer type expected", |
| 35 | + "type_error.string": "str type expected", |
| 36 | + "value_error.missing": "field required", |
| 37 | + } |
| 38 | + |
| 39 | + def dict(self, *args, fill_enum_values=False, **kwargs): |
| 40 | + data = super().dict(*args, **kwargs) |
| 41 | + if fill_enum_values: |
| 42 | + data = self._fill_enum_values(data) |
| 43 | + return data |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def _fill_enum_values(data: dict) -> dict: |
| 47 | + for key, val in data.items(): |
| 48 | + if isinstance(val, BaseTitledEnum): |
| 49 | + data[key] = val.__doc__ |
| 50 | + return data |
| 51 | + |
| 52 | + |
| 53 | +class GroupTypeEnum(str, Enum): |
| 54 | + RADIO = "radio" |
| 55 | + CHECKLIST = "checklist" |
| 56 | + NUMERIC = "numeric" |
| 57 | + TEXT = "text" |
| 58 | + |
| 59 | + |
| 60 | +class Attribute(BaseModel): |
| 61 | + id: Optional[StrictInt] |
| 62 | + group_id: Optional[StrictInt] |
| 63 | + project_id: Optional[StrictInt] |
| 64 | + name: StrictStr |
| 65 | + |
| 66 | + def __hash__(self): |
| 67 | + return hash(f"{self.id}{self.group_id}{self.name}") |
| 68 | + |
| 69 | + |
| 70 | +class AttributeGroup(BaseModel): |
| 71 | + id: Optional[StrictInt] |
| 72 | + group_type: Optional[GroupTypeEnum] |
| 73 | + class_id: Optional[StrictInt] |
| 74 | + name: StrictStr |
| 75 | + is_multiselect: Optional[bool] |
| 76 | + attributes: Optional[List[Attribute]] |
| 77 | + |
| 78 | + def __hash__(self): |
| 79 | + return hash(f"{self.id}{self.class_id}{self.name}") |
| 80 | + |
| 81 | + |
| 82 | +class AnnotationClassEntity(BaseModel): |
| 83 | + id: Optional[StrictInt] |
| 84 | + project_id: Optional[StrictInt] |
| 85 | + type: ClassTypeEnum = ClassTypeEnum.OBJECT |
| 86 | + name: StrictStr |
| 87 | + color: HexColor |
| 88 | + attribute_groups: List[AttributeGroup] = [] |
| 89 | + |
| 90 | + def __hash__(self): |
| 91 | + return hash(f"{self.id}{self.type}{self.name}") |
| 92 | + |
| 93 | + class Config: |
| 94 | + validate_assignment = True |
| 95 | + exclude_none = True |
0 commit comments