fields: make "live" values available as form_var_xxx in page conditions (#7489)

This commit is contained in:
Frédéric Péters 2015-06-08 13:24:12 +02:00
parent ac03787bc0
commit adbb8a9d43
3 changed files with 42 additions and 3 deletions

View File

@ -64,7 +64,7 @@ trouvent pas à <em>flotter</em> en-dehors de la structure de pages.
<code>mode_de_transport</code>, 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 :
<code>var_mode_de_transport != "Piéton"</code> (où le <code>!=</code>
<code>form_var_mode_de_transport != "Piéton"</code> (où le <code>!=</code>
correspond à la syntaxe Python signifiant « différent de »).
</p>

View File

@ -258,6 +258,36 @@ def test_form_multi_page_condition_select(pub):
assert '3rd page' in resp.body
assert '<li class="current"><span>3rd page</span>' 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 '<li class="current"><span>2nd page</span>' 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 '<li class="current"><span>3rd page</span>' in resp.body
def test_form_submit_with_user(pub):
create_user(pub)
formdef = create_formdef()

View File

@ -1135,8 +1135,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):