-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.py
More file actions
243 lines (202 loc) · 7.26 KB
/
view.py
File metadata and controls
243 lines (202 loc) · 7.26 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import typing as t
from abc import ABC, abstractmethod
from collections import OrderedDict
import ellar.common as ecm
import ellar.core as ec
import sqlalchemy as sa
from pydantic import BaseModel, Field
from ellar_sql.model.base import ModelBase
from ellar_sql.schemas import BasicPaginationSchema, PageNumberPaginationSchema
from .base import Paginator
from .utils import remove_query_param, replace_query_param
class PaginationBase(ABC):
InputSource = ecm.Query
class Input(BaseModel):
pass
def get_annotation(self) -> t.Any:
return self.InputSource[self.Input, self.InputSource.P(...)]
@abstractmethod
def get_output_schema(
self, item_schema: t.Type[BaseModel]
) -> t.Type[BaseModel]: # pragma: no cover
"""Return a Response Schema Type for item schema"""
def validate_model(
self,
model: t.Union[t.Type[ModelBase], sa.sql.Select[t.Any]],
fallback: t.Optional[t.Union[t.Type[ModelBase], sa.sql.Select[t.Any]]],
) -> t.Union[t.Type[ModelBase], sa.sql.Select[t.Any]]:
if isinstance(model, sa.sql.Select) or (
isinstance(model, type) and issubclass(model, ModelBase)
):
working_model = model
else:
working_model = fallback # type:ignore[assignment]
assert working_model is not None, "Model Can not be None"
return working_model
@abstractmethod
def api_paginate(
self,
model: t.Union[t.Type[ModelBase], sa.sql.Select[t.Any]],
input_schema: t.Any,
request: ec.Request,
**params: t.Any,
) -> t.Any:
pass # pragma: no cover
@abstractmethod
def pagination_context(
self,
model: t.Union[t.Type[ModelBase], sa.sql.Select[t.Any]],
input_schema: t.Any,
request: ec.Request,
**params: t.Any,
) -> t.Dict[str, t.Any]:
pass # pragma: no cover
if t.TYPE_CHECKING:
def __init__(self, **kwargs: t.Any) -> None: ...
class PageNumberPagination(PaginationBase):
class Input(BaseModel):
page: int = Field(1, gt=0)
paginator_class: t.Type[Paginator] = Paginator
page_query_param: str = "page"
def __init__(
self,
*,
model: t.Optional[t.Type[ModelBase]] = None,
per_page: int = 20,
max_per_page: int = 100,
error_out: bool = True,
) -> None:
super().__init__()
self._model = model
self._paginator_init_kwargs = {
"per_page": per_page,
"max_per_page": max_per_page,
"error_out": error_out,
}
def get_output_schema(self, item_schema: t.Type[BaseModel]) -> t.Type[BaseModel]:
return PageNumberPaginationSchema[item_schema] # type:ignore[valid-type]
def api_paginate(
self,
model: t.Union[t.Type[ModelBase], sa.sql.Select, t.Any],
input_schema: Input,
request: ec.Request,
**params: t.Any,
) -> t.Any:
working_model = self.validate_model(model, self._model)
paginator = self.paginator_class(
model=working_model, page=input_schema.page, **self._paginator_init_kwargs
)
return self._get_paginated_response(
base_url=str(request.url), paginator=paginator
)
def pagination_context(
self,
model: t.Union[t.Type, sa.sql.Select],
input_schema: Input,
request: ec.Request,
**params: t.Any,
) -> t.Dict[str, t.Any]:
working_model = self.validate_model(model, self._model)
paginator = self.paginator_class(
model=working_model, page=input_schema.page, **self._paginator_init_kwargs
)
return {"paginator": paginator}
def _get_paginated_response(
self, *, base_url: str, paginator: Paginator
) -> t.Dict[str, t.Any]:
is_query = self.InputSource.name == "Query"
next_url = (
self._get_next_link(base_url, paginator=paginator) if is_query else None
)
prev_url = (
self._get_previous_link(base_url, paginator=paginator) if is_query else None
)
return OrderedDict(
[
("count", paginator.total),
("next", next_url),
("previous", prev_url),
("items", list(paginator)),
]
)
def _get_next_link(self, url: str, paginator: Paginator) -> t.Optional[str]:
if not paginator.has_next:
return None
page_number = paginator.page + 1
return replace_query_param(url, self.page_query_param, page_number)
def _get_previous_link(self, url: str, paginator: Paginator) -> t.Optional[str]:
if not paginator.has_prev:
return None
page_number = paginator.page - 1
if page_number == 1:
return remove_query_param(url, self.page_query_param)
return replace_query_param(url, self.page_query_param, page_number)
class LimitOffsetPagination(PaginationBase):
class Input(BaseModel):
limit: int = Field(50, ge=1)
offset: int = Field(0, ge=0)
paginator_class: t.Type[Paginator] = Paginator
def __init__(
self,
*,
model: t.Optional[t.Type[ModelBase]] = None,
limit: int = 50,
max_limit: int = 100,
error_out: bool = True,
) -> None:
super().__init__()
self._model = model
self._max_limit = max_limit
self._error_out = error_out
self._paginator_init_kwargs = {
"error_out": error_out,
"max_per_page": max_limit,
}
self.Input = self.create_input(limit) # type:ignore[misc]
def create_input(self, limit: int) -> t.Type[Input]:
_limit = int(limit)
class DynamicInput(self.Input):
limit: int = Field(_limit, ge=1)
offset: int = Field(0, ge=0)
return DynamicInput
def get_output_schema(self, item_schema: t.Type[BaseModel]) -> t.Type[BaseModel]:
return BasicPaginationSchema[item_schema]
def api_paginate(
self,
model: t.Union[t.Type[ModelBase], sa.sql.Select[t.Any], t.Any],
input_schema: Input,
request: ec.Request,
**params: t.Any,
) -> t.Any:
working_model = self.validate_model(model, self._model)
page = input_schema.offset or 1
per_page: int = min(input_schema.limit, self._max_limit)
paginator = self.paginator_class(
model=working_model,
page=page,
per_page=per_page,
**self._paginator_init_kwargs,
)
return OrderedDict(
[
("count", paginator.total),
("items", list(paginator)),
]
)
def pagination_context(
self,
model: t.Union[t.Type, sa.sql.Select[t.Any]],
input_schema: Input,
request: ec.Request,
**params: t.Any,
) -> t.Dict[str, t.Any]:
working_model = self.validate_model(model, self._model)
page = input_schema.offset or 1
per_page: int = min(input_schema.limit, self._max_limit)
paginator = self.paginator_class(
model=working_model,
page=page,
per_page=per_page,
**self._paginator_init_kwargs,
)
return {"paginator": paginator}