Projet

Général

Profil

0002-misc-update-Context-usage-in-template-strings-20936.patch

Frédéric Péters, 01 janvier 2018 13:49

Télécharger (11,2 ko)

Voir les différences:

Subject: [PATCH 2/2] misc: update Context usage in template strings (#20936)

 tests/test_variadic_url.py | 49 +++++++++++++++++++++++++++-------------------
 wcs/qommon/emails.py       |  3 +--
 wcs/qommon/misc.py         |  4 ++--
 wcs/qommon/template.py     |  5 ++---
 4 files changed, 34 insertions(+), 27 deletions(-)
tests/test_variadic_url.py
3 3
from wcs.qommon.misc import get_variadic_url
4 4
from wcs.qommon.ezt import EZTException
5 5

  
6
from utilities import create_temporary_pub, clean_temporary_pub
6 7

  
7
def test_url_unchanged():
8
@pytest.fixture
9
def pub(request):
10
    return create_temporary_pub()
11

  
12
def teardown_module(module):
13
    clean_temporary_pub()
14

  
15

  
16
def test_url_unchanged(pub):
8 17
    assert get_variadic_url('http://www.example.net/foobar', {}) == 'http://www.example.net/foobar'
9 18

  
10 19

  
11
def test_url_scheme():
20
def test_url_scheme(pub):
12 21
    assert get_variadic_url('http[https]://www.example.net/foobar',
13 22
                      {'https': 's'}) == 'https://www.example.net/foobar'
14 23
    assert get_variadic_url('http[https]://www.example.net/foobar',
......
32 41
                      {'https': ''}) == 'http://www.example.net/foo/bar'
33 42

  
34 43

  
35
def test_url_netloc():
44
def test_url_netloc(pub):
36 45
    assert get_variadic_url('http://[hostname]/foobar',
37 46
                      {'hostname': 'www.example.net'}) == 'http://www.example.net/foobar'
38 47
    assert get_variadic_url('http://[hostname]/foobar',
......
44 53
                      {'hostname': 'www.example.com'}) == 'http://www.example.com/foobar'
45 54

  
46 55

  
47
def test_url_netloc_port():
56
def test_url_netloc_port(pub):
48 57
    assert get_variadic_url('http://www.example.net:[port]/foobar',
49 58
                      {'port': '80'}) == 'http://www.example.net:80/foobar'
50 59
    assert get_variadic_url('http://www.example.net:{{ port }}/foobar',
51 60
                      {'port': '80'}) == 'http://www.example.net:80/foobar'
52 61

  
53 62

  
54
def test_url_path():
63
def test_url_path(pub):
55 64
    assert get_variadic_url('http://www.example.net/[path]',
56 65
                      {'path': 'foobar'}) == 'http://www.example.net/foobar'
57 66
    assert get_variadic_url('http://www.example.net/[path]',
......
65 74
                      {'path': 'foo bar'}) == 'http://www.example.net/foo%20bar'
66 75

  
67 76

  
68
def test_url_query_variable():
77
def test_url_query_variable(pub):
69 78
    assert get_variadic_url('http://www.example.net/foobar?hello=[world]',
70 79
                      {'world': 'world'}) == 'http://www.example.net/foobar?hello=world'
71 80
    assert get_variadic_url('http://www.example.net/foobar?hello=[world]',
......
91 100
                      {'world': 'a=b'}) == 'http://www.example.net/foobar?hello=a%3Db'
92 101

  
93 102

  
94
def test_url_query_key():
103
def test_url_query_key(pub):
95 104
    assert get_variadic_url('http://www.example.net/foobar?[hello]=world',
96 105
                      {'hello': 'hello'}) == 'http://www.example.net/foobar?hello=world'
97 106
    assert get_variadic_url('http://www.example.net/foobar?[hello]=world',
......
117 126
                      {'hello': 'a=b'}) == 'http://www.example.net/foobar?a%3Db=world'
118 127

  
119 128

  
120
def test_url_query_whole():
129
def test_url_query_whole(pub):
121 130
    assert get_variadic_url('http://www.example.net/foobar?[hello]',
122 131
                      {'hello': 'hello=world'}) == 'http://www.example.net/foobar?hello=world'
123 132

  
......
127 136
                      {'hello': 'hello=world'}) == 'http://www.example.net/foobar?hello%3Dworld'
128 137

  
129 138

  
130
def test_url_netloc_port_and_path():
139
def test_url_netloc_port_and_path(pub):
131 140
    assert get_variadic_url('http://www.example.net:[port]/foobar/[path]',
132 141
                      {'port': '80', 'path': 'baz'}) == 'http://www.example.net:80/foobar/baz'
133 142
    assert get_variadic_url('http://www.example.net:[port]/foobar/[path]',
......
141 150
                      {'port': '80', 'path': 'b z'}) == 'http://www.example.net:80/foobar/b%20z'
142 151

  
143 152

  
144
def test_url_whole():
153
def test_url_whole(pub):
145 154
    assert get_variadic_url('[url]',
146 155
            {'url': 'http://www.example.net/foobar'}) == 'http://www.example.net/foobar'
147 156

  
......
149 158
            {'url': 'http://www.example.net/foobar'}) == 'http://www.example.net/foobar'
150 159

  
151 160

  
152
def test_url_server():
161
def test_url_server(pub):
153 162
    for url in ('http://www.example.net', 'http://www.example.net/'):
154 163
        assert get_variadic_url('[url]/foobar',
155 164
                {'url': url}) == 'http://www.example.net/foobar'
......
180 189
            {'url': 'http://www.example.net/'}) == 'http://www.example.net/foo/bar'
181 190

  
182 191

  
183
def test_url_server_qs():
192
def test_url_server_qs(pub):
184 193
    assert get_variadic_url('[url]?foo=bar',
185 194
            {'url': 'http://www.example.net'}) == 'http://www.example.net/?foo=bar'
186 195
    assert get_variadic_url('[url]?foo=bar',
......
201 210
            {'url': 'http://www.example.net/'}) == 'http://www.example.net//?foo=bar'
202 211

  
203 212

  
204
def test_url_server_more():
213
def test_url_server_more(pub):
205 214
    assert get_variadic_url('[url]/foobar/json?toto',
206 215
            {'url': 'http://www.example.net'}) == 'http://www.example.net/foobar/json?toto'
207 216
    assert get_variadic_url('[url]/foobar/json?toto',
......
218 227
            {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/json?toto'
219 228

  
220 229

  
221
def test_url_server_even_more():
230
def test_url_server_even_more(pub):
222 231
    assert get_variadic_url('[url]/foobar/json?foo=bar',
223 232
            {'url': 'http://www.example.net'}) == 'http://www.example.net/foobar/json?foo=bar'
224 233
    assert get_variadic_url('[url]/foobar/json?foo=bar',
......
235 244
            {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/json?foo=bar'
236 245

  
237 246

  
238
def test_url_server_even_more_more():
247
def test_url_server_even_more_more(pub):
239 248
    assert get_variadic_url('[url]/foobar/baz/json?foo=bar',
240 249
            {'url': 'http://www.example.net'}) == 'http://www.example.net/foobar/baz/json?foo=bar'
241 250
    assert get_variadic_url('[url]/foobar/baz/json?foo=bar',
......
252 261
            {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/baz/json?foo=bar'
253 262

  
254 263

  
255
def test_url_whole_with_qs():
264
def test_url_whole_with_qs(pub):
256 265
    assert get_variadic_url('[url]',
257 266
            {'url': 'http://www.example.net/?foo=bar'}) == 'http://www.example.net/?foo=bar'
258 267

  
......
260 269
            {'url': 'http://www.example.net/?foo=bar'}) == 'http://www.example.net/?foo=bar'
261 270

  
262 271

  
263
def test_url_whole_with_qs_2():
272
def test_url_whole_with_qs_2(pub):
264 273
    for url in ('[url]?bar=foo', '[url]&bar=foo', '[url]/?bar=foo'):
265 274
        assert get_variadic_url(url, {'url': 'http://www.example.net/?foo=bar'}) in \
266 275
                ('http://www.example.net/?bar=foo&foo=bar', 'http://www.example.net/?foo=bar&bar=foo')
......
274 283
            {'url': 'http://www.example.net/'}) == 'http://www.example.net/?bar=foo'
275 284

  
276 285

  
277
def test_path_missing_var():
286
def test_path_missing_var(pub):
278 287
    assert get_variadic_url('http://www.example.net/foobar/[path]',
279 288
            {}) == 'http://www.example.net/foobar/[path]'
280 289

  
......
283 292
            {}) == 'http://www.example.net/foobar/'
284 293

  
285 294

  
286
def test_url_base_and_missing_var():
295
def test_url_base_and_missing_var(pub):
287 296
    assert get_variadic_url('[url]/foobar/[path]',
288 297
            {'url': 'http://www.example.net'}) == 'http://www.example.net/foobar/[path]'
289 298
    assert get_variadic_url('[url]foobar/[path]',
......
294 303
    assert get_variadic_url('{{ url }}foobar/{{ path }}',
295 304
            {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/'
296 305

  
297
def test_url_bad_syntax():
306
def test_url_bad_syntax(pub):
298 307
    with pytest.raises(EZTException):
299 308
        get_variadic_url('[if-any form_avr]https://example.net/[foo]/', {'foo': 'bar'})
300 309

  
wcs/qommon/emails.py
39 39
except ImportError:
40 40
    docutils = None
41 41

  
42
from django.template import Context
43 42
from django.template.loader import render_to_string
44 43
from django.utils.safestring import mark_safe
45 44

  
......
168 167
        except IndexError:
169 168
            pass
170 169

  
171
    context = Context(get_publisher().get_substitution_variables())
170
    context = get_publisher().get_substitution_variables()
172 171
    context['email_signature'] = footer
173 172

  
174 173
    if text_body:
wcs/qommon/misc.py
37 37

  
38 38
from django.conf import settings
39 39
from django.utils import datetime_safe
40
from django.template import Template, Context, TemplateSyntaxError, VariableDoesNotExist
40
from django.template import engines, TemplateSyntaxError, VariableDoesNotExist
41 41

  
42 42
from quixote import get_publisher, get_response, get_request
43 43
from quixote.html import htmltext
......
338 338
    # django template
339 339
    if '{{' in url or '{%' in url:
340 340
        try:
341
            return Template(url).render(Context(variables))
341
            return Template(url).render(variables)
342 342
        except (TemplateSyntaxError, VariableDoesNotExist):
343 343
            return url
344 344

  
wcs/qommon/template.py
20 20
import xml.etree.ElementTree as ET
21 21

  
22 22
import django.template
23
from django.template import (Context as DjangoContext, Template as DjangoTemplate,
23
from django.template import (engines,
24 24
                             TemplateSyntaxError as DjangoTemplateSyntaxError,
25 25
                             VariableDoesNotExist as DjangoVariableDoesNotExist)
26 26
from django.template.loader import render_to_string
......
460 460
        if ('{{' in value or '{%' in value) and not ezt_only:  # Django template
461 461
            self.render = self.django_render
462 462
            try:
463
                self.template = DjangoTemplate(value)
463
                self.template = engines['django'].from_string(value)
464 464
            except DjangoTemplateSyntaxError as e:
465 465
                if raises:
466 466
                    from qommon import _
......
484 484
        return str(self.value)
485 485

  
486 486
    def django_render(self, context={}):
487
        context = DjangoContext(context)
488 487
        try:
489 488
            rendered = self.template.render(context)
490 489
        except (DjangoTemplateSyntaxError, DjangoVariableDoesNotExist) as e:
491
-