-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathmem.py
129 lines (104 loc) · 4.31 KB
/
mem.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
from typing import Any, Callable, List, Type, cast, Optional, Union
from fastapi import HTTPException
from . import CRUDGenerator, NOT_FOUND
from ._types import DEPENDENCIES, PAGINATION, PYDANTIC_SCHEMA as SCHEMA
from ._utils import get_pk_type, create_schema_default_factory
CALLABLE = Callable[..., SCHEMA]
CALLABLE_LIST = Callable[..., List[SCHEMA]]
class MemoryCRUDRouter(CRUDGenerator[SCHEMA]):
def __init__(
self,
schema: Type[SCHEMA],
create_schema: Optional[Type[SCHEMA]] = None,
update_schema: Optional[Type[SCHEMA]] = 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:
self._pk_type: type = get_pk_type(schema)
super().__init__(
schema=schema,
create_schema=create_schema,
update_schema=update_schema,
prefix=prefix,
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.models: List[SCHEMA] = []
self._id = 1
def _get_all(self, *args: Any, **kwargs: Any) -> CALLABLE_LIST:
def route(pagination: PAGINATION = self.pagination) -> List[SCHEMA]:
skip, limit = pagination.get("skip"), pagination.get("limit")
skip = cast(int, skip)
return (
self.models[skip:]
if limit is None
else self.models[skip : skip + limit]
)
return route
def _get_one(self, *args: Any, **kwargs: Any) -> CALLABLE:
def route(item_id: self._pk_type) -> SCHEMA:
for model in self.models:
if model.id == item_id: # type: ignore
return model
raise NOT_FOUND
return route
def _create(self, *args: Any, **kwargs: Any) -> CALLABLE:
def route(model: self.create_schema) -> SCHEMA: # type: ignore
model, using_default_factory = create_schema_default_factory(
schema_cls=self.schema,
create_schema_instance=model,
pk_field_name=self._pk,
)
model_dict = model.dict()
if using_default_factory:
for _model in self.models:
if _model.id == model.id: # type: ignore
raise HTTPException(422, "Key already exists") from None
else:
model_dict["id"] = self._get_next_id()
ready_model = self.schema(**model_dict)
self.models.append(ready_model)
return ready_model
return route
def _update(self, *args: Any, **kwargs: Any) -> CALLABLE:
def route(item_id: self._pk_type, model: self.update_schema) -> SCHEMA: # type: ignore
for ind, model_ in enumerate(self.models):
if model_.id == item_id: # type: ignore
self.models[ind] = self.schema(
**model.dict(), id=model_.id # type: ignore
)
return self.models[ind]
raise NOT_FOUND
return route
def _delete_all(self, *args: Any, **kwargs: Any) -> CALLABLE_LIST:
def route() -> List[SCHEMA]:
self.models = []
return self.models
return route
def _delete_one(self, *args: Any, **kwargs: Any) -> CALLABLE:
def route(item_id: self._pk_type) -> SCHEMA:
for ind, model in enumerate(self.models):
if model.id == item_id: # type: ignore
del self.models[ind]
return model
raise NOT_FOUND
return route
def _get_next_id(self) -> int:
id_ = self._id
self._id += 1
return id_