xn-model 1.0.3__tar.gz → 1.0.5__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.3
3
+ Version: 1.0.5
4
4
  Summary: Base model for xn-api
5
5
  Author-email: Mike Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -0,0 +1,12 @@
1
+ from types import ModuleType
2
+
3
+ from tortoise import Tortoise, connections
4
+ from tortoise.backends.asyncpg import AsyncpgDBClient
5
+
6
+
7
+ async def init_db(dsn: str, models: ModuleType, create_tables: bool = True) -> AsyncpgDBClient | str:
8
+ await Tortoise.init(db_url=dsn, modules={"models": [models]})
9
+ if create_tables:
10
+ await Tortoise.generate_schemas()
11
+ cn: AsyncpgDBClient = connections.get("default")
12
+ return cn
@@ -1,7 +1,7 @@
1
1
  from enum import IntEnum
2
2
  from typing import Any
3
3
 
4
- from asyncpg import Point, Polygon, Range, Box
4
+ from asyncpg import Range # Box, Point, Polygon,
5
5
  from tortoise import Model
6
6
  from tortoise.contrib.postgres.fields import ArrayField
7
7
  from tortoise.fields import Field, SmallIntField, IntField, FloatField, DatetimeField
@@ -51,23 +51,23 @@ class RangeField(CollectionField[Range]):
51
51
  return value
52
52
 
53
53
 
54
- class PointField(CollectionField[Point]):
55
- SQL_TYPE = "POINT"
56
- field_type = Point
57
- base_field = FloatField
58
- labels = ("lat", "lon")
59
-
60
-
61
- class PolygonField(ListField[Polygon]):
62
- SQL_TYPE = "POLYGON"
63
- field_type = Polygon
64
- base_field = PointField
65
-
66
-
67
- class BoxField(ListField[Box]):
68
- SQL_TYPE = "BOX"
69
- field_type = Box
70
- base_field = PointField
54
+ # class PointField(CollectionField[Point]):
55
+ # SQL_TYPE = "POINT"
56
+ # field_type = Point
57
+ # base_field = FloatField
58
+ # labels = ("lat", "lon")
59
+ #
60
+ #
61
+ # class PolygonField(ListField[Polygon]):
62
+ # SQL_TYPE = "POLYGON"
63
+ # field_type = Polygon
64
+ # base_field = PointField
65
+ #
66
+ #
67
+ # class BoxField(ListField[Box]):
68
+ # SQL_TYPE = "BOX"
69
+ # field_type = Box
70
+ # base_field = PointField
71
71
 
72
72
 
73
73
  class DatetimeSecField(DatetimeField):
@@ -1,16 +1,10 @@
1
1
  from datetime import datetime
2
-
3
- from pydantic import ConfigDict
4
- from tortoise import Model as BaseModel
2
+ from pydantic import ConfigDict, BaseModel
3
+ from tortoise import Model as TrtModel
5
4
  from tortoise.contrib.pydantic import pydantic_model_creator, PydanticModel
6
- from tortoise.fields import DatetimeField, IntField
7
-
8
- from x_model import HTTPException, FailReason
5
+ from tortoise.fields import IntField
9
6
 
10
-
11
- class DatetimeSecField(DatetimeField):
12
- class _db_postgres:
13
- SQL_TYPE = "TIMESTAMPTZ(0)"
7
+ from x_model.field import DatetimeSecField
14
8
 
15
9
 
16
10
  class TsTrait:
@@ -18,10 +12,19 @@ class TsTrait:
18
12
  updated_at: datetime | None = DatetimeSecField(auto_now=True)
19
13
 
20
14
 
21
- class Model(BaseModel):
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):
22
24
  id: int = IntField(True)
23
25
 
24
- _pyd: type[PydanticModel] = None
26
+ _pyd: type[PydanticModel] = None # overridable
27
+ _pydIn: type[PydIn | PydanticModel] = None # overridable
25
28
  _name: tuple[str] = ("name",)
26
29
  _sorts: tuple[str] = ("-id",)
27
30
 
@@ -34,12 +37,16 @@ class Model(BaseModel):
34
37
  cls._pyd = pydantic_model_creator(cls, name=cls.__name__)
35
38
  return cls._pyd
36
39
 
37
- # # # CRUD Methods # # #
38
40
  @classmethod
39
- async def get_one(cls, id_: int, **filters) -> PydanticModel:
40
- if obj := await cls.get_or_none(id=id_, **filters):
41
- return await cls.pyd().from_tortoise_orm(obj)
42
- raise HTTPException(reason=FailReason.path, status_=404, parent=f"{cls.__name__}#{id_} not found")
41
+ def pyd_in(cls):
42
+ if not cls._pydIn:
43
+ cls._pydIn = pydantic_model_creator(
44
+ cls, name=cls.__name__ + "In", exclude_readonly=True, meta_override=cls.PydanticMetaIn
45
+ )
46
+ return cls._pydIn
47
+
48
+ async def one(self) -> PydanticModel:
49
+ return await self.pyd().from_tortoise_orm(self)
43
50
 
44
51
  @classmethod
45
52
  async def get_or_create_by_name(cls, name: str, attr_name: str = None, def_dict: dict = None) -> "Model":
@@ -55,11 +62,16 @@ class Model(BaseModel):
55
62
  # exclude: tuple[str, ...] = ("Meta",)
56
63
  # computed: tuple[str, ...] = ()
57
64
  # backward_relations: bool = True
58
- max_recursion: int = 0 # default: 3
65
+ max_recursion: int = 1 # default: 3
59
66
  # allow_cycles: bool = False
60
67
  # exclude_raw_fields: bool = True
61
68
  # sort_alphabetically: bool = False
62
69
  # model_config: ConfigDict | None = None
63
70
 
71
+ class PydanticMetaIn:
72
+ backward_relations: bool = False
73
+ max_recursion: int = 0
74
+ exclude_raw_fields: bool = False
75
+
64
76
  class Meta:
65
77
  abstract = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: xn-model
3
- Version: 1.0.3
3
+ Version: 1.0.5
4
4
  Summary: Base model for xn-api
5
5
  Author-email: Mike Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -10,7 +10,6 @@ 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/pydantic.py
14
13
  xn_model.egg-info/PKG-INFO
15
14
  xn_model.egg-info/SOURCES.txt
16
15
  xn_model.egg-info/dependency_links.txt
@@ -1,36 +0,0 @@
1
- import logging
2
- from enum import IntEnum
3
- from types import ModuleType
4
-
5
- from tortoise import Tortoise, connections
6
- from tortoise.backends.asyncpg import AsyncpgDBClient
7
-
8
-
9
- async def init_db(dsn: str, models: ModuleType, create_tables: bool = False) -> AsyncpgDBClient | str:
10
- await Tortoise.init(db_url=dsn, modules={"models": [models]})
11
- if create_tables:
12
- await Tortoise.generate_schemas()
13
- cn: AsyncpgDBClient = connections.get("default")
14
- return cn
15
-
16
-
17
- class FailReason(IntEnum):
18
- body = 8
19
- query = 9
20
- path = 10
21
- host = 11
22
- protocol = 12
23
- method = 13
24
-
25
-
26
- class HTTPException(Exception):
27
- def __init__(
28
- self,
29
- reason: IntEnum,
30
- parent: Exception | str = None,
31
- status_: int = 400,
32
- hdrs: dict = None,
33
- ) -> None:
34
- detail = f"{reason.name}{f': {parent}' if parent else ''}"
35
- logging.error(detail)
36
- super().__init__(status_, detail, hdrs)
@@ -1,16 +0,0 @@
1
- from pydantic import BaseModel
2
-
3
-
4
- class Names(BaseModel):
5
- # models for name endpoint for select2 inputs
6
- class Name(BaseModel):
7
- id: int
8
- text: str
9
- logo: str | None = None
10
- selected: bool | None = None
11
-
12
- class Pagination(BaseModel):
13
- more: bool
14
-
15
- results: list[Name]
16
- pagination: Pagination
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