-
Notifications
You must be signed in to change notification settings - Fork 55
/
view_classes.py
437 lines (304 loc) · 9.21 KB
/
view_classes.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from flask import Response
from flask_classful import FlaskView, route
from functools import wraps
VALUE1 = "value1"
def get_value():
return VALUE1
class BasicView(FlaskView):
def index(self):
"""A docstring for testing that docstrings are set"""
return "Index"
def get(self, obj_id):
return "Get " + obj_id, 404
def put(self, id):
return "Put " + id, 403, {'say': 'hello'}
def patch(self, id):
return "Patch " + id
def post(self):
return "Post"
def delete(self, id):
return "Delete " + id
def custom_method(self):
return "Custom Method"
def custom_method_with_params(self, p_one, p_two):
return "Custom Method %s %s" % (p_one, p_two,)
@route("/routed/")
def routed_method(self):
return "Routed Method"
@route("/route1/")
@route("/route2/")
def multi_routed_method(self):
return "Multi Routed Method"
@route("/noslash")
def no_slash_method(self):
return "No Slash Method"
@route("/endpoint/", endpoint="basic_endpoint")
def custom_endpoint(self):
return "Custom Endpoint"
@route("/route3/", methods=['POST'])
def custom_http_method(self):
return "Custom HTTP Method"
class SubdomainAttributeView(FlaskView):
subdomain = "sub1"
def index(self):
return "Index"
class SubdomainRouteView(FlaskView):
@route("/", subdomain="sub2")
def index(self):
return "Index"
class IndexView(FlaskView):
route_base = "/"
def index(self):
return "Index"
class RouteBaseView(FlaskView):
route_base = "/base-routed/"
def index(self):
return "Index"
class RoutePrefixView(FlaskView):
route_prefix = "/my_prefix/"
def index(self):
return "Index"
class VarBaseView(FlaskView):
route_base = "/var-base-route/<route>"
def before_index(self):
from flask import request
request.view_args.pop('route')
def index(self):
return "Custom routed."
def with_base_arg(self, route):
return "Base route arg: " + route
@route('/local/<route_local>', methods=['GET'])
def with_route_arg(self, route, route_local):
return "%s %s" % (route, route_local)
class BeforeRequestView(FlaskView):
def before_request(self, name):
self.response = "Before Request"
def index(self):
return self.response
class BeforeViewView(FlaskView):
def before_index(self):
self.response = "Before View"
def index(self):
return self.response
class BeforeRequestReturnsView(FlaskView):
def before_request(self, name):
return "BEFORE"
def index(self):
return "Should never see this"
class BeforeViewReturnsView(FlaskView):
def before_index(self):
return "BEFORE"
def index(self):
return "Should never see this"
class AfterViewView(FlaskView):
def after_index(self, response):
return "After View"
def index(self):
return "Index"
class AfterRequestView(FlaskView):
def after_request(self, name, response):
return "After Request"
def index(self):
return "Index"
class VariedMethodsView(FlaskView):
def index(self):
return "Index"
@route("/routed/")
def routed_method(self):
return "Routed Method"
@classmethod
def class_method(cls):
return "Class Method"
class SubVariedMethodsView(VariedMethodsView):
pass
def func_decorator(f):
def decorated_view(*args, **kwargs):
return f(*args, **kwargs)
return decorated_view
def wraps_decorator(f):
@wraps(f)
def decorated_view(*args, **kwargs):
return f(*args, **kwargs)
return decorated_view
def params_decorator(p_1, p_2):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
return f(*args, **kwargs)
return decorated_function
return decorator
def recursive_decorator(f):
@wraps(f)
def decorated_view(*args, **kwargs):
decorated_view.foo()
return f(*args, **kwargs)
def foo():
return 'bar'
decorated_view.foo = foo
return decorated_view
def more_recursive(stop_type):
def _inner(func):
def _recursive(*args, **kw):
return func(*args, **kw)
return _recursive
return _inner
class DecoratedView(FlaskView):
@func_decorator
def index(self):
return "Index"
@func_decorator
def get(self, id):
return "Get " + id
@recursive_decorator
def post(self):
return "Post"
@params_decorator("oneval", "anotherval")
def params_decorator_method(self):
return "Params Decorator"
@params_decorator(get_value(), "value")
def delete(self, obj_id):
return "Params Decorator Delete " + obj_id
@more_recursive(None)
def get_some(self):
return "Get Some"
@more_recursive(None)
@recursive_decorator
def get_this(self):
return "Get This"
@route('/mixitup')
@more_recursive(None)
@recursive_decorator
def mixitup(self):
return "Mix It Up"
@more_recursive(None)
def someval(self, val):
return "Someval " + val
@route('/anotherval/<val>')
@more_recursive(None)
@recursive_decorator
def anotherval(self, val):
return "Anotherval " + val
class InheritanceView(BasicView):
# Tests method override
def get(self, obj_id):
return "Inheritance Get " + obj_id
@route('/<obj_id>/delete', methods=['DELETE'])
def delete(self, obj_id):
return "Inheritance Delete " + obj_id
@route('/with_route')
def with_route(self):
return "Inheritance with route"
class DecoratedInheritanceView(DecoratedView):
@recursive_decorator
def get(self, obj_id):
return "Decorated Inheritance Get " + obj_id
class TrailingSlashView(FlaskView):
trailing_slash = False
route_base = '/trailing/'
def index(self):
"""A docstring for testing that docstrings are set"""
return "Index"
def get(self, obj_id):
return "Get " + obj_id
def put(self, id):
return "Put " + id
def patch(self, id):
return "Patch " + id
# def post(self):
# return "Post"
@route('', methods=['POST'])
def post(self):
return "Post"
def delete(self, id):
return "Delete " + id
def custom_method(self):
return "Custom Method"
def custom_method_with_params(self, p_one, p_two):
return "Custom Method %s %s" % (p_one, p_two,)
@route("/routed/")
def routed_method(self):
return "Routed Method"
@route("/routed2")
def routed_method2(self):
return "Routed Method 2"
class InheritedTrailingSlashView(TrailingSlashView):
route_base = '/inherited/trailing/'
def index(self):
return "Index"
class OverrideInheritedTrailingSlashView(TrailingSlashView):
route_base = "/override/trailing/"
trailing_slash = True
def index(self):
return "Index"
def make_bold_decorator(fn):
@wraps(fn)
def inner(*args, **kwargs):
return '<b>' + fn(*args, **kwargs) + '</b>'
return inner
def make_italics_decorator(fn):
@wraps(fn)
def inner(*args, **kwargs):
return '<i>' + fn(*args, **kwargs) + '</i>'
return inner
def make_paragraph_decorator(fn):
@wraps(fn)
def inner(*args, **kwargs):
return '<p>' + fn(*args, **kwargs) + '</p>'
return inner
class DecoratedBoldListView(FlaskView):
route_base = '/decorated_bold_list_view/'
decorators = [make_bold_decorator]
def get(self, id):
return 'Get %s'%id
def index(self):
return 'Index'
class DecoratedBoldItalicsListView(FlaskView):
route_base = '/decorated_bold_italics_list_view/'
decorators = [make_bold_decorator, make_italics_decorator]
def get(self, id):
return 'Get %s'%id
def index(self):
return 'Index'
class DecoratedListMemberView(FlaskView):
route_base = '/decorated_list_member_view/'
decorators = [
# Third Decorator
make_bold_decorator,
# Second Decorator
make_italics_decorator
]
# First decorator
@make_paragraph_decorator
def get(self, id):
return 'Get %s'%id
def index(self):
return 'Index'
def eggs_attribute_decorator(eggs_style):
def decorator(f):
# Apply the style to the function
f._eggs = eggs_style
@wraps(f)
def decorated_function(*args, **kwargs):
return f(*args, **kwargs)
return decorated_function
return decorator
class DecoratedListFunctionAttributesView(FlaskView):
route_base = '/decorated_list_function_attributes_view/'
decorators = [
make_italics_decorator,
eggs_attribute_decorator('scrambled')
]
@make_bold_decorator
def get(self, id):
return 'Get %s'%id
def index(self):
return 'Index'
class DecoratedListMemberFunctionAttributesView(FlaskView):
route_base = '/decorated_list_member_function_attributes_view/'
decorators = [make_italics_decorator]
@make_bold_decorator
def get(self, id):
return 'Get %s'%id
@eggs_attribute_decorator('scrambled')
def index(self):
return 'Index'