这是@Mark Hildreth's answer的一个变体,它确实包装并返回了一个函数:
from functools import wraps
from flask import Flask, request, g
app = Flask(__name__)
def exclude_from_analytics(*args, **kw):
def wrapper(endpoint_method):
endpoint_method._skip_analytics = True
@wraps(endpoint_method)
def wrapped(*endpoint_args, **endpoint_kw):
# This is what I want I want to do. Will not work.
#g.skip_analytics = getattr(endpoint_method, '_skip_analytics', False)
return endpoint_method(*endpoint_args, **endpoint_kw)
return wrapped
return wrapper
@app.route('/')
def no_skip():
return 'Skip analytics? %s' % (g.skip_analytics)
@app.route('/skip')
@exclude_from_analytics()
def skip():
return 'Skip analytics? %s' % (g.skip_analytics)
@app.before_request
def analytics_view(*args, **kwargs):
if request.endpoint in app.view_functions:
view_func = app.view_functions[request.endpoint]
g.skip_analytics = hasattr(view_func, '_skip_analytics')
print 'Should skip analytics on {0}: {1}'.format(request.path, g.skip_analytics)
app.run(debug=True)
它不能像我预期和希望的那样简单地工作的原因与 Flask 上下文堆栈和应用回调的顺序有关。这是方法调用的时间线(基于删除后的一些调试语句):
$ python test-flask-app.py
# Application Launched
DECORATOR exclude_from_analytics
DECORATOR wrapper
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
# REQUEST: /
DECORATOR app.before_request: analytics_view
> Should skip analytics on /: False
ENDPOINT no_skip
127.0.0.1 - - [14/May/2016 16:10:39] "GET / HTTP/1.1" 200 -
# REQUEST: /skip
DECORATOR app.before_request: analytics_view
> Should skip analytics on /skip: True
DECORATOR wrapped
ENDPOINT skip
127.0.0.1 - - [14/May/2016 16:12:46] "GET /skip HTTP/1.1" 200 -
我宁愿g.skip_analytics
从wrapped
函数内部进行设置。但是因为直到钩子之后才调用它analytics_view
@app.before_request
,所以我必须按照 Mark 的示例并_skip_analytics
在端点方法上设置 attr,该方法加载在我调用的应用程序(而不是request)上下文中,该上下文仅在启动时被调用。
有关更多信息flask.g
和应用程序上下文,请参阅此 StackOverflow 答案。