Skip to content
Browse files

Merge branch 'development' of https://github.com/TurboGears/tg2 into …

…development
  • Loading branch information...
2 parents 89a890a + 8af6bf8 commit c2e2cd3fac87a6a1b78b7f4a23ded82726e6dc75 @amol- amol- committed Nov 24, 2012
View
2 tests/test_stack/lib/templatetools/jinja_filters.py
@@ -10,3 +10,5 @@ def codify(value):
string_hash = sha1(value)
return string_hash.hexdigest()
+def polluting_function(value):
+ return "Template filter namespace has been POLLUTED"
View
4 tests/test_stack/rendering/controllers/root.py
@@ -180,6 +180,10 @@ def genshi_inherits_sub_from_bottom(self):
def jinja_index(self):
return {}
+ @expose('jinja:jinja_autoload.jinja')
+ def jinja_autoload(self):
+ return {}
+
@expose('jinja:jinja_inherits.jinja')
def jinja_inherits(self):
return {}
View
18 tests/test_stack/rendering/templates/jinja_autoload.jinja
@@ -0,0 +1,18 @@
+{% extends "jinja_base.jinja" %}
+
+{% block title %}Index{% endblock %}
+
+{% block html_head %}
+ <style type="text/css">
+ .important {
+ color: #336699;
+ }
+ </style>
+{% endblock %}
+
+{% block content %}
+ <h1>Index</h1>
+ <p class="important">
+ {{ "Hello Jinja!"|polluting_function }}
+ </p>
+{% endblock %}
View
16 tests/test_stack/rendering/test_rendering.py
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
+from jinja2 import TemplateAssertionError
import tg
from tests.test_stack import TestConfig, app_from_config
@@ -276,6 +277,21 @@ def test_chameleon_genshi_inheritance():
assert "Inheritance template" in resp
assert "Master template" in resp
+def test_jinja_autoload():
+ app = setup_noDB()
+ try:
+ resp = app.get('/jinja_autoload')
+
+ # Normally the template should not load, if it does
+ # check if the filter namespace has been polluted.
+ assert not ("POLLUTED" in resp), resp
+ except TemplateAssertionError:
+ # If autoloading is working ok a template
+ # getting a function not in __all__ should raise
+ # a template exception, thus this is the normal behaviour.
+ pass
+
+
def _test_jinja_inherits():
app = setup_noDB()
resp = app.get('/jinja_inherits')
View
10 tg/configuration/app_config.py
@@ -488,7 +488,15 @@ def setup_jinja_renderer(self):
try:
filter_package = self.package.__name__ + ".lib.templatetools"
autoload_lib = __import__(filter_package, {}, {}, ['jinja_filters'])
- autoload_filters = autoload_lib.jinja_filters.__dict__
+ try:
+ autoload_filters = dict(
+ map(lambda x: (x,autoload_lib.jinja_filters.__dict__[x]), autoload_lib.jinja_filters.__all__)
+ )
+ except AttributeError:
+ autoload_filters = dict(
+ filter(lambda x: callable(x[1]),
+ autoload_lib.jinja_filters.__dict__.iteritems())
+ )
except (ImportError, AttributeError):
autoload_filters = {}

0 comments on commit c2e2cd3

Please sign in to comment.
Something went wrong with that request. Please try again.