Permalink
Browse files

Added missing decorators for module wide context processors.

This fixes #25.
  • Loading branch information...
1 parent 7f83790 commit 745638e774063de206aae930df71789de13e7c73 @mitsuhiko mitsuhiko committed May 4, 2010
Showing with 41 additions and 2 deletions.
  1. +18 −2 flask.py
  2. +23 −0 tests/flask_tests.py
View
@@ -465,7 +465,7 @@ def before_request(self, f):
def before_app_request(self, f):
"""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
.setdefault(None, []).append(f))
@@ -482,12 +482,28 @@ def after_request(self, f):
def after_app_request(self, f):
"""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
.setdefault(None, []).append(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):
self._register_events.append(func)
View
@@ -441,6 +441,29 @@ def index():
assert catched == ['before-app', 'before-admin',
'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):
app = flask.Flask(__name__)
admin = flask.Module(__name__, 'admin')

0 comments on commit 745638e

Please sign in to comment.