forked from orbit-oss/flask
Added request/response processing based on modules.
This commit is contained in:
parent
e0148a00c0
commit
5c52fe980e
2 changed files with 44 additions and 6 deletions
12
flask.py
12
flask.py
|
|
@ -336,8 +336,8 @@ class Module(object):
|
||||||
def _register_rule(self, state, rule, endpoint, view_func, options):
|
def _register_rule(self, state, rule, endpoint, view_func, options):
|
||||||
if self.url_prefix:
|
if self.url_prefix:
|
||||||
rule = state.url_prefix + rule
|
rule = state.url_prefix + rule
|
||||||
self.app.add_url_rule(rule, '%s.%s' % (self.name, endpoint),
|
state.app.add_url_rule(rule, '%s.%s' % (self.name, endpoint),
|
||||||
view_func, **options)
|
view_func, **options)
|
||||||
|
|
||||||
|
|
||||||
class Flask(object):
|
class Flask(object):
|
||||||
|
|
@ -573,8 +573,8 @@ class Flask(object):
|
||||||
|
|
||||||
def register_module(self, module, **options):
|
def register_module(self, module, **options):
|
||||||
"""Registers a module with this application."""
|
"""Registers a module with this application."""
|
||||||
options.setdefault('url_prefix', self.url_prefix)
|
options.setdefault('url_prefix', module.url_prefix)
|
||||||
state = _ModuleSetupState(app, options)
|
state = _ModuleSetupState(self, **options)
|
||||||
for func, args in module._register_events:
|
for func, args in module._register_events:
|
||||||
func(state, *args)
|
func(state, *args)
|
||||||
|
|
||||||
|
|
@ -809,9 +809,11 @@ class Flask(object):
|
||||||
mod = ctx.request.module
|
mod = ctx.request.module
|
||||||
if not isinstance(ctx.session, _NullSession):
|
if not isinstance(ctx.session, _NullSession):
|
||||||
self.save_session(ctx.session, response)
|
self.save_session(ctx.session, response)
|
||||||
funcs = self.after_request_funcs.get(None, ())
|
funcs = ()
|
||||||
if mod and mod in self.after_request_funcs:
|
if mod and mod in self.after_request_funcs:
|
||||||
funcs = chain(funcs, self.after_request_funcs[mod])
|
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:
|
for handler in funcs:
|
||||||
response = handler(response)
|
response = handler(response)
|
||||||
return response
|
return response
|
||||||
|
|
|
||||||
|
|
@ -315,13 +315,48 @@ class ModuleTestCase(unittest.TestCase):
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
return 'the index'
|
return 'the index'
|
||||||
app.register_module('admin', admin)
|
app.register_module(admin)
|
||||||
c = app.test_client()
|
c = app.test_client()
|
||||||
assert c.get('/').data == 'the index'
|
assert c.get('/').data == 'the index'
|
||||||
assert c.get('/admin/').data == 'admin index'
|
assert c.get('/admin/').data == 'admin index'
|
||||||
assert c.get('/admin/login').data == 'admin login'
|
assert c.get('/admin/login').data == 'admin login'
|
||||||
assert c.get('/admin/logout').data == 'admin logout'
|
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():
|
def suite():
|
||||||
from minitwit_tests import MiniTwitTestCase
|
from minitwit_tests import MiniTwitTestCase
|
||||||
|
|
@ -334,6 +369,7 @@ def suite():
|
||||||
suite.addTest(unittest.makeSuite(JSONTestCase))
|
suite.addTest(unittest.makeSuite(JSONTestCase))
|
||||||
suite.addTest(unittest.makeSuite(MiniTwitTestCase))
|
suite.addTest(unittest.makeSuite(MiniTwitTestCase))
|
||||||
suite.addTest(unittest.makeSuite(FlaskrTestCase))
|
suite.addTest(unittest.makeSuite(FlaskrTestCase))
|
||||||
|
suite.addTest(unittest.makeSuite(ModuleTestCase))
|
||||||
return suite
|
return suite
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue