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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: xn-model
3
- Version: 1.0.6
3
+ Version: 1.0.8
4
4
  Summary: Base model for xn-api
5
5
  Author-email: Mike Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -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 TrtModel
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 PydIn(BaseModel):
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
- _pyd: type[PydanticModel] = None # overridable
27
- _pydIn: type[PydIn | PydanticModel] = None # overridable
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 repr(self) -> str:
32
- return " ".join(getattr(self, name_fragment) for name_fragment in self._name)
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 pyd(cls):
36
- if not cls._pyd:
37
- cls._pyd = pydantic_model_creator(cls, name=cls.__name__)
38
- return cls._pyd
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 pyd_in(cls):
42
- if not cls._pydIn:
43
- cls._pydIn = pydantic_model_creator(
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._pydIn
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.pyd().from_tortoise_orm(obj)
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.pyd().from_tortoise_orm(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) -> "Model":
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: xn-model
3
- Version: 1.0.6
3
+ Version: 1.0.8
4
4
  Summary: Base model for xn-api
5
5
  Author-email: Mike Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -10,6 +10,7 @@ x_model/__init__.py
10
10
  x_model/field.py
11
11
  x_model/func.py
12
12
  x_model/models.py
13
+ x_model/types.py
13
14
  xn_model.egg-info/PKG-INFO
14
15
  xn_model.egg-info/SOURCES.txt
15
16
  xn_model.egg-info/dependency_links.txt
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