xn-model 1.0.3__tar.gz → 1.0.4__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.3/xn_model.egg-info → xn_model-1.0.4}/PKG-INFO +1 -1
- xn_model-1.0.4/x_model/__init__.py +12 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/x_model/field.py +18 -18
- {xn_model-1.0.3 → xn_model-1.0.4}/x_model/models.py +19 -10
- {xn_model-1.0.3 → xn_model-1.0.4/xn_model.egg-info}/PKG-INFO +1 -1
- {xn_model-1.0.3 → xn_model-1.0.4}/xn_model.egg-info/SOURCES.txt +0 -1
- xn_model-1.0.3/x_model/__init__.py +0 -36
- xn_model-1.0.3/x_model/pydantic.py +0 -16
- {xn_model-1.0.3 → xn_model-1.0.4}/.env.sample +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/.gitignore +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/.pre-commit-config.yaml +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/README.md +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/makefile +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/pyproject.toml +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/setup.cfg +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/tests/__init__.py +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/tests/test_db.py +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/x_model/func.py +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/xn_model.egg-info/dependency_links.txt +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/xn_model.egg-info/requires.txt +0 -0
- {xn_model-1.0.3 → xn_model-1.0.4}/xn_model.egg-info/top_level.txt +0 -0
|
@@ -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,
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
class PolygonField(ListField[Polygon]):
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
class BoxField(ListField[Box]):
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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):
|
|
@@ -3,14 +3,9 @@ from datetime import datetime
|
|
|
3
3
|
from pydantic import ConfigDict
|
|
4
4
|
from tortoise import Model as BaseModel
|
|
5
5
|
from tortoise.contrib.pydantic import pydantic_model_creator, PydanticModel
|
|
6
|
-
from tortoise.fields import
|
|
6
|
+
from tortoise.fields import IntField
|
|
7
7
|
|
|
8
|
-
from x_model import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class DatetimeSecField(DatetimeField):
|
|
12
|
-
class _db_postgres:
|
|
13
|
-
SQL_TYPE = "TIMESTAMPTZ(0)"
|
|
8
|
+
from x_model.field import DatetimeSecField
|
|
14
9
|
|
|
15
10
|
|
|
16
11
|
class TsTrait:
|
|
@@ -21,7 +16,8 @@ class TsTrait:
|
|
|
21
16
|
class Model(BaseModel):
|
|
22
17
|
id: int = IntField(True)
|
|
23
18
|
|
|
24
|
-
_pyd: type[PydanticModel] = None
|
|
19
|
+
_pyd: type[PydanticModel] = None # overridable
|
|
20
|
+
_pydIn: type[PydanticModel] = None # overridable
|
|
25
21
|
_name: tuple[str] = ("name",)
|
|
26
22
|
_sorts: tuple[str] = ("-id",)
|
|
27
23
|
|
|
@@ -34,12 +30,20 @@ class Model(BaseModel):
|
|
|
34
30
|
cls._pyd = pydantic_model_creator(cls, name=cls.__name__)
|
|
35
31
|
return cls._pyd
|
|
36
32
|
|
|
33
|
+
@classmethod
|
|
34
|
+
def pyd_in(cls):
|
|
35
|
+
if not cls._pydIn:
|
|
36
|
+
cls._pydIn = pydantic_model_creator(
|
|
37
|
+
cls, name=cls.__name__ + "In", exclude_readonly=True, meta_override=cls.PydanticMetaIn
|
|
38
|
+
)
|
|
39
|
+
return cls._pydIn
|
|
40
|
+
|
|
37
41
|
# # # CRUD Methods # # #
|
|
38
42
|
@classmethod
|
|
39
43
|
async def get_one(cls, id_: int, **filters) -> PydanticModel:
|
|
40
44
|
if obj := await cls.get_or_none(id=id_, **filters):
|
|
41
45
|
return await cls.pyd().from_tortoise_orm(obj)
|
|
42
|
-
raise
|
|
46
|
+
raise LookupError(f"{cls.__name__}#{id_} not found")
|
|
43
47
|
|
|
44
48
|
@classmethod
|
|
45
49
|
async def get_or_create_by_name(cls, name: str, attr_name: str = None, def_dict: dict = None) -> "Model":
|
|
@@ -55,11 +59,16 @@ class Model(BaseModel):
|
|
|
55
59
|
# exclude: tuple[str, ...] = ("Meta",)
|
|
56
60
|
# computed: tuple[str, ...] = ()
|
|
57
61
|
# backward_relations: bool = True
|
|
58
|
-
max_recursion: int =
|
|
62
|
+
max_recursion: int = 1 # default: 3
|
|
59
63
|
# allow_cycles: bool = False
|
|
60
64
|
# exclude_raw_fields: bool = True
|
|
61
65
|
# sort_alphabetically: bool = False
|
|
62
66
|
# model_config: ConfigDict | None = None
|
|
63
67
|
|
|
68
|
+
class PydanticMetaIn:
|
|
69
|
+
backward_relations: bool = False
|
|
70
|
+
max_recursion: int = 0
|
|
71
|
+
exclude_raw_fields: bool = False
|
|
72
|
+
|
|
64
73
|
class Meta:
|
|
65
74
|
abstract = True
|
|
@@ -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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|