Projet

Général

Profil

0001-templatetags-add-endswith-template-filter-43558.patch

Nicolas Roche, 03 juin 2020 10:42

Télécharger (2,52 ko)

Voir les différences:

Subject: [PATCH] templatetags: add 'endswith' template filter (#43558)

 combo/public/templatetags/combo.py | 4 ++++
 tests/test_public_templatetags.py  | 8 ++++++++
 2 files changed, 12 insertions(+)
combo/public/templatetags/combo.py
338 338
@register.simple_tag
339 339
def get_page(page_slug):
340 340
    return Page.objects.get(slug=page_slug)
341 341

  
342 342
@register.filter
343 343
def startswith(string, substring):
344 344
    return string and force_text(string).startswith(force_text(substring))
345 345

  
346
@register.filter
347
def endswith(string, substring):
348
    return string and force_text(string).endswith(force_text(substring))
349

  
346 350
def parse_float(value):
347 351
    if isinstance(value, six.string_types):
348 352
        # replace , by . for French users comfort
349 353
        value = value.replace(',', '.')
350 354
    try:
351 355
        return float(value)
352 356
    except (ValueError, TypeError):
353 357
        return ''
tests/test_public_templatetags.py
236 236
    t = Template('{% if foo|startswith:"bar" %}ok{% endif %}')
237 237
    context = Context({'foo': None})
238 238
    assert t.render(context) == ''
239 239
    context = Context({'foo': 'xx'})
240 240
    assert t.render(context) == ''
241 241
    context = Context({'foo': 'bar'})
242 242
    assert t.render(context) == 'ok'
243 243

  
244

  
245
def test_endswith_templatetag():
246
    tmpl = Template('{% if foo|endswith:"bar" %}ok{% endif %}')
247
    assert tmpl.render(Context({'foo': None})) == ''
248
    assert tmpl.render(Context({'foo': 'bar-baz'})) == ''
249
    assert tmpl.render(Context({'foo': 'baz-bar'})) == 'ok'
250

  
251

  
244 252
def test_datetime_templatetags():
245 253
    tmpl = Template('{{ plop|datetime }}')
246 254
    assert tmpl.render(Context({'plop': '2017-12-21 10:32'})) == 'Dec. 21, 2017, 10:32 a.m.'
247 255
    assert tmpl.render(Context({'plop': '21/12/2017 10h32'})) == 'Dec. 21, 2017, 10:32 a.m.'
248 256
    assert tmpl.render(Context({'plop': '2017-12-21'})) == 'Dec. 21, 2017, midnight'
249 257
    assert tmpl.render(Context({'plop': '21/12/2017'})) == 'Dec. 21, 2017, midnight'
250 258
    assert tmpl.render(Context({'plop': '10h32'})) == ''
251 259
    assert tmpl.render(Context({'plop': 'x'})) == ''
252
-