Added after_this_request decorator.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent dbfd406 · Browse files · 6 files·+51 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+51 added−2 removed
- other:+51 added−2 removed
Provenance
- Commit
086348e- Tree
a57653d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
dbfd406
6 files changed+51 added−2 removed
modified:docs/api.rst+2 added
287 unmodified lines288 .. autofunction:: redirect289 290 .. autofunction:: make_response291 292+.. autofunction:: after_this_request293+294 .. autofunction:: send_file295 296 .. autofunction:: send_from_directory297 361 unmodified linesmodified:flask/testsuite/basic.py+15 added
409 unmodified lines410 rv = app.test_client().get('/').data411 self.assert_('after' in evts)412 self.assert_equal(rv, 'request|after')413 414+ def test_after_request_processing(self):415+ app = flask.Flask(__name__)416+ app.testing = True417+ @app.route('/')418+ def index():419+ @flask.after_this_request420+ def foo(response):421+ response.headers['X-Foo'] = 'a header'422+ return response423+ return 'Test'424+ c = app.test_client()425+ resp = c.get('/')426+ self.assertEqual(resp.status_code, 200)427+ self.assertEqual(resp.headers['X-Foo'], 'a header')428+429 def test_teardown_request_handler(self):430 called = []431 app = flask.Flask(__name__)432 @app.teardown_request756 unmodified linesmodified:flask/__init__.py+2 added-1 removed
23 unmodified lines24 send_file, send_from_directory, get_flashed_messages, \25 get_template_attribute, make_response, safe_join26 from .globals import current_app, g, request, session, _request_ctx_stack, \27 _app_ctx_stack28-from .ctx import has_request_context, has_app_context28+from .ctx import has_request_context, has_app_context, \29+ after_this_request30 from .module import Module31 from .blueprints import Blueprint32 from .templating import render_template, render_template_string33 10 unmodified linesmodified:flask/app.py+1 added-1 removed
1553 unmodified lines1554 instance of :attr:`response_class`.1555 """1556 ctx = _request_ctx_stack.top1557 bp = ctx.request.blueprint1558- funcs = ()1558+ funcs = ctx._after_request_functions1559 if bp is not None and bp in self.after_request_funcs:1560 funcs = reversed(self.after_request_funcs[bp])1561 if None in self.after_request_funcs:1562 funcs = chain(funcs, reversed(self.after_request_funcs[None]))139 unmodified linesmodified:flask/ctx.py+30 added
9 unmodified lines10 """11 12 import sys13 14+from functools import partial15 from werkzeug.exceptions import HTTPException16 17 from .globals import _request_ctx_stack, _app_ctx_stack18 from .module import blueprint_is_module11 unmodified lines30 ctx.push()31 return ctx32 33 34+def after_this_request(f):35+ """Executes a function after this request. This is useful to modify36+ response objects. The function is passed the response object and has37+ to return the same or a new one.38+39+ Example::40+41+ @app.route('/')42+ def index():43+ @after_this_request44+ def add_header():45+ response.headers['X-Foo'] = 'Parachute'46+ return response47+ return 'Hello World!'48+49+ This is more useful if a function other than the view function wants to50+ modify a response. For instance think of a decorator that wants to add51+ some headers without converting the return value into a response object.52+53+ .. versionadded:: 0.954+ """55+ _request_ctx_stack.top._after_request_functions.append(f)56+ return f57+58+59 def has_request_context():60 """If you have code that wants to test if a request context is there or61 not this function can be used. For instance, you may want to take advantage62 of request information if the request object is available, but fail115 unmodified lines178 # of an application context. If it implicitly pushed an application179 # context, it will be stored there180 self._pushed_application_context = None181 182+ # Functions that should be executed after the request on the response183+ # object. These will even be called in case of an error.184+ self._after_request_functions = []185+186 self.match_request()187 188 # XXX: Support for deprecated functionality. This is going away with189 # Flask 1.094 unmodified linesmodified:CHANGES+1 added
68 unmodified lines69 - Added :attr:`flask.Flask.request_globals_class` to allow a specific class to70 be used on creation of the :data:`~flask.g` instance of each request.71 - Added `required_methods` attribute to view functions to force-add methods72 on registration.73+- Added :func:`flask.after_this_request`.74 75 Version 0.8.176 -------------77 294 unmodified lines