utils: flatten context passed to get_templated_url (#33393)
This commit is contained in:
parent
afe102d95d
commit
6b7d5938fc
|
@ -36,6 +36,9 @@ def get_templated_url(url, context=None):
|
||||||
return url
|
return url
|
||||||
template_vars = Context(use_l10n=False)
|
template_vars = Context(use_l10n=False)
|
||||||
if context:
|
if context:
|
||||||
|
if hasattr(context, 'flatten'):
|
||||||
|
# it's a django Context, dictionarize it:
|
||||||
|
context = context.flatten()
|
||||||
template_vars.update(context)
|
template_vars.update(context)
|
||||||
template_vars['user_email'] = ''
|
template_vars['user_email'] = ''
|
||||||
template_vars['user_nameid'] = ''
|
template_vars['user_nameid'] = ''
|
||||||
|
|
|
@ -4,7 +4,7 @@ from combo.utils import (aes_hex_decrypt, aes_hex_encrypt, get_templated_url,
|
||||||
TemplateError)
|
TemplateError)
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.test import override_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.test.client import RequestFactory
|
||||||
from django.contrib.auth.models import AnonymousUser
|
from django.contrib.auth.models import AnonymousUser
|
||||||
|
|
||||||
|
@ -133,3 +133,10 @@ def test_templated_url():
|
||||||
for template in ('{% foobar %}', '{% if "coucou" %}', '{{}}', '{{ if "x" }}', '{{ _private }}'):
|
for template in ('{% foobar %}', '{% if "coucou" %}', '{{}}', '{{ if "x" }}', '{{ _private }}'):
|
||||||
with pytest.raises(TemplateError, match='syntax error'):
|
with pytest.raises(TemplateError, match='syntax error'):
|
||||||
assert get_templated_url(template, context=ctx) == 'bar'
|
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'
|
||||||
|
|
Loading…
Reference in New Issue