From 664063ce5a7b90e6c74e48cfbe1510afe6448e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 10 Sep 2018 11:45:39 +0200 Subject: [PATCH] misc: fix behaviour on missing variables with a known prefix (#26269) --- tests/test_formdata.py | 24 ++++++++++++++++++++++++ wcs/qommon/substitution.py | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_formdata.py b/tests/test_formdata.py index 95bc3f789..f6bf8ff27 100644 --- a/tests/test_formdata.py +++ b/tests/test_formdata.py @@ -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() diff --git a/wcs/qommon/substitution.py b/wcs/qommon/substitution.py index c7baf03c1..e045e7f3b 100644 --- a/wcs/qommon/substitution.py +++ b/wcs/qommon/substitution.py @@ -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: -- 2.19.0.rc2