Projet

Général

Profil

0001-templatetags-add-support-for-lazy-vars-to-string-fil.patch

Frédéric Péters, 17 mai 2019 11:56

Télécharger (2,28 ko)

Voir les différences:

Subject: [PATCH] templatetags: add support for lazy vars to string filters
 (#33196)

 tests/test_formdata.py            | 16 ++++++++++++++++
 wcs/qommon/templatetags/qommon.py |  6 ++++++
 2 files changed, 22 insertions(+)
tests/test_formdata.py
1616 1616
    local_user.store()
1617 1617
    condition = Condition({'type': 'django', 'value': 'form_user_var_test'})
1618 1618
    assert condition.evaluate() is False
1619

  
1620
def test_string_filters(pub, variable_test_data):
1621
    tmpl = Template('{% with form_var_foo_foo|split:"a" as x %}{{x.0}}{% endwith %}', raises=True)
1622
    for mode in (None, 'lazy'):
1623
        context = pub.substitutions.get_context_variables(mode=mode)
1624
        assert tmpl.render(context) == 'b'
1625

  
1626
    tmpl = Template('{% if form_var_foo_foo|startswith:"b" %}test{% endif %}', raises=True)
1627
    for mode in (None, 'lazy'):
1628
        context = pub.substitutions.get_context_variables(mode=mode)
1629
        assert tmpl.render(context) == 'test'
1630

  
1631
    tmpl = Template('{% if form_var_foo_foo|startswith:form_var_term1 %}test{% endif %}', raises=True)
1632
    for mode in (None, 'lazy'):
1633
        context = pub.substitutions.get_context_variables(mode=mode)
1634
        assert tmpl.render(context) == ''
wcs/qommon/templatetags/qommon.py
41 41

  
42 42
@register.filter
43 43
def startswith(string, substring):
44
    if hasattr(string, 'get_value'):
45
        string = string.get_value()  # unlazy
46
    if hasattr(substring, 'get_value'):
47
        substring = substring.get_value()  # unlazy
44 48
    return string and string.startswith(substring)
45 49

  
46 50
@register.filter
47 51
def split(string, separator=' '):
52
    if hasattr(string, 'get_value'):
53
        string = string.get_value()  # unlazy
48 54
    return (string or '').split(separator)
49 55

  
50 56
@register.filter
51
-