Projet

Général

Profil

0001-fields-feed-live-formdata-as-variables-when-evaluati.patch

Frédéric Péters, 17 septembre 2015 11:12

Télécharger (4,02 ko)

Voir les différences:

Subject: [PATCH] fields: feed "live" formdata as variables when evaluating
 conditions (#8272)

 tests/test_form_pages.py | 32 ++++++++++++++++++++++++++++++++
 wcs/fields.py            | 25 +++++++++++++++----------
 2 files changed, 47 insertions(+), 10 deletions(-)
tests/test_form_pages.py
344 344
    resp = resp.forms[0].submit('submit') # should go to second page
345 345
    assert 'f3' in resp.forms[0].fields
346 346

  
347
def test_form_multi_page_condition_data_source_with_form_variable(pub):
348
    # this tries to recreate #8272 which is about a json datasource being
349
    # used in a page condition and taking a value from the given page to
350
    # filter its content.  It is emulated here with a Python datasource
351
    # being empty if a field was not set.
352
    formdef = create_formdef()
353
    formdef.fields = [fields.PageField(id='0', label='1st page', type='page'),
354
            fields.StringField(id='1', label='string', varname='xxx',
355
                required=False),
356
            fields.PageField(id='2', label='2nd page', type='page',
357
                condition='len(data_source.foobar) > 0'),
358
            fields.StringField(id='3', label='string 2')]
359
    formdef.store()
360

  
361
    # add the named data source, related to a field on the first page
362
    NamedDataSource.wipe()
363
    data_source = NamedDataSource(name='foobar')
364
    data_source.data_source = {'type': 'formula', 'value': 'form_var_xxx and [form_var_xxx] or []'}
365
    data_source.store()
366

  
367
    resp = get_app(pub).get('/test/')
368
    formdef.data_class().wipe()
369
    resp = resp.forms[0].submit('submit') # should go straight to validation
370
    assert 'Check values then click submit.' in resp.body
371
    assert resp.forms[0]['previous']
372
    resp = resp.forms[0].submit('previous')
373

  
374
    resp = get_app(pub).get('/test/')
375
    resp.forms[0]['f1'] = 'HELLO'
376
    resp = resp.forms[0].submit('submit') # should go to second page
377
    assert 'f3' in resp.forms[0].fields
378

  
347 379
def test_form_submit_with_user(pub):
348 380
    create_user(pub)
349 381
    formdef = create_formdef()
wcs/fields.py
1177 1177
        if not self.condition:
1178 1178
            return True
1179 1179

  
1180
        # create variables with values currently being evaluated, not yet
1181
        # available in the formdata, keep them as var_xxx for compatibility,
1182
        # and add them as form_var_xxx.
1180 1183
        from formdata import get_dict_with_varnames
1184
        live_data = get_dict_with_varnames(formdef.fields, dict)
1185
        for k, v in live_data.items():
1186
            live_data['form_' + k] = v
1187

  
1188
        # and feed those new variables in the global substitution system, they
1189
        # will shadow formdata context variables with their new "live" value,
1190
        # this may be useful when evaluating data sources.
1191
        class ConditionVars(object):
1192
            def get_substitution_variables(self):
1193
                return live_data
1194

  
1195
        get_publisher().substitutions.feed(ConditionVars())
1181 1196
        data = get_publisher().substitutions.get_context_variables()
1182 1197

  
1183
        # create variables with values currently being evaluated, not yet
1184
        # available in the formdata
1185
        dict_with_varnames = get_dict_with_varnames(formdef.fields, dict)
1186
        # add them as var_xxx for compatibility
1187
        data.update(dict_with_varnames)
1188
        # and add them as form_var_xxx, overriding context variables with their
1189
        # "live" value
1190
        for k, v in dict_with_varnames.items():
1191
            data['form_' + k] = v
1192

  
1193 1198
        try:
1194 1199
            if eval(self.condition, get_publisher().get_global_eval_dict(), data):
1195 1200
                return True
1196
-