Added request/response processing based on modules.
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 e0148a0 · Browse files · 2 files·+44 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+44 added−6 removed
- tests:+37 added−1 removed
- other:+7 added−5 removed
Provenance
- Commit
5c52fe9- Tree
3f88432- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
e0148a0
2 files changed+44 added−6 removed
modified:tests/flask_tests.py+37 added-1 removed
313 unmodified lines314 return 'admin logout'315 @app.route('/')316 def index():317 return 'the index'318- app.register_module('admin', admin)318+ app.register_module(admin)319 c = app.test_client()320 assert c.get('/').data == 'the index'321 assert c.get('/admin/').data == 'admin index'322 assert c.get('/admin/login').data == 'admin login'323 assert c.get('/admin/logout').data == 'admin logout'324 325+ def test_request_processing(self):326+ catched = []327+ app = flask.Flask(__name__)328+ admin = flask.Module('admin', url_prefix='/admin')329+ @admin.before_request330+ def before_admin_request():331+ catched.append('before-admin')332+ @admin.after_request333+ def after_admin_request(response):334+ catched.append('after-admin')335+ return response336+ @admin.route('/')337+ def index():338+ return 'the admin'339+ @app.before_request340+ def before_request():341+ catched.append('before-app')342+ @app.after_request343+ def after_request(response):344+ catched.append('after-app')345+ return response346+ @app.route('/')347+ def index():348+ return 'the index'349+ app.register_module(admin)350+ c = app.test_client()351 352+ assert c.get('/').data == 'the index'353+ assert catched == ['before-app', 'after-app']354+ del catched[:]355+356+ assert c.get('/admin/').data == 'the admin'357+ assert catched == ['before-app', 'before-admin',358+ 'after-admin', 'after-app']359+360+361 def suite():362 from minitwit_tests import MiniTwitTestCase363 from flaskr_tests import FlaskrTestCase364 suite = unittest.TestSuite()3 unmodified lines368 if flask.json_available:369 suite.addTest(unittest.makeSuite(JSONTestCase))370 suite.addTest(unittest.makeSuite(MiniTwitTestCase))371 suite.addTest(unittest.makeSuite(FlaskrTestCase))372+ suite.addTest(unittest.makeSuite(ModuleTestCase))373 return suite374 375 376 if __name__ == '__main__':1 unmodified linemodified:flask.py+7 added-5 removed
334 unmodified lines335 336 def _register_rule(self, state, rule, endpoint, view_func, options):337 if self.url_prefix:338 rule = state.url_prefix + rule339- self.app.add_url_rule(rule, '%s.%s' % (self.name, endpoint),340- view_func, **options)339+ state.app.add_url_rule(rule, '%s.%s' % (self.name, endpoint),340+ view_func, **options)341 342 343 class Flask(object):344 """The flask object implements a WSGI application and acts as the central227 unmodified lines572 session.save_cookie(response, self.session_cookie_name)573 574 def register_module(self, module, **options):575 """Registers a module with this application."""576- options.setdefault('url_prefix', self.url_prefix)577- state = _ModuleSetupState(app, options)576+ options.setdefault('url_prefix', module.url_prefix)577+ state = _ModuleSetupState(self, **options)578 for func, args in module._register_events:579 func(state, *args)580 581 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):226 unmodified lines808 ctx = _request_ctx_stack.top809 mod = ctx.request.module810 if not isinstance(ctx.session, _NullSession):811 self.save_session(ctx.session, response)812- funcs = self.after_request_funcs.get(None, ())812+ funcs = ()813 if mod and mod in self.after_request_funcs:814 funcs = chain(funcs, self.after_request_funcs[mod])815+ if None in self.after_request_funcs:816+ funcs = chain(funcs, self.after_request_funcs[None])817 for handler in funcs:818 response = handler(response)819 return response820 60 unmodified lines