Skip to content

Commit

Permalink
Don't crash if there is a key error in a template. Bug 556814
Browse files Browse the repository at this point in the history
  • Loading branch information
clouserw committed Apr 5, 2010
1 parent 69349e2 commit c73d2c5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/jingo-project/locale/xx/LC_MESSAGES/messages.po
@@ -0,0 +1,4 @@
#, python-format
msgid "Broken %(string)s"
msgstr "Broken %(fuuu)s"

6 changes: 6 additions & 0 deletions examples/jingo-project/settings.py
@@ -1 +1,7 @@
import os

# Make filepaths relative to settings.
ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT, *a)

JINJA_CONFIG = {}
3 changes: 2 additions & 1 deletion fabfile.py
Expand Up @@ -26,11 +26,12 @@ def doc(kind='html'):
with cd('docs'):
local('make clean %s' % kind)

def shell():
local('django-admin.py shell')

def test():
local('nosetests')


def updoc():
doc('dirhtml')
rsync_project('p/%s' % NAME, 'docs/_build/dirhtml/', delete=True)
11 changes: 10 additions & 1 deletion jingo/__init__.py
Expand Up @@ -4,8 +4,10 @@
from django import http
from django.conf import settings
from django.template.context import get_standard_processors
from django.utils.translation import trans_real

import jinja2
import tower

VERSION = (0, 3)
__version__ = '.'.join(map(str, VERSION))
Expand Down Expand Up @@ -74,7 +76,14 @@ def render_to_string(request, template, context=None):
# If it's not a Template, it must be a path to be loaded.
if not isinstance(template, jinja2.environment.Template):
template = env.get_template(template)
return template.render(**context)
try:
ret = template.render(**context)
except KeyError:
_lang = trans_real.get_language()
tower.deactivate_all()
ret = template.render(**context)
tower.activate(_lang)
return ret


def load_helpers():
Expand Down
19 changes: 18 additions & 1 deletion jingo/tests/test_views.py
@@ -1,10 +1,27 @@
from mock import patch, sentinel
from mock import Mock, patch, sentinel

from nose.tools import eq_
from test_helpers import render
import jingo.views
from jingo import get_env
import jinja2
import tower


@patch('jingo.render')
def test_direct_to_template(mock_render):
request = sentinel.request
jingo.views.direct_to_template(request, 'base.html', x=1)
mock_render.assert_called_with(request, 'base.html', {'x': 1})


def test_template_substitution_crash():
tower.activate('xx')

env = get_env()

# The localized string has the wrong variable name in it
s = '{% trans string="heart" %}Broken {{ string }}{% endtrans %}'
template = env.from_string(s)
rendered = jingo.render_to_string(Mock(), template, {})
eq_(rendered, 'Broken heart')
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -4,4 +4,5 @@ jinja2
nose
mock
-e svn+http://code.djangoproject.com/svn/django/trunk@12335#egg=Django
-e git://github.com/clouserw/tower.git#egg=tower
fabric

0 comments on commit c73d2c5

Please sign in to comment.