Browse Source

Added missing decorators for module wide context processors.

This fixes #25.
pull/1638/head
Armin Ronacher 15 years ago
parent
commit
745638e774
  1. 20
      flask.py
  2. 23
      tests/flask_tests.py

20
flask.py

@ -465,7 +465,7 @@ class Module(_PackageBoundObject):
def before_app_request(self, f): def before_app_request(self, f):
"""Like :meth:`Flask.before_request`. Such a function is executed """Like :meth:`Flask.before_request`. Such a function is executed
before each request. before each request, even if outside of a module.
""" """
self._record(lambda s: s.app.before_request_funcs self._record(lambda s: s.app.before_request_funcs
.setdefault(None, []).append(f)) .setdefault(None, []).append(f))
@ -482,12 +482,28 @@ class Module(_PackageBoundObject):
def after_app_request(self, f): def after_app_request(self, f):
"""Like :meth:`Flask.after_request` but for a module. Such a function """Like :meth:`Flask.after_request` but for a module. Such a function
is executed after each request. is executed after each request, even if outside of the module.
""" """
self._record(lambda s: s.app.after_request_funcs self._record(lambda s: s.app.after_request_funcs
.setdefault(None, []).append(f)) .setdefault(None, []).append(f))
return f return f
def context_processor(self, f):
"""Like :meth:`Flask.context_processor` but for a modul. This
function is only executed for requests handled by a module.
"""
self._record(lambda s: s.app.template_context_processors
.setdefault(self.name, []).append(f))
return f
def app_context_processor(self, f):
"""Like :meth:`Flask.context_processor` but for a module. Such a
function is executed each request, even if outside of the module.
"""
self._record(lambda s: s.app.template_context_processors
.setdefault(None, []).append(f))
return f
def _record(self, func): def _record(self, func):
self._register_events.append(func) self._register_events.append(func)

23
tests/flask_tests.py

@ -441,6 +441,29 @@ class ModuleTestCase(unittest.TestCase):
assert catched == ['before-app', 'before-admin', assert catched == ['before-app', 'before-admin',
'after-admin', 'after-app'] 'after-admin', 'after-app']
def test_context_processors(self):
app = flask.Flask(__name__)
admin = flask.Module(__name__, 'admin', url_prefix='/admin')
@app.context_processor
def inject_all_regualr():
return {'a': 1}
@admin.context_processor
def inject_admin():
return {'b': 2}
@admin.app_context_processor
def inject_all_module():
return {'c': 3}
@app.route('/')
def index():
return flask.render_template_string('{{ a }}{{ b }}{{ c }}')
@admin.route('/')
def index():
return flask.render_template_string('{{ a }}{{ b }}{{ c }}')
app.register_module(admin)
c = app.test_client()
assert c.get('/').data == '13'
assert c.get('/admin/').data == '123'
def test_late_binding(self): def test_late_binding(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
admin = flask.Module(__name__, 'admin') admin = flask.Module(__name__, 'admin')

Loading…
Cancel
Save