Move decorate_and_route to flasktools.
This commit is contained in:
parent
1f18713875
commit
1e9e948431
1 changed files with 14 additions and 28 deletions
|
@ -3,6 +3,7 @@ Do not execute this file directly.
|
|||
Use etiquette_flask_dev.py or etiquette_flask_prod.py.
|
||||
'''
|
||||
import flask; from flask import request
|
||||
import functools
|
||||
import mimetypes
|
||||
import traceback
|
||||
|
||||
|
@ -52,34 +53,19 @@ file_etag_manager = client_caching.FileEtagManager(
|
|||
|
||||
# Response wrappers ################################################################################
|
||||
|
||||
# Flask provides decorators for before_request and after_request, but not for
|
||||
# wrapping the whole request. The decorators I am using need to wrap the whole
|
||||
# request, either to catch exceptions (which don't get passed through
|
||||
# after_request) or to maintain some state before running the function and
|
||||
# adding it to the response after.
|
||||
# Instead of copy-pasting my decorators onto every single function and
|
||||
# forgetting to keep up with them in the future, let's just hijack the
|
||||
# decorator I know every endpoint will have: site.route.
|
||||
_original_route = site.route
|
||||
def decorate_and_route(*route_args, **route_kwargs):
|
||||
def wrapper(endpoint):
|
||||
# Since a function might have multiple routes, we may be seeing the
|
||||
# same one multiple times. The _fully_decorated will track that.
|
||||
if not hasattr(endpoint, '_fully_decorated'):
|
||||
endpoint = flasktools.ensure_response_type(endpoint)
|
||||
endpoint = flasktools.give_theme_cookie(
|
||||
endpoint,
|
||||
site.route = flasktools.decorate_and_route(
|
||||
flask_app=site,
|
||||
decorators=[
|
||||
flasktools.ensure_response_type,
|
||||
functools.partial(
|
||||
flasktools.give_theme_cookie,
|
||||
cookie_name='etiquette_theme',
|
||||
default_theme='slate',
|
||||
)
|
||||
endpoint = decorators.catch_etiquette_exception(endpoint)
|
||||
endpoint = session_manager.give_token(endpoint)
|
||||
|
||||
endpoint = _original_route(*route_args, **route_kwargs)(endpoint)
|
||||
endpoint._fully_decorated = True
|
||||
return endpoint
|
||||
return wrapper
|
||||
site.route = decorate_and_route
|
||||
),
|
||||
decorators.catch_etiquette_exception,
|
||||
session_manager.give_token
|
||||
],
|
||||
)
|
||||
|
||||
@site.before_request
|
||||
def before_request():
|
||||
|
|
Loading…
Reference in a new issue