xn-model 1.0.2__py3-none-any.whl → 1.0.4__py3-none-any.whl

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.
x_model/__init__.py CHANGED
@@ -1,36 +1,12 @@
1
- import logging
2
- from enum import IntEnum
3
1
  from types import ModuleType
4
2
 
5
3
  from tortoise import Tortoise, connections
6
4
  from tortoise.backends.asyncpg import AsyncpgDBClient
7
5
 
8
6
 
9
- async def init_db(dsn: str, models: ModuleType, create_tables: bool = False) -> AsyncpgDBClient | str:
7
+ async def init_db(dsn: str, models: ModuleType, create_tables: bool = True) -> AsyncpgDBClient | str:
10
8
  await Tortoise.init(db_url=dsn, modules={"models": [models]})
11
9
  if create_tables:
12
10
  await Tortoise.generate_schemas()
13
11
  cn: AsyncpgDBClient = connections.get("default")
14
12
  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)
x_model/field.py CHANGED
@@ -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):
x_model/models.py CHANGED
@@ -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 DatetimeField, IntField
6
+ from tortoise.fields import IntField
7
7
 
8
- from x_model import HTTPException, FailReason
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,6 +16,8 @@ class TsTrait:
21
16
  class Model(BaseModel):
22
17
  id: int = IntField(True)
23
18
 
19
+ _pyd: type[PydanticModel] = None # overridable
20
+ _pydIn: type[PydanticModel] = None # overridable
24
21
  _name: tuple[str] = ("name",)
25
22
  _sorts: tuple[str] = ("-id",)
26
23
 
@@ -29,14 +26,24 @@ class Model(BaseModel):
29
26
 
30
27
  @classmethod
31
28
  def pyd(cls):
32
- return pydantic_model_creator(cls, name=cls.__name__)
29
+ if not cls._pyd:
30
+ cls._pyd = pydantic_model_creator(cls, name=cls.__name__)
31
+ return cls._pyd
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
33
40
 
34
41
  # # # CRUD Methods # # #
35
42
  @classmethod
36
43
  async def get_one(cls, id_: int, **filters) -> PydanticModel:
37
44
  if obj := await cls.get_or_none(id=id_, **filters):
38
45
  return await cls.pyd().from_tortoise_orm(obj)
39
- raise HTTPException(reason=FailReason.path, status_=404, parent=f"{cls.__name__}#{id_} not found")
46
+ raise LookupError(f"{cls.__name__}#{id_} not found")
40
47
 
41
48
  @classmethod
42
49
  async def get_or_create_by_name(cls, name: str, attr_name: str = None, def_dict: dict = None) -> "Model":
@@ -52,11 +59,16 @@ class Model(BaseModel):
52
59
  # exclude: tuple[str, ...] = ("Meta",)
53
60
  # computed: tuple[str, ...] = ()
54
61
  # backward_relations: bool = True
55
- max_recursion: int = 0 # default: 3
62
+ max_recursion: int = 1 # default: 3
56
63
  # allow_cycles: bool = False
57
64
  # exclude_raw_fields: bool = True
58
65
  # sort_alphabetically: bool = False
59
66
  # model_config: ConfigDict | None = None
60
67
 
68
+ class PydanticMetaIn:
69
+ backward_relations: bool = False
70
+ max_recursion: int = 0
71
+ exclude_raw_fields: bool = False
72
+
61
73
  class Meta:
62
74
  abstract = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: xn-model
3
- Version: 1.0.2
3
+ Version: 1.0.4
4
4
  Summary: Base model for xn-api
5
5
  Author-email: Mike Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -0,0 +1,8 @@
1
+ x_model/__init__.py,sha256=JRsYD3P-8pHpEkCUgy6GuodkT7ZaY1AEeEK1CbpgZw4,428
2
+ x_model/field.py,sha256=w5BAoNA33a-uROaExx5Pr3jYQnUMKIYFYcPTjdBlx3M,2784
3
+ x_model/func.py,sha256=E7jDoHJGaFpKvxbHnT_lyBxUZeMo-GRd5gv9dLw7B9s,289
4
+ x_model/models.py,sha256=nI7HkWaSMs90AgT526HaaxWkEvakE8MlmaZZjHP5Fiw,2537
5
+ xn_model-1.0.4.dist-info/METADATA,sha256=Dy-N6fK1wjfGdxytbYwIutLNq0cGxReRmsXqyNvi-So,961
6
+ xn_model-1.0.4.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
+ xn_model-1.0.4.dist-info/top_level.txt,sha256=QCYyfv5AA_8jPPtCpShkBXzQRUCGuuW7Ro0mqysDE8E,8
8
+ xn_model-1.0.4.dist-info/RECORD,,
x_model/pydantic.py DELETED
@@ -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
@@ -1,9 +0,0 @@
1
- x_model/__init__.py,sha256=JgpONZwWeYiV4mzk0nT8PM4ZzTSCSAGy4PV2G2ZIW04,933
2
- x_model/field.py,sha256=S461M94ryQG7yu8lreXtWnZo3YdCP97xhbcCJ3BzXsY,2751
3
- x_model/func.py,sha256=E7jDoHJGaFpKvxbHnT_lyBxUZeMo-GRd5gv9dLw7B9s,289
4
- x_model/models.py,sha256=XZu7jWf0Wl5JiHvS222iumMk3eJLuQBkhOVfq-46qd8,2141
5
- x_model/pydantic.py,sha256=18O4yFvYDyZaSqIaOKcZhQRpIBM2o73DNlqrXvU_4XM,342
6
- xn_model-1.0.2.dist-info/METADATA,sha256=0B-FErvCVRVC13r_j1TMqZrcQ6KssyvNoO6OlsnArsw,961
7
- xn_model-1.0.2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
8
- xn_model-1.0.2.dist-info/top_level.txt,sha256=QCYyfv5AA_8jPPtCpShkBXzQRUCGuuW7Ro0mqysDE8E,8
9
- xn_model-1.0.2.dist-info/RECORD,,