utils: flatten context passed to get_templated_url (#33393)

This commit is contained in:
Frédéric Péters 2019-05-24 08:27:31 +02:00
parent afe102d95d
commit 6b7d5938fc
2 changed files with 11 additions and 1 deletions

View File

@ -36,6 +36,9 @@ def get_templated_url(url, context=None):
return url
template_vars = Context(use_l10n=False)
if context:
if hasattr(context, 'flatten'):
# it's a django Context, dictionarize it:
context = context.flatten()
template_vars.update(context)
template_vars['user_email'] = ''
template_vars['user_nameid'] = ''

View File

@ -4,7 +4,7 @@ from combo.utils import (aes_hex_decrypt, aes_hex_encrypt, get_templated_url,
TemplateError)
from django.conf import settings
from django.test import override_settings
from django.template import Context
from django.template import Context, RequestContext
from django.test.client import RequestFactory
from django.contrib.auth.models import AnonymousUser
@ -133,3 +133,10 @@ def test_templated_url():
for template in ('{% foobar %}', '{% if "coucou" %}', '{{}}', '{{ if "x" }}', '{{ _private }}'):
with pytest.raises(TemplateError, match='syntax error'):
assert get_templated_url(template, context=ctx) == 'bar'
# requestcontext
with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net'}):
request = RequestFactory().get('/')
ctx = RequestContext(request, {'foo': 'bar'})
assert get_templated_url('{{ test_url }}/{{ foo }}', context=ctx) == 'http://www.example.net/bar'
assert get_templated_url('[test_url]/[foo]', context=ctx) == 'http://www.example.net/bar'