From 08ac3aa4e3739e20d385c4c2973893945f228470 Mon Sep 17 00:00:00 2001 From: ThiefMaster Date: Mon, 6 Apr 2015 14:55:01 +0200 Subject: [PATCH 1/2] Allow custom jinja environments This is useful e.g. when using the new Jinja Environment attributes added in mitsuhiko/jinja2#404 --- flask/app.py | 7 ++++++- tests/test_templating.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/flask/app.py b/flask/app.py index 1f7df2e8..54ebc358 100644 --- a/flask/app.py +++ b/flask/app.py @@ -157,6 +157,11 @@ class Flask(_PackageBoundObject): #: :class:`~flask.Response` for more information. response_class = Response + #: The class that is used for the Jinja environment. + #: + #: .. versionadded:: 0.11 + jinja_env_class = Environment + #: The class that is used for the :data:`~flask.g` instance. #: #: Example use cases for a custom class: @@ -680,7 +685,7 @@ class Flask(_PackageBoundObject): options['auto_reload'] = self.config['TEMPLATES_AUTO_RELOAD'] else: options['auto_reload'] = self.debug - rv = Environment(self, **options) + rv = self.jinja_env_class(self, **options) rv.globals.update( url_for=url_for, get_flashed_messages=get_flashed_messages, diff --git a/tests/test_templating.py b/tests/test_templating.py index 7285c18c..132f42a4 100644 --- a/tests/test_templating.py +++ b/tests/test_templating.py @@ -361,3 +361,13 @@ def test_template_loader_debugging(test_apps): app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting assert len(called) == 1 + +def test_custom_jinja_env(): + class CustomEnvironment(flask.templating.Environment): + pass + + class CustomFlask(flask.Flask): + jinja_env_class = CustomEnvironment + + app = CustomFlask(__name__) + assert isinstance(app.jinja_env, CustomEnvironment) From 2446ca63a8d840a35ea4eac02672b24a6fcf406f Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sat, 6 Jun 2015 03:29:44 +0200 Subject: [PATCH 2/2] 0.11 => 1.0 --- flask/app.py | 2 +- flask/blueprints.py | 2 +- flask/wrappers.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/flask/app.py b/flask/app.py index 54ebc358..7e045ff4 100644 --- a/flask/app.py +++ b/flask/app.py @@ -159,7 +159,7 @@ class Flask(_PackageBoundObject): #: The class that is used for the Jinja environment. #: - #: .. versionadded:: 0.11 + #: .. versionadded:: 1.0 jinja_env_class = Environment #: The class that is used for the :data:`~flask.g` instance. diff --git a/flask/blueprints.py b/flask/blueprints.py index 8bbc9521..3c3cf7c6 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -408,7 +408,7 @@ class Blueprint(_PackageBoundObject): application-wide function of the :class:`~flask.Flask` object but for error handlers limited to this blueprint. - .. versionadded:: 0.11 + .. versionadded:: 1.0 """ self.record_once(lambda s: s.app._register_error_handler( self.name, code_or_exception, f)) diff --git a/flask/wrappers.py b/flask/wrappers.py index de5b21a1..8b1ca251 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -113,7 +113,7 @@ class Request(RequestBase): is considered to include JSON data if the mimetype is :mimetype:`application/json` or :mimetype:`application/*+json`. - .. versionadded:: 0.11 + .. versionadded:: 1.0 """ mt = self.mimetype if mt == 'application/json':