Project

General

Profile

0001-workflows-reevaluate-form-according-to-possible-new-.patch

Nicolas Roche, 19 Sep 2019 05:56 PM

Download (6.02 KB)

View differences:

Subject: [PATCH] workflows: reevaluate form according to possible new content
 (#35903)

 tests/test_form_pages.py | 76 ++++++++++++++++++++++++++++++++++++++++
 wcs/forms/common.py      | 23 ++++++------
 2 files changed, 89 insertions(+), 10 deletions(-)
tests/test_form_pages.py
6303 6303
    assert formdata.data['3'] == 'a'
6304 6304
    assert formdata.data['3_display'] == 'b'
6305 6305

  
6306
def test_field_live_select_content_on_workflow_form(pub, http_requests):
6307
    create_user(pub)
6308
    wf = Workflow(name='wf-title')
6309
    st1 = wf.add_status('Status1', 'st1')
6310

  
6311
    # form displayed into the workflow
6312
    display_form = FormWorkflowStatusItem()
6313
    display_form.id = '_x'
6314
    display_form.by = ['_submitter']
6315
    display_form.varname = 'xxx'
6316
    display_form.formdef = WorkflowFormFieldsFormDef(item=display_form)
6317
    display_form.formdef.fields = [
6318
        fields.StringField(type='string', id='1', label='Bar', size='40',
6319
            required=True, varname='bar'),
6320
        fields.StringField(type='string', id='2', label='Bar2', size='40',
6321
            required=True, varname='bar2'),
6322
        fields.ItemField(type='item', id='3', label='Foo',
6323
            required=False, varname='foo',
6324
            data_source={
6325
                'type': 'json',
6326
                'value': '{% if xxx_var_bar2 %}http://remote.example.net/json-list?plop={{xxx_var_bar2}}{% endif %}'
6327
            }),
6328
    ]
6329
    st1.items.append(display_form)
6330
    display_form.parent = st1
6331
    wf.store()
6332

  
6333
    # initial empty form
6334
    formdef = create_formdef()
6335
    formdef.fields = []
6336
    formdef.confirmation = False
6337
    formdef.workflow_id = wf.id
6338
    formdef.store()
6339
    formdef.data_class().wipe()
6340

  
6341
    app = get_app(pub)
6342
    resp = login(app, username='foo', password='foo').get('/test/')
6343
    assert 'Forms - test' in resp.text
6344

  
6345
    resp = resp.form.submit('submit').follow()
6346
    assert 'The form has been recorded' in resp.text
6347

  
6348
    assert 'f1' in resp.form.fields
6349
    assert 'f2' in resp.form.fields
6350
    assert resp.html.find('div', {'data-field-id': '2'}).attrs['data-live-source'] == 'true'
6351
    assert resp.html.find('div', {'data-field-id': '3'}).find('select')
6352
    resp.form['f1'] = 'hello'
6353
    live_resp = app.post('/test/1/live', params=resp.form.submit_fields())
6354
    assert live_resp.json['result']['1']['visible']
6355
    assert live_resp.json['result']['2']['visible']
6356
    assert live_resp.json['result']['3']['visible']
6357
    assert not 'items' in live_resp.json['result']['3']
6358
    resp.form['f2'] = 'plop'
6359
    live_resp = app.post('/test/1/live?modified_field_id=2', params=resp.form.submit_fields())
6360
    assert live_resp.json['result']['1']['visible']
6361
    assert live_resp.json['result']['2']['visible']
6362
    assert live_resp.json['result']['3']['visible']
6363
    assert 'items' in live_resp.json['result']['3']
6364
    assert len(live_resp.json['result']['3']['items']) > 0
6365

  
6366
    resp.form['f3'].options = []
6367
    for item in live_resp.json['result']['3']['items']:
6368
        # simulate javascript filling the <select>
6369
        resp.form['f3'].options.append((item['id'], False, item['text']))
6370
    resp.form['f3'] = 'a'
6371

  
6372
    resp = resp.form.submit('submit')
6373
    if resp.status_code != 303:
6374
        assert 'invalid value selected' not in resp.text
6375
    resp = resp.follow()
6376
    formdata = formdef.data_class().select()[0]
6377
    assert formdata.workflow_data['xxx_var_bar'] == 'hello'
6378
    assert formdata.workflow_data['xxx_var_bar2'] == 'plop'
6379
    assert formdata.workflow_data['xxx_var_foo_raw'] == 'a'
6380
    assert formdata.workflow_data['xxx_var_foo'] == 'b'
6381

  
6306 6382
def test_field_live_comment_content(pub, http_requests):
6307 6383
    FormDef.wipe()
6308 6384
    formdef = FormDef()
wcs/forms/common.py
260 260
                templates=list(self.get_formdef_template_variants(self.status_templates)),
261 261
                context=context)
262 262

  
263
    def evaluate_workflow_form(self, user):
264
        fields = []
265
        form = self.filled.get_workflow_form(user, displayed_fields=fields)
266
        self.filled.evaluate_live_workflow_form(user, form)
267
        get_publisher().substitutions.unfeed(lambda x: x is self.filled)
268
        get_publisher().substitutions.feed(self.filled)
269
        # reevaluate workflow form according to possible new content
270
        fields = []
271
        form = self.filled.get_workflow_form(user, displayed_fields=fields)
272
        return form, fields
273

  
263 274
    def get_workflow_form(self, user):
264
        submitted_fields = []
265
        form = self.filled.get_workflow_form(user, displayed_fields=submitted_fields)
275
        form, submitted_fields = self.evaluate_workflow_form(user)
266 276
        if form:
267 277
            form.attrs['data-live-url'] = self.filled.get_url() + 'live'
268 278
        if form and form.is_submitted():
......
674 684
        if not session:
675 685
            return result_error('missing session')
676 686

  
677
        displayed_fields = []
678 687
        user = get_request().user
679
        form = self.filled.get_workflow_form(user, displayed_fields=displayed_fields)
680
        self.filled.evaluate_live_workflow_form(user, form)
681
        get_publisher().substitutions.unfeed(lambda x: x is self.filled)
682
        get_publisher().substitutions.feed(self.filled)
683
        # reevaluate workflow form according to possible new content
684
        displayed_fields = []
685
        form = self.filled.get_workflow_form(user, displayed_fields=displayed_fields)
688
        form, displayed_fields = self.evaluate_workflow_form(user)
686 689
        return self.live_process_fields(form, self.filled, displayed_fields)
687 690

  
688 691
    def _q_lookup(self, component):
689
-