Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add @template_test() decorator for creating custom jinja2 tests. Fixes #332 #597

Closed
wants to merge 1 commit into from

2 participants

@techniq

No description provided.

@mitsuhiko
Owner

Thank you, I applied this with added versionadded parts to the docs.

@mitsuhiko mitsuhiko closed this
@techniq

Welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 26, 2012
  1. @techniq

    Add @template_test() decorator for creating custom jinja2 tests, like…

    techniq authored
    … existing @template_filter() for filters. Fixes #332
This page is out of date. Refresh to see the latest.
View
34 flask/app.py
@@ -1087,6 +1087,40 @@ def add_template_filter(self, f, name=None):
self.jinja_env.filters[name or f.__name__] = f
@setupmethod
+ def template_test(self, name=None):
+ """A decorator that is used to register custom template test.
+ You can specify a name for the test, otherwise the function
+ name will be used. Example::
+
+ @app.template_test()
+ def is_prime(n):
+ if n == 2:
+ return True
+ for i in xrange(2, int(math.ceil(math.sqrt(n))) + 1):
+ if n % i == 0:
+ return False
+ return True
+
+ :param name: the optional name of the test, otherwise the
+ function name will be used.
+ """
+ def decorator(f):
+ self.add_template_test(f, name=name)
+ return f
+ return decorator
+
+ @setupmethod
+ def add_template_test(self, f, name=None):
+ """Register a custom template test. Works exactly like the
+ :meth:`template_test` decorator.
+
+ :param name: the optional name of the test, otherwise the
+ function name will be used.
+ """
+ self.jinja_env.tests[name or f.__name__] = f
+
+
+ @setupmethod
def before_request(self, f):
"""Registers a function to run before each request."""
self.before_request_funcs.setdefault(None, []).append(f)
View
24 flask/blueprints.py
@@ -209,6 +209,30 @@ def register_template(state):
state.app.jinja_env.filters[name or f.__name__] = f
self.record_once(register_template)
+ def app_template_test(self, name=None):
+ """Register a custom template test, available application wide. Like
+ :meth:`Flask.template_test` but for a blueprint.
+
+ :param name: the optional name of the test, otherwise the
+ function name will be used.
+ """
+ def decorator(f):
+ self.add_app_template_test(f, name=name)
+ return f
+ return decorator
+
+ def add_app_template_test(self, f, name=None):
+ """Register a custom template test, available application wide. Like
+ :meth:`Flask.add_template_test` but for a blueprint. Works exactly
+ like the :meth:`app_template_test` decorator.
+
+ :param name: the optional name of the test, otherwise the
+ function name will be used.
+ """
+ def register_template(state):
+ state.app.jinja_env.tests[name or f.__name__] = f
+ self.record_once(register_template)
+
def before_request(self, f):
"""Like :meth:`Flask.before_request` but for a blueprint. This function
is only executed before each request that is handled by a function of
View
116 flask/testsuite/blueprints.py
@@ -548,7 +548,7 @@ def my_reverse(s):
return s[::-1]
app = flask.Flask(__name__)
app.register_blueprint(bp, url_prefix='/py')
- self.assert_('my_reverse' in app.jinja_env.filters.keys())
+ self.assert_('my_reverse' in app.jinja_env.filters.keys())
self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse)
self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba')
@@ -559,7 +559,7 @@ def my_reverse(s):
bp.add_app_template_filter(my_reverse)
app = flask.Flask(__name__)
app.register_blueprint(bp, url_prefix='/py')
- self.assert_('my_reverse' in app.jinja_env.filters.keys())
+ self.assert_('my_reverse' in app.jinja_env.filters.keys())
self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse)
self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba')
@@ -570,7 +570,7 @@ def my_reverse(s):
return s[::-1]
app = flask.Flask(__name__)
app.register_blueprint(bp, url_prefix='/py')
- self.assert_('strrev' in app.jinja_env.filters.keys())
+ self.assert_('strrev' in app.jinja_env.filters.keys())
self.assert_equal(app.jinja_env.filters['strrev'], my_reverse)
self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba')
@@ -581,7 +581,7 @@ def my_reverse(s):
bp.add_app_template_filter(my_reverse, 'strrev')
app = flask.Flask(__name__)
app.register_blueprint(bp, url_prefix='/py')
- self.assert_('strrev' in app.jinja_env.filters.keys())
+ self.assert_('strrev' in app.jinja_env.filters.keys())
self.assert_equal(app.jinja_env.filters['strrev'], my_reverse)
self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba')
@@ -650,6 +650,114 @@ def index():
rv = app.test_client().get('/')
self.assert_equal(rv.data, 'dcba')
+ def test_template_test(self):
+ bp = flask.Blueprint('bp', __name__)
+ @bp.app_template_test()
+ def is_boolean(value):
+ return isinstance(value, bool)
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+ self.assert_('is_boolean' in app.jinja_env.tests.keys())
+ self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean)
+ self.assert_(app.jinja_env.tests['is_boolean'](False))
+
+ def test_add_template_test(self):
+ bp = flask.Blueprint('bp', __name__)
+ def is_boolean(value):
+ return isinstance(value, bool)
+ bp.add_app_template_test(is_boolean)
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+ self.assert_('is_boolean' in app.jinja_env.tests.keys())
+ self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean)
+ self.assert_(app.jinja_env.tests['is_boolean'](False))
+
+ def test_template_test_with_name(self):
+ bp = flask.Blueprint('bp', __name__)
+ @bp.app_template_test('boolean')
+ def is_boolean(value):
+ return isinstance(value, bool)
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+ self.assert_('boolean' in app.jinja_env.tests.keys())
+ self.assert_equal(app.jinja_env.tests['boolean'], is_boolean)
+ self.assert_(app.jinja_env.tests['boolean'](False))
+
+ def test_add_template_test_with_name(self):
+ bp = flask.Blueprint('bp', __name__)
+ def is_boolean(value):
+ return isinstance(value, bool)
+ bp.add_app_template_test(is_boolean, 'boolean')
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+ self.assert_('boolean' in app.jinja_env.tests.keys())
+ self.assert_equal(app.jinja_env.tests['boolean'], is_boolean)
+ self.assert_(app.jinja_env.tests['boolean'](False))
+
+ def test_template_test_with_template(self):
+ bp = flask.Blueprint('bp', __name__)
+ @bp.app_template_test()
+ def boolean(value):
+ return isinstance(value, bool)
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
+
+ def test_template_test_after_route_with_template(self):
+ app = flask.Flask(__name__)
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ bp = flask.Blueprint('bp', __name__)
+ @bp.app_template_test()
+ def boolean(value):
+ return isinstance(value, bool)
+ app.register_blueprint(bp, url_prefix='/py')
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
+
+ def test_add_template_test_with_template(self):
+ bp = flask.Blueprint('bp', __name__)
+ def boolean(value):
+ return isinstance(value, bool)
+ bp.add_app_template_test(boolean)
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
+
+ def test_template_test_with_name_and_template(self):
+ bp = flask.Blueprint('bp', __name__)
+ @bp.app_template_test('boolean')
+ def is_boolean(value):
+ return isinstance(value, bool)
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
+
+ def test_add_template_test_with_name_and_template(self):
+ bp = flask.Blueprint('bp', __name__)
+ def is_boolean(value):
+ return isinstance(value, bool)
+ bp.add_app_template_test(is_boolean, 'boolean')
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
def suite():
suite = unittest.TestSuite()
View
3  flask/testsuite/templates/template_test.html
@@ -0,0 +1,3 @@
+{% if value is boolean %}
+ Success!
+{% endif %}
View
91 flask/testsuite/templating.py
@@ -89,7 +89,7 @@ def test_template_filter(self):
@app.template_filter()
def my_reverse(s):
return s[::-1]
- self.assert_('my_reverse' in app.jinja_env.filters.keys())
+ self.assert_('my_reverse' in app.jinja_env.filters.keys())
self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse)
self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba')
@@ -98,7 +98,7 @@ def test_add_template_filter(self):
def my_reverse(s):
return s[::-1]
app.add_template_filter(my_reverse)
- self.assert_('my_reverse' in app.jinja_env.filters.keys())
+ self.assert_('my_reverse' in app.jinja_env.filters.keys())
self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse)
self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba')
@@ -107,7 +107,7 @@ def test_template_filter_with_name(self):
@app.template_filter('strrev')
def my_reverse(s):
return s[::-1]
- self.assert_('strrev' in app.jinja_env.filters.keys())
+ self.assert_('strrev' in app.jinja_env.filters.keys())
self.assert_equal(app.jinja_env.filters['strrev'], my_reverse)
self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba')
@@ -116,7 +116,7 @@ def test_add_template_filter_with_name(self):
def my_reverse(s):
return s[::-1]
app.add_template_filter(my_reverse, 'strrev')
- self.assert_('strrev' in app.jinja_env.filters.keys())
+ self.assert_('strrev' in app.jinja_env.filters.keys())
self.assert_equal(app.jinja_env.filters['strrev'], my_reverse)
self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba')
@@ -164,6 +164,86 @@ def index():
rv = app.test_client().get('/')
self.assert_equal(rv.data, 'dcba')
+ def test_template_test(self):
+ app = flask.Flask(__name__)
+ @app.template_test()
+ def boolean(value):
+ return isinstance(value, bool)
+ self.assert_('boolean' in app.jinja_env.tests.keys())
+ self.assert_equal(app.jinja_env.tests['boolean'], boolean)
+ self.assert_(app.jinja_env.tests['boolean'](False))
+
+ def test_add_template_test(self):
+ app = flask.Flask(__name__)
+ def boolean(value):
+ return isinstance(value, bool)
+ app.add_template_test(boolean)
+ self.assert_('boolean' in app.jinja_env.tests.keys())
+ self.assert_equal(app.jinja_env.tests['boolean'], boolean)
+ self.assert_(app.jinja_env.tests['boolean'](False))
+
+ def test_template_test_with_name(self):
+ app = flask.Flask(__name__)
+ @app.template_test('boolean')
+ def is_boolean(value):
+ return isinstance(value, bool)
+ self.assert_('boolean' in app.jinja_env.tests.keys())
+ self.assert_equal(app.jinja_env.tests['boolean'], is_boolean)
+ self.assert_(app.jinja_env.tests['boolean'](False))
+
+ def test_add_template_test_with_name(self):
+ app = flask.Flask(__name__)
+ def is_boolean(value):
+ return isinstance(value, bool)
+ app.add_template_test(is_boolean, 'boolean')
+ self.assert_('boolean' in app.jinja_env.tests.keys())
+ self.assert_equal(app.jinja_env.tests['boolean'], is_boolean)
+ self.assert_(app.jinja_env.tests['boolean'](False))
+
+ def test_template_test_with_template(self):
+ app = flask.Flask(__name__)
+ @app.template_test()
+ def boolean(value):
+ return isinstance(value, bool)
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
+
+ def test_add_template_test_with_template(self):
+ app = flask.Flask(__name__)
+ def boolean(value):
+ return isinstance(value, bool)
+ app.add_template_test(boolean)
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
+
+ def test_template_test_with_name_and_template(self):
+ app = flask.Flask(__name__)
+ @app.template_test('boolean')
+ def is_boolean(value):
+ return isinstance(value, bool)
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
+
+ def test_add_template_test_with_name_and_template(self):
+ app = flask.Flask(__name__)
+ def is_boolean(value):
+ return isinstance(value, bool)
+ app.add_template_test(is_boolean, 'boolean')
+ @app.route('/')
+ def index():
+ return flask.render_template('template_test.html', value=False)
+ rv = app.test_client().get('/')
+ self.assert_('Success!' in rv.data)
+
def test_custom_template_loader(self):
class MyFlask(flask.Flask):
def create_global_jinja_loader(self):
@@ -177,7 +257,6 @@ def index():
rv = c.get('/')
self.assert_equal(rv.data, 'Hello Custom World!')
-
def test_iterable_loader(self):
app = flask.Flask(__name__)
@app.context_processor
@@ -195,8 +274,6 @@ def index():
self.assert_equal(rv.data, '<h1>Jameson</h1>')
-
-
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TemplatingTestCase))
Something went wrong with that request. Please try again.