Skip to content
Permalink
Browse files

Don't crash if there is a key error in a template. Bug 556814

  • Loading branch information...
clouserw committed Apr 5, 2010
1 parent 69349e2 commit c73d2c55182301e6f3ce507fc9a17ea843fed24f
Binary file not shown.
@@ -0,0 +1,4 @@
#, python-format
msgid "Broken %(string)s"
msgstr "Broken %(fuuu)s"

@@ -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 = {}
@@ -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)
@@ -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))
@@ -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():
@@ -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')
@@ -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.
You can’t perform that action at this time.