Projet

Général

Profil

0001-workflows-save-workflow-form-data-on-choice-action-b.patch

Frédéric Péters, 17 mai 2022 19:14

Télécharger (4,98 ko)

Voir les différences:

Subject: [PATCH 1/2] workflows: save workflow form data on "choice" action
 buttons (#65334)

 tests/backoffice_pages/test_all.py | 62 ++++++++++++++++++++++++++++++
 tests/form_pages/test_all.py       |  7 +++-
 wcs/wf/form.py                     |  9 +++--
 3 files changed, 73 insertions(+), 5 deletions(-)
tests/backoffice_pages/test_all.py
3236 3236
    }
3237 3237

  
3238 3238

  
3239
def test_backoffice_workflow_form_with_other_buttons(pub):
3240
    user = create_user(pub)
3241
    create_environment(pub)
3242

  
3243
    wf = Workflow.get_default_workflow()
3244
    wf.id = '2'
3245
    wf.store()
3246
    wf = Workflow.get(wf.id)
3247
    status = wf.get_status('new')
3248
    status.items = []
3249
    display_form = status.add_action('form', id='_display_form')
3250
    display_form.by = [user.roles[0]]
3251
    display_form.varname = 'blah'
3252
    display_form.formdef = WorkflowFormFieldsFormDef(item=display_form)
3253
    display_form.formdef.fields.append(
3254
        fields.StringField(id='1', label='Test', varname='str', type='string', required=True)
3255
    )
3256
    choice_ok = status.add_action('choice', id='_ok')
3257
    choice_ok.label = 'OK'
3258
    choice_ok.status = 'accepted'
3259
    choice_ok.by = [user.roles[0]]
3260

  
3261
    choice_ko = status.add_action('choice', id='_ko')
3262
    choice_ko.label = 'KO'
3263
    choice_ko.status = 'accepted'
3264
    choice_ko.ignore_form_errors = True
3265
    choice_ko.by = [user.roles[0]]
3266

  
3267
    jump = status.add_action('jumponsubmit', id='_jump')
3268
    jump.status = 'accepted'
3269

  
3270
    wf.store()
3271
    formdef = FormDef.get_by_urlname('form-title')
3272
    formdef.workflow_id = wf.id
3273
    formdef.store()
3274

  
3275
    for button_name in ('submit', 'button_ok', 'button_ko'):
3276
        formdata = formdef.data_class()()
3277
        formdata.data = {}
3278
        formdata.just_created()
3279
        formdata.jump_status('new')
3280
        formdata.store()
3281

  
3282
        app = login(get_app(pub))
3283
        resp = app.get(formdata.get_url(backoffice=True))
3284
        resp.form['fblah_1'] = 'blah'
3285
        resp = resp.form.submit(button_name)
3286

  
3287
        formdata.refresh_from_storage()
3288
        pub.substitutions.reset()
3289
        pub.substitutions.feed(formdata)
3290
        context = pub.substitutions.get_context_variables(mode='lazy')
3291
        assert formdata.status == 'wf-accepted'
3292
        if button_name == 'button_ko':
3293
            assert context['form_workflow_data_blah_var_str'] == 'blah'  # leak
3294
            with pytest.raises(KeyError):
3295
                assert context['form_workflow_form_blah_var_str'] == 'blah'
3296
        else:
3297
            assert context['form_workflow_form_blah_var_str'] == 'blah'
3298
            assert context['form_workflow_data_blah_var_str'] == 'blah'
3299

  
3300

  
3239 3301
def test_backoffice_criticality_formdata_view(pub):
3240 3302
    create_user(pub)
3241 3303
    create_environment(pub)
tests/form_pages/test_all.py
8223 8223
    pub.substitutions.reset()
8224 8224
    pub.substitutions.feed(formdata)
8225 8225
    context = pub.substitutions.get_context_variables(mode='lazy')
8226
    # check workflow form data is not saved (good)
8227
    assert 'form_workflow_form_blah_var_foo' not in context
8226
    # check workflow form data is saved if button comes after form action
8227
    if button_position == 'before':
8228
        assert 'form_workflow_form_blah_var_foo' not in context
8229
    else:
8230
        assert 'form_workflow_form_blah_var_foo' in context
8228 8231
    # but legacy behaviout it leaks into workflow_data :/
8229 8232
    assert context['form_workflow_data_blah_var_foo'] == 'a'
8230 8233

  
wcs/wf/form.py
294 294
                    add_element_widget = widget.get_widget('add_element')
295 295
                    if add_element_widget and add_element_widget.parse():
296 296
                        raise RedisplayFormException()
297
        if form.get_submit() == 'submit' and not form.has_errors():
298
            self.evaluate_live_form(form, formdata, user, submit=True)
299
            formdata.store()
297
        elif not form.has_errors():
298
            button_name = form.get_submit()
299
            button = form.get_widget(button_name)
300
            if button and not getattr(button, 'ignore_form_errors', False):
301
                self.evaluate_live_form(form, formdata, user, submit=True)
302
                formdata.store()
300 303
        get_publisher().substitutions.unfeed(lambda x: x.__class__.__name__ == 'ConditionVars')
301 304

  
302 305
    def get_parameters_view(self):
303
-