Skip to content
Myr

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

2 files+44 added−6 removed

modified:tests/flask_tests.py+37 added-1 removed
313 unmodified lines
314 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'324325 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()351352 assert c.get('/').data == 'the index'353 assert catched == ['before-app', 'after-app']354 del catched[:]355356 assert c.get('/admin/').data == 'the admin'357 assert catched == ['before-app', 'before-admin',358 'after-admin', 'after-app']359360361def suite():362 from minitwit_tests import MiniTwitTestCase363 from flaskr_tests import FlaskrTestCase364 suite = unittest.TestSuite()
3 unmodified lines
368 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 suite374375376if __name__ == '__main__':
1 unmodified line
modified:flask.py+7 added-5 removed
334 unmodified lines
335336 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)341342343class Flask(object):344 """The flask object implements a WSGI application and acts as the central
227 unmodified lines
572 session.save_cookie(response, self.session_cookie_name)573574 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)580581 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
226 unmodified lines
808 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