Projet

Général

Profil

0001-utils-flatten-context-passed-to-get_templated_url-33.patch

Frédéric Péters, 24 mai 2019 08:31

Télécharger (2,13 ko)

Voir les différences:

Subject: [PATCH] utils: flatten context passed to get_templated_url (#33393)

 combo/utils/urls.py | 3 +++
 tests/test_utils.py | 9 ++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)
combo/utils/urls.py
36 36
        return url
37 37
    template_vars = Context(use_l10n=False)
38 38
    if context:
39
        if hasattr(context, 'flatten'):
40
            # it's a django Context, dictionnarize it:
41
            context = context.flatten()
39 42
        template_vars.update(context)
40 43
        template_vars['user_email'] = ''
41 44
        template_vars['user_nameid'] = ''
tests/test_utils.py
4 4
                         TemplateError)
5 5
from django.conf import settings
6 6
from django.test import override_settings
7
from django.template import Context
7
from django.template import Context, RequestContext
8 8
from django.test.client import RequestFactory
9 9
from django.contrib.auth.models import AnonymousUser
10 10

  
......
133 133
    for template in ('{% foobar %}', '{% if "coucou" %}', '{{}}', '{{ if "x" }}', '{{ _private }}'):
134 134
        with pytest.raises(TemplateError, match='syntax error'):
135 135
            assert get_templated_url(template, context=ctx) == 'bar'
136

  
137
    # requestcontext
138
    with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net'}):
139
        request = RequestFactory().get('/')
140
        ctx = RequestContext(request, {'foo': 'bar'})
141
        assert get_templated_url('{{ test_url }}/{{ foo }}', context=ctx) == 'http://www.example.net/bar'
142
        assert get_templated_url('[test_url]/[foo]', context=ctx) == 'http://www.example.net/bar'
136
-