From 5c52fe980eda6065edcc39e6dd346636b1c40768 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 23 Apr 2010 17:10:22 +0200 Subject: [PATCH] Added request/response processing based on modules. --- flask.py | 12 +++++++----- tests/flask_tests.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/flask.py b/flask.py index 94072ce9..8d516181 100644 --- a/flask.py +++ b/flask.py @@ -336,8 +336,8 @@ class Module(object): def _register_rule(self, state, rule, endpoint, view_func, options): if self.url_prefix: rule = state.url_prefix + rule - self.app.add_url_rule(rule, '%s.%s' % (self.name, endpoint), - view_func, **options) + state.app.add_url_rule(rule, '%s.%s' % (self.name, endpoint), + view_func, **options) class Flask(object): @@ -573,8 +573,8 @@ class Flask(object): def register_module(self, module, **options): """Registers a module with this application.""" - options.setdefault('url_prefix', self.url_prefix) - state = _ModuleSetupState(app, options) + options.setdefault('url_prefix', module.url_prefix) + state = _ModuleSetupState(self, **options) for func, args in module._register_events: func(state, *args) @@ -809,9 +809,11 @@ class Flask(object): mod = ctx.request.module if not isinstance(ctx.session, _NullSession): self.save_session(ctx.session, response) - funcs = self.after_request_funcs.get(None, ()) + funcs = () if mod and mod in self.after_request_funcs: funcs = chain(funcs, self.after_request_funcs[mod]) + if None in self.after_request_funcs: + funcs = chain(funcs, self.after_request_funcs[None]) for handler in funcs: response = handler(response) return response diff --git a/tests/flask_tests.py b/tests/flask_tests.py index 2f372514..e57b2f32 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -315,13 +315,48 @@ class ModuleTestCase(unittest.TestCase): @app.route('/') def index(): return 'the index' - app.register_module('admin', admin) + app.register_module(admin) c = app.test_client() assert c.get('/').data == 'the index' assert c.get('/admin/').data == 'admin index' assert c.get('/admin/login').data == 'admin login' assert c.get('/admin/logout').data == 'admin logout' + def test_request_processing(self): + catched = [] + app = flask.Flask(__name__) + admin = flask.Module('admin', url_prefix='/admin') + @admin.before_request + def before_admin_request(): + catched.append('before-admin') + @admin.after_request + def after_admin_request(response): + catched.append('after-admin') + return response + @admin.route('/') + def index(): + return 'the admin' + @app.before_request + def before_request(): + catched.append('before-app') + @app.after_request + def after_request(response): + catched.append('after-app') + return response + @app.route('/') + def index(): + return 'the index' + app.register_module(admin) + c = app.test_client() + + assert c.get('/').data == 'the index' + assert catched == ['before-app', 'after-app'] + del catched[:] + + assert c.get('/admin/').data == 'the admin' + assert catched == ['before-app', 'before-admin', + 'after-admin', 'after-app'] + def suite(): from minitwit_tests import MiniTwitTestCase @@ -334,6 +369,7 @@ def suite(): suite.addTest(unittest.makeSuite(JSONTestCase)) suite.addTest(unittest.makeSuite(MiniTwitTestCase)) suite.addTest(unittest.makeSuite(FlaskrTestCase)) + suite.addTest(unittest.makeSuite(ModuleTestCase)) return suite