misc: fix behaviour on missing variables with a known prefix (#26269)
This commit is contained in:
parent
4af9f76780
commit
6a05724c8e
|
@ -571,6 +571,7 @@ def variable_test_data(pub):
|
|||
fields.DateField(id='3', label='date', varname='datefield'),
|
||||
fields.ItemsField(id='4', label='items', items=['aa', 'ab', 'ac'], varname='itemsfield'),
|
||||
fields.FileField(id='5', label='file', varname='filefield'),
|
||||
fields.StringField(id='6', label='string2', varname='foo_foo_baz_baz'),
|
||||
]
|
||||
formdef.workflow_roles = {'_receiver': role.id}
|
||||
formdef.geolocations = {'base': 'Base'}
|
||||
|
@ -587,6 +588,7 @@ def variable_test_data(pub):
|
|||
'4': ['aa', 'ac'],
|
||||
'4_display': 'aa, ac',
|
||||
'5': PicklableUpload('test.txt', 'text/plain'),
|
||||
'6': 'other',
|
||||
}
|
||||
formdata.data['5'].receive(['hello world'])
|
||||
formdata.geolocations = {'base': {'lat': 1, 'lon': 2}}
|
||||
|
@ -645,6 +647,24 @@ def test_lazy_variables(pub, variable_test_data):
|
|||
assert context['form_var_foo_foo'] + 'ab' == 'barab'
|
||||
for item in enumerate(context['form_var_foo_foo']):
|
||||
assert item in [(0, 'b'), (1, 'a'), (2, 'r')]
|
||||
assert context['form_var_foo_foo_baz_baz'] == 'other'
|
||||
|
||||
def test_lazy_variables_missing(pub, variable_test_data):
|
||||
formdef = FormDef.select()[0]
|
||||
formdata = formdef.data_class()()
|
||||
formdata.just_created()
|
||||
formdata.data = {
|
||||
'0': 'bar',
|
||||
}
|
||||
pub.substitutions.reset()
|
||||
pub.substitutions.feed(formdef)
|
||||
pub.substitutions.feed(formdata)
|
||||
for mode in (None, 'lazy'):
|
||||
context = pub.substitutions.get_context_variables(mode=mode)
|
||||
assert context['form_var_foo_foo_baz_baz'] is None
|
||||
assert context['form_var_foo_foo'] == 'bar'
|
||||
with pytest.raises(KeyError):
|
||||
assert context['form_var_foo_foo_xxx'] == 'bar'
|
||||
|
||||
def test_lazy_conditions(pub, variable_test_data):
|
||||
condition = Condition({'type': 'django', 'value': 'form_var_foo_foo == "bar"'})
|
||||
|
@ -671,6 +691,10 @@ def test_lazy_conditions(pub, variable_test_data):
|
|||
condition = Condition({'type': 'django', 'value': 'form_user_backoffice_access'})
|
||||
assert condition.evaluate() is False
|
||||
|
||||
condition = Condition({'type': 'python', 'value':
|
||||
'vars().get("form_var_foo_foo_xxx", "") == ""'})
|
||||
assert condition.evaluate() is True
|
||||
|
||||
user = pub.user_class.select()[0]
|
||||
user.is_admin = True
|
||||
user.store()
|
||||
|
|
|
@ -150,7 +150,8 @@ class CompatibilityNamesDict(dict):
|
|||
current_dict = current_dict[part]
|
||||
else:
|
||||
current_dict = getattr(current_dict, part)
|
||||
except (AttributeError, KeyError):
|
||||
except (AttributeError, KeyError, TypeError):
|
||||
# TypeError will happen if indexing is used on a string
|
||||
if i == 1:
|
||||
raise KeyError(key)
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue