Skip to content
Myr

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

6 files+51 added−2 removed

modified:docs/api.rst+2 added
287 unmodified lines
288.. autofunction:: redirect289290.. autofunction:: make_response291292.. autofunction:: after_this_request293294.. autofunction:: send_file295296.. autofunction:: send_from_directory297
361 unmodified lines
modified:flask/testsuite/basic.py+15 added
409 unmodified lines
410 rv = app.test_client().get('/').data411 self.assert_('after' in evts)412 self.assert_equal(rv, 'request|after')413414 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')428429 def test_teardown_request_handler(self):430 called = []431 app = flask.Flask(__name__)432 @app.teardown_request
756 unmodified lines
modified:flask/__init__.py+2 added-1 removed
23 unmodified lines
24 send_file, send_from_directory, get_flashed_messages, \25 get_template_attribute, make_response, safe_join26from .globals import current_app, g, request, session, _request_ctx_stack, \27 _app_ctx_stack28from .ctx import has_request_context, has_app_context28from .ctx import has_request_context, has_app_context, \29 after_this_request30from .module import Module31from .blueprints import Blueprint32from .templating import render_template, render_template_string33
10 unmodified lines
modified:flask/app.py+1 added-1 removed
1553 unmodified lines
1554 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 lines
modified:flask/ctx.py+30 added
9 unmodified lines
10"""1112import sys1314from functools import partial15from werkzeug.exceptions import HTTPException1617from .globals import _request_ctx_stack, _app_ctx_stack18from .module import blueprint_is_module
11 unmodified lines
30 ctx.push()31 return ctx323334def 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.3839 Example::4041 @app.route('/')42 def index():43 @after_this_request44 def add_header():45 response.headers['X-Foo'] = 'Parachute'46 return response47 return 'Hello World!'4849 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.5253 .. versionadded:: 0.954 """55 _request_ctx_stack.top._after_request_functions.append(f)56 return f575859def 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 fail
115 unmodified lines
178 # of an application context. If it implicitly pushed an application179 # context, it will be stored there180 self._pushed_application_context = None181182 # 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 = []185186 self.match_request()187188 # XXX: Support for deprecated functionality. This is going away with189 # Flask 1.0
94 unmodified lines
modified:CHANGES+1 added
68 unmodified lines
69- 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`.7475Version 0.8.176-------------77
294 unmodified lines