Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/TurboGears/tg2 into …
Browse files Browse the repository at this point in the history
…development
  • Loading branch information
amol- committed Nov 24, 2012
2 parents 89a890a + 8af6bf8 commit c2e2cd3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tests/test_stack/lib/templatetools/jinja_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions tests/test_stack/rendering/controllers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
18 changes: 18 additions & 0 deletions tests/test_stack/rendering/templates/jinja_autoload.jinja
Original file line number Diff line number Diff line change
@@ -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 %}
16 changes: 16 additions & 0 deletions tests/test_stack/rendering/test_rendering.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from jinja2 import TemplateAssertionError

import tg
from tests.test_stack import TestConfig, app_from_config
Expand Down Expand Up @@ -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')
Expand Down
10 changes: 9 additions & 1 deletion tg/configuration/app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down

0 comments on commit c2e2cd3

Please sign in to comment.