-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
336 lines (285 loc) · 12.1 KB
/
controller.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from __future__ import annotations
from collections.abc import Callable, Sequence
from functools import wraps
from flask import jsonify
from flask_jwt_extended import (
unset_jwt_cookies,
set_access_cookies,
create_access_token,
jwt_required,
)
from flask_restx import Namespace, Model as BaseModel, abort as default_abort
from flask_restx.fields import (
List as ListField,
Boolean as BoolField,
Integer as IntegerField,
Nested,
)
from flask_restx.marshalling import marshal
from flask_restx.reqparse import RequestParser
from flask_restx.utils import unpack, merge
from .marshals import ResponseDoc, Model
from ..base import UserRole, DatabaseSearcherMixin, JWTAuthorizerMixin
Undefined = object()
class ResourceController(Namespace, DatabaseSearcherMixin, JWTAuthorizerMixin):
"""
Expansion of :class:`Namespace`, which adds decorators for methods of :class:`Resource`.
Methods of this class (used as decorators) allow parsing request parameters,
modifying responses and automatic updates to the Swagger documentation where possible
"""
def __init__(
self,
name: str,
*,
description: str = None,
path: str = None,
decorators=None,
validate=None,
authorizations=None,
ordered: bool = False,
**kwargs,
):
super().__init__(
name,
description,
path,
decorators,
validate,
authorizations,
ordered,
**kwargs,
)
def abort(self, code: int, message: str = None, **kwargs):
default_abort(code, message, **kwargs)
def doc_abort(
self,
error_code: int | str,
description: str,
*,
critical: bool = False,
):
response = ResponseDoc.error_response(error_code, description)
return self.response(*response.get_args())
def add_authorization(
self,
response,
auth_agent: UserRole,
auth_name: str = None,
) -> None:
jwt = self._get_identity()
if jwt is None:
jwt = {}
jwt[auth_name or ""] = auth_agent.get_identity()
set_access_cookies(response, create_access_token(identity=jwt))
def remove_authorization(self, response, auth_name: str = None) -> None:
jwt = self._get_identity()
unset_jwt_cookies(response)
if jwt is not None:
jwt.pop(auth_name or "")
if len(jwt) != 0:
set_access_cookies(response, create_access_token(identity=jwt))
def adds_authorization(self, auth_name: str = None):
def adds_authorization_wrapper(function):
@wraps(function)
def adds_authorization_inner(*args, **kwargs):
response, result, headers = unpack(function(*args, **kwargs))
if isinstance(result, UserRole): # TODO passthrough for headers
response = jsonify(response)
self.add_authorization(response, result, auth_name)
return response
return response, result, headers
return adds_authorization_inner
return adds_authorization_wrapper
def removes_authorization(self, auth_name: str = None):
def removes_authorization_wrapper(function):
@wraps(function)
@jwt_required()
def removes_authorization_inner(*args, **kwargs):
response, code, headers = unpack(function(*args, **kwargs))
response = jsonify(response) # TODO passthrough for headers & code
self.remove_authorization(response, auth_name)
return response
return removes_authorization_inner
return removes_authorization_wrapper
def argument_parser(self, parser: RequestParser, use_undefined: bool = False):
"""
- Parses request parameters and adds them to kwargs used to call the decorated function.
- Automatically updates endpoint's parameters with arguments from the parser.
"""
def argument_wrapper(function):
@self.expect(parser)
@wraps(function)
def argument_inner(*args, **kwargs):
kwargs.update(parser.parse_args())
if use_undefined:
kwargs.update({
args.dest or args.name: Undefined
for args in parser.args
if (args.dest or args.name) not in kwargs.keys()
})
return function(*args, **kwargs)
return argument_inner
return argument_wrapper
def doc_file_param(self, field_name: str): # redo...
def doc_file_param_wrapper(function):
return self.doc(
params={field_name: {"in": "formData", "type": "file"}},
consumes="multipart/form-data",
)(function)
return doc_file_param_wrapper
def doc_responses(self, *responses: ResponseDoc):
"""
Adds responses to the documentation. **Affects docs only!**
:param responses: all responses to document. Models inside are registered automatically.
"""
def doc_responses_wrapper(function):
for response in responses:
response.register_model(self)
function = self.response(*response.get_args())(function)
return function
return doc_responses_wrapper
def _marshal_result(self, result, fields: type[Model], as_list: bool, **context):
if as_list:
return [fields.convert(d, **context) for d in result]
return fields.convert(result, **context)
def marshal_with(
self,
fields: BaseModel | type[Model],
as_list=False,
skip_none=True,
*args,
**kwargs,
):
result = super().marshal_with
if isinstance(fields, type) and issubclass(fields, Model):
model = self.models.get(fields.name, None) or self.model(model=fields)
def marshal_with_wrapper(function: Callable) -> Callable[..., Model]:
@wraps(function)
@result(model, as_list, *args, skip_none=skip_none, **kwargs)
def marshal_with_inner(*args, **kwargs):
result = function(*args, **kwargs)
if isinstance(result, tuple):
result, code, headers = unpack(result)
return (
self._marshal_result(result, fields, as_list, **kwargs),
code,
headers,
)
return self._marshal_result(result, fields, as_list, **kwargs)
return marshal_with_inner
return marshal_with_wrapper
return result(fields, as_list, *args, skip_none=skip_none, **kwargs)
def marshal(self, data, fields: type[Model] | ..., context=None, *args, **kwargs):
if isinstance(fields, type) and issubclass(fields, Model):
if isinstance(data, Sequence):
data = [fields.convert(d, **context or {}) for d in data]
else:
data = fields.convert(data, **context or {})
fields = self.models.get(fields.name, None) or self.model(model=fields)
return marshal(data, fields, *args, **kwargs)
def marshal_with_authorization(
self,
fields: BaseModel | type[Model],
as_list: bool = False,
auth_name: str = None,
**kwargs,
):
model = self.models.get(fields.name, None) or self.model(model=fields)
def marshal_with_authorization_wrapper(function):
doc = {
"responses": {
"200": (None, [model], kwargs) if as_list else (None, model, kwargs)
},
"__mask__": kwargs.get(
"mask", True
), # Mask values can't be determined outside app context
}
function.__apidoc__ = merge(getattr(function, "__apidoc__", {}), doc)
@wraps(function)
def marshal_with_authorization_inner(*args, **kwargs):
response, result, headers = unpack(function(*args, **kwargs))
if isinstance(result, UserRole): # TODO passthrough for headers
response = jsonify(
self.marshal(response, fields, skip_none=True, context=kwargs)
)
self.add_authorization(response, result, auth_name)
return response
return response, result, headers
return marshal_with_authorization_inner
return marshal_with_authorization_wrapper
def lister(
self,
per_request: int,
marshal_model: BaseModel | type[Model],
skip_none: bool = True,
count_all: Callable[..., int] | None = None,
provided_total: bool = False,
):
"""
- Used for organising pagination.
- Uses `counter` form incoming arguments for the decorated function and `per_request` argument
to define start and finish indexes, passed as keyword arguments to the decorated function.
- Marshals the return of the decorated function as a list with `marshal_model`.
- Adds information on the response to documentation automatically.
:param per_request:
:param marshal_model:
:param skip_none:
:param count_all:
:param provided_total:
:return:
"""
if isinstance(marshal_model, type) and issubclass(marshal_model, Model):
name = marshal_model.name
model = self.models.get(name, None) or self.model(model=marshal_model)
else:
name = marshal_model.name
model = marshal_model
response = {
"results": ListField(Nested(model), max_items=per_request),
"has-next": BoolField,
}
if count_all is not None or provided_total:
response["total"] = IntegerField
response = BaseModel(f"List" + name, response)
response = ResponseDoc(200, f"Max size of results: {per_request}", response)
def lister_wrapper(function):
@self.doc_responses(response)
@wraps(function)
def lister_inner(*args, **kwargs):
offset: int = kwargs.pop("offset", None)
counter: int = kwargs.pop("counter", None)
if offset is None:
if counter is None:
self.abort(400, "Neither counter nor offset are provided")
offset = counter * per_request
kwargs["start"] = offset
kwargs["finish"] = offset + per_request + 1
result_list = function(*args, **kwargs)
if provided_total:
total = result_list[1]
result_list = result_list[0]
if has_next := len(result_list) > per_request:
result_list.pop()
if isinstance(marshal_model, type) and issubclass(marshal_model, Model):
result_list = [
marshal_model.convert(result, **kwargs)
for result in result_list
]
result = {
"results": marshal(result_list, model, skip_none=skip_none),
"has-next": has_next,
}
if count_all is not None:
result["total"] = count_all(*args, **kwargs)
if provided_total:
result["total"] = total
return result
return lister_inner
return lister_wrapper
def model(self, name: str = None, model=None, **kwargs):
# TODO recursive registration
if isinstance(model, type) and issubclass(model, Model):
if model.name is None:
model.name = name or model.__qualname__
return super().model(name or model.name, model.model(), **kwargs)
return super().model(name, model, **kwargs)