|
2 | 2 | This module is to be imported in the dynamically created and updated entity.py module. |
3 | 3 | """ |
4 | 4 |
|
5 | | -from typing import TYPE_CHECKING, Type, TypeVar, Union |
| 5 | +from typing import TYPE_CHECKING, Literal, Optional, Type, TypeVar, Union |
6 | 6 | from uuid import UUID |
7 | 7 |
|
8 | | -from pydantic.v1 import BaseModel |
| 8 | +from pydantic.v1 import BaseModel, Field, constr |
9 | 9 |
|
10 | 10 | from osw.custom_types import NoneType |
| 11 | +from osw.utils.strings import pascal_case |
11 | 12 |
|
12 | 13 | T = TypeVar("T", bound=BaseModel) |
13 | 14 |
|
@@ -88,6 +89,31 @@ class Config: |
88 | 89 | validate_assignment = True |
89 | 90 | # Ensures that the assignment of a value to a field is validated |
90 | 91 |
|
| 92 | + def __init__(self, **data): |
| 93 | + if data.get("label"): |
| 94 | + if not isinstance(data["label"], list): |
| 95 | + raise ValueError( |
| 96 | + "label must be a list of Label objects", |
| 97 | + ) |
| 98 | + labels = [] |
| 99 | + for label in data["label"]: |
| 100 | + if isinstance(label, dict): |
| 101 | + labels.append(Label(**label)) |
| 102 | + else: |
| 103 | + # The list element should be a Label object |
| 104 | + labels.append(label) |
| 105 | + data["label"] = labels |
| 106 | + # Ensure that the label attribute is a list of Label objects, but use |
| 107 | + # custom_isinstance to avoid circular imports and ValidationError since |
| 108 | + # osw.model.entity defines its own Label class |
| 109 | + if not all(custom_isinstance(label, "Label") for label in data["label"]): |
| 110 | + raise ValueError( |
| 111 | + "label must be a list of Label objects", |
| 112 | + ) |
| 113 | + if data.get("name") is None and "label" in data: |
| 114 | + data["name"] = pascal_case(data["label"][0].text) |
| 115 | + super().__init__(**data) |
| 116 | + |
91 | 117 | def full_dict(self, **kwargs): # extent BaseClass export function |
92 | 118 | d = super().dict(**kwargs) |
93 | 119 | for key in ("_osl_template", "_osl_footer"): |
@@ -300,3 +326,8 @@ class Ontology(OswBaseModel): |
300 | 326 | name: str |
301 | 327 | prefix_name: str |
302 | 328 | link: str |
| 329 | + |
| 330 | + |
| 331 | +class Label(OswBaseModel): |
| 332 | + text: constr(min_length=1) = Field(..., title="Text") |
| 333 | + lang: Optional[Literal["en", "de"]] = Field("en", title="Lang code") |
0 commit comments