xn-model 1.0.6__tar.gz → 1.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {xn_model-1.0.6/xn_model.egg-info → xn_model-1.0.8}/PKG-INFO +1 -1
- {xn_model-1.0.6 → xn_model-1.0.8}/x_model/models.py +19 -25
- xn_model-1.0.8/x_model/types.py +15 -0
- {xn_model-1.0.6 → xn_model-1.0.8/xn_model.egg-info}/PKG-INFO +1 -1
- {xn_model-1.0.6 → xn_model-1.0.8}/xn_model.egg-info/SOURCES.txt +1 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/.env.sample +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/.gitignore +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/.pre-commit-config.yaml +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/README.md +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/makefile +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/pyproject.toml +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/setup.cfg +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/tests/__init__.py +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/tests/test_db.py +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/x_model/__init__.py +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/x_model/field.py +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/x_model/func.py +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/xn_model.egg-info/dependency_links.txt +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/xn_model.egg-info/requires.txt +0 -0
- {xn_model-1.0.6 → xn_model-1.0.8}/xn_model.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
|
+
from typing import Self
|
|
3
|
+
|
|
2
4
|
from pydantic import ConfigDict, BaseModel
|
|
3
|
-
from tortoise import Model as
|
|
5
|
+
from tortoise import Model as TortModel
|
|
4
6
|
from tortoise.contrib.pydantic import pydantic_model_creator, PydanticModel
|
|
5
7
|
from tortoise.fields import IntField
|
|
6
8
|
|
|
@@ -12,51 +14,43 @@ class TsTrait:
|
|
|
12
14
|
updated_at: datetime | None = DatetimeSecField(auto_now=True)
|
|
13
15
|
|
|
14
16
|
|
|
15
|
-
class
|
|
16
|
-
_unq: list[str] = ["id"]
|
|
17
|
-
|
|
18
|
-
def df_unq(self) -> dict:
|
|
19
|
-
d = self.model_dump(exclude_none=True)
|
|
20
|
-
return {**{k: d.pop(k) for k in self._unq}, "defaults": d}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
class Model(TrtModel):
|
|
17
|
+
class Model(TortModel):
|
|
24
18
|
id: int = IntField(True)
|
|
25
19
|
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
_out_type: type[BaseModel] = None # overridable
|
|
21
|
+
_in_type: type[BaseModel] = None # overridable
|
|
28
22
|
_name: tuple[str] = ("name",)
|
|
29
23
|
_sorts: tuple[str] = ("-id",)
|
|
30
24
|
|
|
31
|
-
def
|
|
32
|
-
return
|
|
25
|
+
def __repr__(self, sep: str = " ") -> str:
|
|
26
|
+
return sep.join(getattr(self, name_fragment) for name_fragment in self._name)
|
|
33
27
|
|
|
34
28
|
@classmethod
|
|
35
|
-
def
|
|
36
|
-
if not cls.
|
|
37
|
-
cls.
|
|
38
|
-
return cls.
|
|
29
|
+
def out_type(cls):
|
|
30
|
+
if not cls._out_type:
|
|
31
|
+
cls._out = pydantic_model_creator(cls, name=cls.__name__ + "Out")
|
|
32
|
+
return cls._out_type
|
|
39
33
|
|
|
40
34
|
@classmethod
|
|
41
|
-
def
|
|
42
|
-
if not cls.
|
|
43
|
-
cls.
|
|
35
|
+
def in_type(cls):
|
|
36
|
+
if not cls._in_type:
|
|
37
|
+
cls._inn = pydantic_model_creator(
|
|
44
38
|
cls, name=cls.__name__ + "In", exclude_readonly=True, meta_override=cls.PydanticMetaIn
|
|
45
39
|
)
|
|
46
|
-
return cls.
|
|
40
|
+
return cls._in_type
|
|
47
41
|
|
|
48
42
|
# # # CRUD Methods # # #
|
|
49
43
|
@classmethod
|
|
50
44
|
async def get_one(cls, id_: int) -> PydanticModel:
|
|
51
45
|
if obj := await cls.get_or_none(id=id_):
|
|
52
|
-
return await cls.
|
|
46
|
+
return await cls.out_type().from_tortoise_orm(obj)
|
|
53
47
|
raise LookupError(f"{cls.__name__}#{id_} not found")
|
|
54
48
|
|
|
55
49
|
async def one(self) -> PydanticModel:
|
|
56
|
-
return await self.
|
|
50
|
+
return await self.out_type().from_tortoise_orm(self)
|
|
57
51
|
|
|
58
52
|
@classmethod
|
|
59
|
-
async def get_or_create_by_name(cls, name: str, attr_name: str = None, def_dict: dict = None) ->
|
|
53
|
+
async def get_or_create_by_name(cls, name: str, attr_name: str = None, def_dict: dict = None) -> Self:
|
|
60
54
|
attr_name = attr_name or list(cls._name)[0]
|
|
61
55
|
if not (obj := await cls.get_or_none(**{attr_name: name})):
|
|
62
56
|
next_id = (await cls.all().order_by("-id").first()).id + 1
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class New(BaseModel):
|
|
5
|
+
_unq: list[str] = []
|
|
6
|
+
|
|
7
|
+
def df_unq(self) -> dict:
|
|
8
|
+
d = self.model_dump(exclude_none=True)
|
|
9
|
+
return {**{k: d.pop(k) for k in set(self._unq) & d.keys()}, "defaults": d}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Upd(New):
|
|
13
|
+
_unq: list[str] = ["id"]
|
|
14
|
+
|
|
15
|
+
id: int
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|