Project

General

Profile

0001-misc-don-t-feed-var_-prefixed-variables-into-the-sub.patch

Frédéric Péters, 04 February 2016 06:17 PM

Download (4.18 KB)

View differences:

Subject: [PATCH] misc: don't feed var_-prefixed variables into the
 substitution system (#9786)

They are only created for backward-compatibility when evaluating conditions and
interfere with variadic URLs that may be used for dynamic jsonp-based fields.
 tests/test_form_pages.py | 12 +++++++++---
 wcs/fields.py            | 23 +++++++++++++----------
 2 files changed, 22 insertions(+), 13 deletions(-)
tests/test_form_pages.py
1997 1997
def test_form_autocomplete_variadic_url(pub):
1998 1998
    formdef = create_formdef()
1999 1999
    formdef.fields = [fields.PageField(id='0', label='1st page', type='page'),
2000
            fields.PageField(id='3', label='2nd page', type='page', condition="True"),
2000 2001
            fields.ItemField(id='1', label='string', type='item',
2001 2002
                varname='foo', items=['Foo', 'Bar']),
2002
            fields.StringField(id='2', label='string2',
2003
            fields.StringField(id='2', label='string2', required=True,
2003 2004
                data_source={'type': 'jsonp', 'value': '[var_foo]'}),
2004
            fields.PageField(id='3', label='2nd page', type='page',
2005
                condition='foo == "Foo"'),
2005
            fields.PageField(id='4', label='3rd page', type='page', condition="True")
2006 2006
            ]
2007 2007
    formdef.store()
2008 2008

  
2009 2009
    formdef.data_class().wipe()
2010 2010
    resp = get_app(pub).get('/test/')
2011
    resp = resp.form.submit('submit') # next
2011 2012
    # test javascript will be used to compute the full URL
2012 2013
    assert 'options.wcs_base_url' in resp.body
2013 2014

  
2015
    # test going forward (will error out), check it's still a variadic URL (#9786)
2016
    resp.form['f1'] = 'Foo'
2017
    resp = resp.form.submit('submit')
2018
    assert 'options.wcs_base_url' in resp.body
2019

  
2014 2020
def test_form_page_formula_prefill_user_name(pub):
2015 2021
    user = create_user(pub)
2016 2022
    formdef = create_formdef()
wcs/fields.py
1341 1341
    def add_to_view_form(self, *args):
1342 1342
        pass
1343 1343

  
1344
    def evaluate_condition(self, dict, formdef, condition):
1344
    def evaluate_condition(self, dict_vars, formdef, condition):
1345 1345
        if not condition:
1346 1346
            return True
1347 1347

  
1348 1348
        # create variables with values currently being evaluated, not yet
1349
        # available in the formdata, keep them as var_xxx for compatibility,
1350
        # and add them as form_var_xxx.
1349
        # available in the formdata.
1351 1350
        from formdata import get_dict_with_varnames
1352
        live_data = get_dict_with_varnames(formdef.fields, dict)
1353
        for k, v in live_data.items():
1354
            live_data['form_' + k] = v
1351
        live_data = get_dict_with_varnames(formdef.fields, dict_vars)
1352
        form_live_data = dict(('form_' + x, y) for x, y in live_data.items())
1355 1353

  
1356
        # and feed those new variables in the global substitution system, they
1357
        # will shadow formdata context variables with their new "live" value,
1358
        # this may be useful when evaluating data sources.
1354
        # 1) feed the form_var_* variables in the global substitution system,
1355
        # they # will shadow formdata context variables with their new "live"
1356
        # value, this may be useful when evaluating data sources.
1359 1357
        class ConditionVars(object):
1360 1358
            def get_substitution_variables(self):
1361
                return live_data
1359
                return form_live_data
1362 1360

  
1363 1361
        get_publisher().substitutions.feed(ConditionVars())
1364 1362
        data = get_publisher().substitutions.get_context_variables()
1363
        # 2) add live data as var_ variables for local evaluation only, for
1364
        # backward compatibility. They are not added globally as they would
1365
        # interfere with the var_ prefixed variables used in dynamic jsonp
1366
        # fields. (#9786)
1367
        data.update(live_data)
1365 1368

  
1366 1369
        try:
1367 1370
            if eval(condition, get_publisher().get_global_eval_dict(), data):
1368
-