xn-model 0.11.1__tar.gz → 0.11.2__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-0.11.1/xn_model.egg-info → xn_model-0.11.2}/PKG-INFO +1 -1
- xn_model-0.11.2/x_model/__init__.py +41 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/x_model/model.py +7 -2
- {xn_model-0.11.1 → xn_model-0.11.2/xn_model.egg-info}/PKG-INFO +1 -1
- xn_model-0.11.1/x_model/__init__.py +0 -15
- {xn_model-0.11.1 → xn_model-0.11.2}/.env.sample +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/.gitignore +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/.pre-commit-config.yaml +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/README.md +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/makefile +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/pyproject.toml +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/setup.cfg +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/tests/__init__.py +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/tests/test_db.py +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/x_model/field.py +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/x_model/func.py +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/x_model/pydantic.py +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/xn_model.egg-info/SOURCES.txt +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/xn_model.egg-info/dependency_links.txt +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/xn_model.egg-info/requires.txt +0 -0
- {xn_model-0.11.1 → xn_model-0.11.2}/xn_model.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from enum import IntEnum
|
|
3
|
+
from types import ModuleType
|
|
4
|
+
|
|
5
|
+
from starlette import status
|
|
6
|
+
from tortoise import Tortoise, connections, ConfigurationError
|
|
7
|
+
from tortoise.backends.asyncpg import AsyncpgDBClient
|
|
8
|
+
from tortoise.exceptions import DBConnectionError
|
|
9
|
+
from fastapi import HTTPException as BaseHTTPException
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def init_db(dsn: str, models: ModuleType, create_tables: bool = False) -> AsyncpgDBClient | str:
|
|
13
|
+
try:
|
|
14
|
+
await Tortoise.init(db_url=dsn, modules={"models": [models]})
|
|
15
|
+
if create_tables:
|
|
16
|
+
await Tortoise.generate_schemas()
|
|
17
|
+
cn: AsyncpgDBClient = connections.get("default")
|
|
18
|
+
except (ConfigurationError, DBConnectionError) as ce:
|
|
19
|
+
return ce.args[0]
|
|
20
|
+
return cn
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class FailReason(IntEnum):
|
|
24
|
+
path = 8
|
|
25
|
+
query = 9
|
|
26
|
+
body = 10
|
|
27
|
+
method = 11
|
|
28
|
+
protocol = 12
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class HTTPException(BaseHTTPException):
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
reason: IntEnum,
|
|
35
|
+
parent: Exception | str = None,
|
|
36
|
+
status_: status = status.HTTP_400_BAD_REQUEST,
|
|
37
|
+
hdrs: dict = None,
|
|
38
|
+
) -> None:
|
|
39
|
+
detail = f"{reason.name}{f': {parent}' if parent else ''}"
|
|
40
|
+
logging.error(detail)
|
|
41
|
+
super().__init__(status_, detail, hdrs)
|
|
@@ -3,8 +3,10 @@ from pydantic import create_model
|
|
|
3
3
|
from tortoise import Model as TortoiseModel
|
|
4
4
|
from tortoise.contrib.pydantic import pydantic_model_creator, PydanticModel
|
|
5
5
|
from tortoise import fields
|
|
6
|
+
from tortoise.exceptions import FieldError
|
|
6
7
|
from tortoise.models import MetaInfo
|
|
7
8
|
from tortoise.queryset import QuerySet
|
|
9
|
+
from x_model import HTTPException, FailReason
|
|
8
10
|
|
|
9
11
|
from x_model.field import DatetimeSecField
|
|
10
12
|
from x_model.pydantic import PydList
|
|
@@ -33,7 +35,7 @@ class BaseModel(TortoiseModel):
|
|
|
33
35
|
def _page_query(cls, sorts: tuple[str], limit: int = 1000, offset: int = 0, q: str = None, **filters) -> QuerySet:
|
|
34
36
|
query = cls.filter(**filters).order_by(*sorts).limit(limit).offset(offset)
|
|
35
37
|
if q:
|
|
36
|
-
query = query.filter(**{f"{cls._name}__icontains": q})
|
|
38
|
+
query = query.filter(**{f"{cls._name[0]}__icontains": q})
|
|
37
39
|
return query
|
|
38
40
|
|
|
39
41
|
@classmethod
|
|
@@ -152,7 +154,10 @@ class Model(BaseModel):
|
|
|
152
154
|
filters = {k: v for k, v in filters.items() if v is not None}
|
|
153
155
|
pyd_item = cls.pyd_list_item()
|
|
154
156
|
query = cls._page_query(sorts, limit, offset, q, **filters)
|
|
155
|
-
|
|
157
|
+
try:
|
|
158
|
+
data = await pyd_item.from_queryset(query)
|
|
159
|
+
except FieldError as e:
|
|
160
|
+
raise HTTPException(FailReason.body, e)
|
|
156
161
|
if limit - (li := len(data)):
|
|
157
162
|
filtered = total = li + offset
|
|
158
163
|
else:
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
from types import ModuleType
|
|
2
|
-
from tortoise import Tortoise, connections, ConfigurationError
|
|
3
|
-
from tortoise.backends.asyncpg import AsyncpgDBClient
|
|
4
|
-
from tortoise.exceptions import DBConnectionError
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
async def init_db(dsn: str, models: ModuleType, create_tables: bool = False) -> AsyncpgDBClient | str:
|
|
8
|
-
try:
|
|
9
|
-
await Tortoise.init(db_url=dsn, modules={"models": [models]})
|
|
10
|
-
if create_tables:
|
|
11
|
-
await Tortoise.generate_schemas()
|
|
12
|
-
cn: AsyncpgDBClient = connections.get("default")
|
|
13
|
-
except (ConfigurationError, DBConnectionError) as ce:
|
|
14
|
-
return ce.args[0]
|
|
15
|
-
return cn
|
|
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
|
|
File without changes
|