templatetags: adapt |length and |length_is for lazy variables (#49383)

This commit is contained in:
Frédéric Péters 2020-12-11 15:16:09 +01:00
parent 93ef308b1b
commit 45580748ae
2 changed files with 26 additions and 0 deletions

View File

@ -1250,6 +1250,25 @@ def test_lazy_variables_missing(pub, variable_test_data):
assert context['form_var_foo_foo_xxx'] == 'bar'
def test_lazy_variables_length(pub, variable_test_data):
formdef = FormDef.select()[0]
formdata = formdef.data_class()()
formdata.just_created()
formdata.data = {
'0': 'bar',
'6': '3',
}
pub.substitutions.reset()
pub.substitutions.feed(formdef)
pub.substitutions.feed(formdata)
for mode in (None, 'lazy'):
pub.substitutions.reset()
pub.substitutions.feed(formdata)
assert WorkflowStatusItem.compute('{{ form_var_foo_foo|length }}') == '3'
assert WorkflowStatusItem.compute('{% if form_var_foo_foo|length_is:3 %}ok{% endif %}') == 'ok'
assert WorkflowStatusItem.compute('{% if form_var_foo_foo|length_is:form_var_foo_foo_baz_baz %}ok{% endif %}') == 'ok'
def test_lazy_map_variable(pub, variable_test_data):
formdef = FormDef.select()[0]
formdata = formdef.data_class().select()[0]

View File

@ -665,6 +665,13 @@ class LazyFieldVar(object):
def __mul__(self, other):
return self.get_value().__mul__(other)
def __len__(self):
return len(self.get_value())
def __int__(self):
return int(self.get_value())
def startswith(self, other):
return self.get_value().startswith(other)