diff --git a/combo/utils/urls.py b/combo/utils/urls.py index 57e67be7..10703e9f 100644 --- a/combo/utils/urls.py +++ b/combo/utils/urls.py @@ -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'] = '' diff --git a/tests/test_utils.py b/tests/test_utils.py index 8ccbde0c..422b9a88 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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'