diff options
| author | Alexis Métaireau <alexis@notmyidea.org> | 2018-01-14 16:52:52 +0100 |
|---|---|---|
| committer | Alexis Métaireau <alexis@notmyidea.org> | 2018-01-14 18:37:10 +0100 |
| commit | 230eafdf58c46b983936cbf4f70b712bbddfd8c9 (patch) | |
| tree | ac2a5e6de21340d1091672eff6b4ccf04114aed1 /ihatemoney/utils.py | |
| parent | 0504fd82f52317d902e85a6e448048d5ef1c58d9 (diff) | |
| download | ihatemoney-mirror-230eafdf58c46b983936cbf4f70b712bbddfd8c9.zip ihatemoney-mirror-230eafdf58c46b983936cbf4f70b712bbddfd8c9.tar.gz ihatemoney-mirror-230eafdf58c46b983936cbf4f70b712bbddfd8c9.tar.bz2 | |
Use Jinja2 strict rendering.
For this I had to create an Jinja2 explicit environment, so I put a
function in `ihatemoney.utils.create_jinja2_env(strict_rendering=False)`.
When using this environment and if `strict_rendering` is activated,
templates using undefined variables will now error out rather than
failing silently.
Diffstat (limited to 'ihatemoney/utils.py')
| -rw-r--r-- | ihatemoney/utils.py | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py index 6af0112..a25e3b9 100644 --- a/ihatemoney/utils.py +++ b/ihatemoney/utils.py @@ -2,7 +2,7 @@ import base64 import re from io import BytesIO, StringIO -from jinja2 import filters +import jinja2 from json import dumps from flask import redirect from werkzeug.routing import HTTPException, RoutingException @@ -83,7 +83,7 @@ def minimal_round(*args, **kw): from http://stackoverflow.com/questions/28458524/ """ # Use the original round filter, to deal with the extra arguments - res = filters.do_round(*args, **kw) + res = jinja2.filters.do_round(*args, **kw) # Test if the result is equivalent to an integer and # return depending on it ires = int(res) @@ -170,3 +170,18 @@ class LoginThrottler(): def reset(self, ip): self._attempts.pop(ip, None) + + +def create_jinja_env(folder, strict_rendering=False): + """Creates and return a Jinja2 Environment object, used, to load the + templates. + + :param strict_rendering: + if set to `True`, all templates which use an undefined variable will + throw an exception (default to `False`). + """ + loader = jinja2.PackageLoader('ihatemoney', folder) + kwargs = {'loader': loader} + if strict_rendering: + kwargs['undefined'] = jinja2.StrictUndefined + return jinja2.Environment(**kwargs) |
