-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathormar.py
163 lines (140 loc) · 5.61 KB
/
ormar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from typing import (
Any,
Callable,
List,
Optional,
Type,
cast,
Coroutine,
Union,
)
from fastapi import HTTPException
from . import CRUDGenerator, NOT_FOUND, _utils
from ._types import DEPENDENCIES, PAGINATION
from ._utils import create_schema_default_factory
try:
from ormar import Model, NoMatch
except ImportError:
Model = None # type: ignore
NoMatch = None # type: ignore
ormar_installed = False
else:
ormar_installed = True
CALLABLE = Callable[..., Coroutine[Any, Any, Model]]
CALLABLE_LIST = Callable[..., Coroutine[Any, Any, List[Optional[Model]]]]
class OrmarCRUDRouter(CRUDGenerator[Model]):
def __init__(
self,
schema: Type[Model],
create_schema: Optional[Type[Model]] = None,
update_schema: Optional[Type[Model]] = None,
default_factory_schema: Optional[Type[Model]] = None,
prefix: Optional[str] = None,
tags: Optional[List[str]] = None,
paginate: Optional[int] = None,
get_all_route: Union[bool, DEPENDENCIES] = True,
get_one_route: Union[bool, DEPENDENCIES] = True,
create_route: Union[bool, DEPENDENCIES] = True,
update_route: Union[bool, DEPENDENCIES] = True,
delete_one_route: Union[bool, DEPENDENCIES] = True,
delete_all_route: Union[bool, DEPENDENCIES] = True,
**kwargs: Any
) -> None:
assert ormar_installed, "Ormar must be installed to use the OrmarCRUDRouter."
self._pk: str = schema.Meta.pkname
self._pk_type: type = _utils.get_pk_type(schema, self._pk)
self.default_factory_schema = (
default_factory_schema if default_factory_schema else schema
)
super().__init__(
schema=schema,
create_schema=create_schema or schema,
update_schema=update_schema or schema,
prefix=prefix or schema.Meta.tablename,
tags=tags,
paginate=paginate,
get_all_route=get_all_route,
get_one_route=get_one_route,
create_route=create_route,
update_route=update_route,
delete_one_route=delete_one_route,
delete_all_route=delete_all_route,
**kwargs
)
self._INTEGRITY_ERROR = self._get_integrity_error_type()
def _get_all(self, *args: Any, **kwargs: Any) -> CALLABLE_LIST:
async def route(
pagination: PAGINATION = self.pagination,
) -> List[Optional[Model]]:
skip, limit = pagination.get("skip"), pagination.get("limit")
query = self.schema.objects.offset(cast(int, skip))
if limit:
query = query.limit(limit)
return await query.all() # type: ignore
return route
def _get_one(self, *args: Any, **kwargs: Any) -> CALLABLE:
async def route(item_id: self._pk_type) -> Model: # type: ignore
try:
filter_ = {self._pk: item_id}
model = await self.schema.objects.filter(
_exclude=False, **filter_
).first()
except NoMatch:
raise NOT_FOUND from None
return model
return route
def _create(self, *args: Any, **kwargs: Any) -> CALLABLE:
async def route(model: self.create_schema) -> Model: # type: ignore
model, _ = create_schema_default_factory(
schema_cls=self.default_factory_schema,
create_schema_instance=model,
pk_field_name=self._pk,
)
model_dict = model.dict()
if self.schema.Meta.model_fields[self._pk].autoincrement:
model_dict.pop(self._pk, None)
try:
return await self.schema.objects.create(**model_dict)
except self._INTEGRITY_ERROR:
raise HTTPException(422, "Key already exists") from None
return route
def _update(self, *args: Any, **kwargs: Any) -> CALLABLE:
async def route(
item_id: self._pk_type, # type: ignore
model: self.update_schema, # type: ignore
) -> Model:
filter_ = {self._pk: item_id}
try:
await self.schema.objects.filter(_exclude=False, **filter_).update(
**model.dict(exclude_unset=True)
)
except self._INTEGRITY_ERROR as e:
self._raise(e)
return await self._get_one()(item_id)
return route
def _delete_all(self, *args: Any, **kwargs: Any) -> CALLABLE_LIST:
async def route() -> List[Optional[Model]]:
await self.schema.objects.delete(each=True)
return await self._get_all()(pagination={"skip": 0, "limit": None})
return route
def _delete_one(self, *args: Any, **kwargs: Any) -> CALLABLE:
async def route(item_id: self._pk_type) -> Model: # type: ignore
model = await self._get_one()(item_id)
await model.delete()
return model
return route
def _get_integrity_error_type(self) -> Type[Exception]:
"""Imports the Integrity exception based on the used backend"""
backend = self.schema.db_backend_name()
try:
if backend == "sqlite":
from sqlite3 import IntegrityError
elif backend == "postgresql":
from asyncpg import ( # type: ignore
IntegrityConstraintViolationError as IntegrityError,
)
else:
from pymysql import IntegrityError # type: ignore
return IntegrityError
except ImportError:
return Exception