forked from orbit-oss/flask
Added after_this_request decorator.
This commit is contained in:
parent
dbfd406a21
commit
086348e2f2
6 changed files with 51 additions and 2 deletions
30
flask/ctx.py
30
flask/ctx.py
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
import sys
|
||||
|
||||
from functools import partial
|
||||
from werkzeug.exceptions import HTTPException
|
||||
|
||||
from .globals import _request_ctx_stack, _app_ctx_stack
|
||||
|
|
@ -30,6 +31,31 @@ def _push_app_if_necessary(app):
|
|||
return ctx
|
||||
|
||||
|
||||
def after_this_request(f):
|
||||
"""Executes a function after this request. This is useful to modify
|
||||
response objects. The function is passed the response object and has
|
||||
to return the same or a new one.
|
||||
|
||||
Example::
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
@after_this_request
|
||||
def add_header():
|
||||
response.headers['X-Foo'] = 'Parachute'
|
||||
return response
|
||||
return 'Hello World!'
|
||||
|
||||
This is more useful if a function other than the view function wants to
|
||||
modify a response. For instance think of a decorator that wants to add
|
||||
some headers without converting the return value into a response object.
|
||||
|
||||
.. versionadded:: 0.9
|
||||
"""
|
||||
_request_ctx_stack.top._after_request_functions.append(f)
|
||||
return f
|
||||
|
||||
|
||||
def has_request_context():
|
||||
"""If you have code that wants to test if a request context is there or
|
||||
not this function can be used. For instance, you may want to take advantage
|
||||
|
|
@ -153,6 +179,10 @@ class RequestContext(object):
|
|||
# context, it will be stored there
|
||||
self._pushed_application_context = None
|
||||
|
||||
# Functions that should be executed after the request on the response
|
||||
# object. These will even be called in case of an error.
|
||||
self._after_request_functions = []
|
||||
|
||||
self.match_request()
|
||||
|
||||
# XXX: Support for deprecated functionality. This is going away with
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue