From 7e6c70a133c833ef08f83a7e5cc926bf6e5581f1 Mon Sep 17 00:00:00 2001 From: Ron DuPlain Date: Mon, 27 Jun 2011 09:23:31 -0400 Subject: [PATCH] Add teardown request decorators to blueprints. --- flask/blueprints.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/flask/blueprints.py b/flask/blueprints.py index 4a3f531a..a0dfd521 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -216,6 +216,26 @@ class Blueprint(_PackageBoundObject): .setdefault(None, []).append(f)) return f + def teardown_request(self, f): + """Like :meth:`Flask.teardown_request` but for a blueprint. This + function is only executed when tearing down requests handled by a + function of that blueprint. Teardown request functions are executed + when the request context is popped, even when no actual request was + performed. + """ + self.record_once(lambda s: s.app.teardown_request_funcs + .setdefault(self.name, []).append(f)) + return f + + def teardown_app_request(self, f): + """Like :meth:`Flask.teardown_request` but for a blueprint. Such a + function is executed when tearing down each request, even if outside of + the blueprint. + """ + self.record_once(lambda s: s.app.teardown_request_funcs + .setdefault(None, []).append(f)) + return f + def context_processor(self, f): """Like :meth:`Flask.context_processor` but for a blueprint. This function is only executed for requests handled by a blueprint.