From 61af27510e470935713e1c7391f672803551af0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 8 Jun 2015 13:24:12 +0200 Subject: [PATCH] fields: make "live" values available as form_var_xxx in page conditions (#7489) --- help/fr/form-multipages.page | 2 +- tests/test_form_pages.py | 30 ++++++++++++++++++++++++++++++ wcs/fields.py | 13 +++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/help/fr/form-multipages.page b/help/fr/form-multipages.page index 37bc41f..383334c 100644 --- a/help/fr/form-multipages.page +++ b/help/fr/form-multipages.page @@ -64,7 +64,7 @@ trouvent pas à flotter en-dehors de la structure de pages. mode_de_transport, la page sur l'accès au parking ne devrait pas être affichée pour les personnes ayant précisés qu'elles venaient à pieds, le champ serait complété avec l'expression suivante : - var_mode_de_transport != "Piéton" (où le != + form_var_mode_de_transport != "Piéton" (où le != correspond à la syntaxe Python signifiant « différent de »).

diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py index d5cc248..3adc6c5 100644 --- a/tests/test_form_pages.py +++ b/tests/test_form_pages.py @@ -258,6 +258,36 @@ def test_form_multi_page_condition_select(pub): assert '3rd page' in resp.body assert '
  • 3rd page' in resp.body +def test_form_multi_page_condition_select_new_varname(pub): + formdef = create_formdef() + formdef.fields = [fields.PageField(id='0', label='1st page', type='page'), + fields.ItemField(id='1', label='select', type='item', + required=True, + varname='foo', items=['Foo', 'Bar']), + fields.PageField(id='2', label='2nd page', type='page', + condition='form_var_foo == "Foo"'), + fields.PageField(id='3', label='3rd page', type='page', + condition='form_var_foo == "Bar"'), + fields.StringField(id='3', label='string 2')] + formdef.store() + formdef.data_class().wipe() + resp = get_app(pub).get('/test/') + assert not '2nd page' in resp.body + assert not '3rd page' in resp.body + resp.forms[0]['f1'] = 'Foo' + resp = resp.forms[0].submit('submit') + assert '2nd page' in resp.body + assert not '3rd page' in resp.body + assert '
  • 2nd page' in resp.body + + resp = get_app(pub).get('/test/') + resp.forms[0]['f1'] = 'Bar' + resp = resp.forms[0].submit('submit') + assert not '2nd page' in resp.body + assert '3rd page' in resp.body + assert '
  • 3rd page' in resp.body + + def test_form_submit_with_user(pub): create_user(pub) formdef = create_formdef() diff --git a/wcs/fields.py b/wcs/fields.py index a6c13d1..1ef6360 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -1172,8 +1172,17 @@ class PageField(Field): return True from formdata import get_dict_with_varnames - data = get_dict_with_varnames(formdef.fields, dict) - data.update(get_publisher().substitutions.get_context_variables()) + data = get_publisher().substitutions.get_context_variables() + + # create variables with values currently being evaluated, not yet + # available in the formdata + dict_with_varnames = get_dict_with_varnames(formdef.fields, dict) + # add them as var_xxx for compatibility + data.update(dict_with_varnames) + # and add them as form_var_xxx, overriding context variables with their + # "live" value + for k, v in dict_with_varnames.items(): + data['form_' + k] = v try: if eval(self.condition, get_publisher().get_global_eval_dict(), data): -- 2.1.4