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.
|
Use etiquette_flask_dev.py or etiquette_flask_prod.py.
|
||||||
'''
|
'''
|
||||||
import flask; from flask import request
|
import flask; from flask import request
|
||||||
|
import functools
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
@ -52,34 +53,19 @@ file_etag_manager = client_caching.FileEtagManager(
|
||||||
|
|
||||||
# Response wrappers ################################################################################
|
# Response wrappers ################################################################################
|
||||||
|
|
||||||
# Flask provides decorators for before_request and after_request, but not for
|
site.route = flasktools.decorate_and_route(
|
||||||
# wrapping the whole request. The decorators I am using need to wrap the whole
|
flask_app=site,
|
||||||
# request, either to catch exceptions (which don't get passed through
|
decorators=[
|
||||||
# after_request) or to maintain some state before running the function and
|
flasktools.ensure_response_type,
|
||||||
# adding it to the response after.
|
functools.partial(
|
||||||
# Instead of copy-pasting my decorators onto every single function and
|
flasktools.give_theme_cookie,
|
||||||
# forgetting to keep up with them in the future, let's just hijack the
|
cookie_name='etiquette_theme',
|
||||||
# decorator I know every endpoint will have: site.route.
|
default_theme='slate',
|
||||||
_original_route = site.route
|
),
|
||||||
def decorate_and_route(*route_args, **route_kwargs):
|
decorators.catch_etiquette_exception,
|
||||||
def wrapper(endpoint):
|
session_manager.give_token
|
||||||
# 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,
|
|
||||||
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
|
|
||||||
|
|
||||||
@site.before_request
|
@site.before_request
|
||||||
def before_request():
|
def before_request():
|
||||||
|
|
Loading…
Reference in a new issue