Projet

Général

Profil

0001-unfeed-clean-cached-values-of-substitution-variables.patch

Nicolas Roche, 24 avril 2019 13:03

Télécharger (4,6 ko)

Voir les différences:

Subject: [PATCH] unfeed: clean cached values of substitution variables using a
 condition before entering into workflow (#32558)

 tests/test_form_pages.py | 75 ++++++++++++++++++++++++++++++++++++++++
 wcs/forms/root.py        |  2 ++
 2 files changed, 77 insertions(+)
tests/test_form_pages.py
6158 6158
    params = resp.form.submit_fields()
6159 6159
    params = [(key, value if key != 'page_id' else 'eiuiu') for key, value in params]
6160 6160
    app.post('/foo/live', params=params)
6161

  
6162
def test_unfeed_structured_list_when_condition_changes(pub):
6163
    """
6164
    simulate selection of a structured list via condition on from,
6165
    followed by a firstof evaluation on workflow in order to get
6166
    the a structured value from the seleted list.
6167
    """
6168
    # workflow
6169
    Workflow.wipe()
6170
    workflow = Workflow(name='test')
6171
    workflow.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(workflow)
6172
    workflow.backoffice_fields_formdef.fields = [
6173
        fields.StringField(
6174
            id='bo1', label='first text', type='string', varname='firstof_text'),
6175
        fields.StringField(
6176
            id='bo2', label='first more', type='string', varname='firstof_more')
6177
    ]
6178

  
6179
    # status 1 having backoffice fields
6180
    st1 = workflow.add_status('Status1', 'st1')
6181
    setbo = SetBackofficeFieldsWorkflowStatusItem()
6182
    setbo.parent = st1
6183
    setbo.fields = [
6184
        {'field_id': 'bo1',
6185
         'value': '{% firstof form_var_listA form_var_listB %}'},
6186
        {'field_id': 'bo2',
6187
         'value': '{% firstof form_var_listA_more form_var_listB_more %}'}
6188
         ]
6189
    st1.items.append(setbo)
6190
    workflow.store()
6191

  
6192
    # form providing mainly 2 lists having a condition
6193
    items_A = [dict(id=i, text='A%s' % i, more='moreA%s' % i) for i in range(3)]
6194
    items_B = [dict(id=i, text='B%s' % i, more='moreB%s' % i) for i in range(3)]
6195
    formdef = create_formdef()
6196
    formdef.fields = [
6197
        fields.ItemField(id='1', varname='choice', items=['A', 'B'],
6198
                         type='item', label='list to choice'),
6199
        fields.ItemField(id='2', varname='listA', type='item', label='list A',
6200
                         data_source={'type': 'formula', 'value': str(items_A)},
6201
                         condition={'type': 'python',
6202
                                    'value': 'form_var_choice_raw == "A"'}),
6203
        fields.ItemField(id='3', varname='listB', type='item', label='list B',
6204
                         data_source={'type': 'formula', 'value': str(items_B)},
6205
                         condition={'type': 'python',
6206
                                    'value': 'form_var_choice_raw == "B"'})
6207
        ]
6208
    formdef.confirmation = False
6209
    formdef.workflow_id = workflow.id
6210
    formdef.store()
6211
    formdef.data_class().wipe()
6212

  
6213
    # get form
6214
    create_user_and_admin(pub)
6215
    resp = get_app(pub).get('/test/')
6216

  
6217
    # user selection on form
6218
    resp.form['f1'].value = 'B' # change condition: switch on list B
6219
    resp.form['f2'].value = '1' # make 'A1' choice on list A
6220
    resp.form['f3'].value = '2' # make 'B2' choice on list B
6221

  
6222
    # submit form
6223
    resp = resp.form.submit('submit').follow()
6224
    assert 'The form has been recorded' in resp.body
6225

  
6226
    # assert we get a new formdata
6227
    assert formdef.data_class().count() == 1
6228
    formdata = formdef.data_class().select()[0]
6229
    assert formdata.data['1'] == 'B'         # choice is list B
6230
    assert formdata.data.get('2') is None    # list A is hidden
6231
    assert formdata.data['3'] == '2'         # B2 on list B
6232

  
6233
    # assert firstof value are not computed using previous cached values
6234
    assert formdata.data['bo1'] == 'B2'      # B2 on list B
6235
    assert formdata.data['bo2'] == 'moreB2'  # B2 on list B
wcs/forms/root.py
1148 1148
        get_logger().info('form %s - done (id: %s)' % (self.formdef.name, filled.id))
1149 1149
        url = None
1150 1150
        if existing_formdata is None:
1151
            get_publisher().substitutions.unfeed(lambda x: x.__class__.__name__ == 'ConditionVars')
1152
            get_publisher().substitutions.unfeed(lambda x: isinstance(x, FormData))
1151 1153
            url = filled.perform_workflow()
1152 1154

  
1153 1155
        if not filled.user_id:
1154
-