Projet

Général

Profil

0001-forms-only-include-pages-with-actual-data-in-summary.patch

Frédéric Péters, 07 juin 2018 15:33

Télécharger (8,67 ko)

Voir les différences:

Subject: [PATCH] forms: only include pages with actual data in summary
 (#24048)

 tests/test_form_pages.py | 65 ++++++++++++++++++++++++++++++
 wcs/forms/common.py      | 87 +++++++++++++++++++++++-----------------
 2 files changed, 115 insertions(+), 37 deletions(-)
tests/test_form_pages.py
1041 1041
    assert '<h3>1st page</h3>' in resp.body
1042 1042
    assert '<h4>subtitle of 1st page</h4>' in resp.body
1043 1043
    assert '<h3>title of second page</h3>' in resp.body
1044
    resp.form['f3'] = 'foo'
1044 1045
    resp = resp.form.submit('submit').follow()  # -> submit
1045 1046
    assert '<h3>1st page</h3>' in resp.body
1046 1047
    assert not '<div class="title "><h3>1st page</h3></div>' in resp.body
1047 1048
    assert '<div class="subtitle "><h4>subtitle of 1st page</h4></div>' in resp.body
1048 1049
    assert '<div class="title "><h3>title of second page</h3></div>' in resp.body
1049 1050

  
1051
def test_form_summary_empty_pages(pub):
1052
    user = create_user(pub)
1053
    formdef = create_formdef()
1054
    formdef.fields = [
1055
        fields.PageField(id='0', label='1st page', type='page'),
1056
        fields.StringField(id='1', label='string', varname='toto'),
1057
        fields.PageField(id='2', label='2nd page', type='page',
1058
            condition={'type': 'python', 'value': 'form_var_toto == "foo"'}),
1059
        fields.TitleField(id='6', label='title in second page', type='title'),
1060
        fields.StringField(id='3', label='string'),
1061
        fields.PageField(id='4', label='3rd page', type='page'),
1062
        fields.StringField(id='5', label='string'),
1063
        fields.PageField(id='7', label='4th page', type='page'),
1064
        fields.CommentField(id='8', label='Bla bla bla', type='comment'),
1065
    ]
1066
    formdef.store()
1067
    formdef.data_class().wipe()
1068

  
1069
    app = login(get_app(pub), username='foo', password='foo')
1070

  
1071
    resp = app.get('/test/')  # -> 1st page
1072
    resp.form['f1'] = 'foo'
1073
    resp = resp.form.submit('submit')  # -> 2nd page
1074
    resp.form['f3'] = 'bar'
1075
    resp = resp.form.submit('submit')  # -> 3rd page
1076
    resp.form['f5'] = 'baz'
1077
    resp = resp.form.submit('submit')  # -> 4th page
1078
    resp = resp.form.submit('submit')  # -> validation
1079
    resp = resp.form.submit('submit')
1080
    formdata_id = resp.location.split('/')[-2]
1081
    resp = resp.follow()  # -> submit
1082
    assert '<h3>1st page</h3>' in resp.body
1083
    assert '<h3>2nd page</h3>' in resp.body
1084
    assert '<h3>3rd page</h3>' in resp.body
1085
    assert '<h3>4th page</h3>' not in resp.body
1086

  
1087
    resp = app.get('/test/')  # -> 1st page
1088
    resp.form['f1'] = 'foo'
1089
    resp = resp.form.submit('submit')  # -> 2nd page
1090
    resp.form['f3'] = 'bar'
1091
    resp = resp.form.submit('previous')  # -> 1st page
1092
    resp.form['f1'] = 'baz'
1093
    resp = resp.form.submit('submit')  # -> 3rd page
1094
    resp.form['f5'] = 'baz'
1095
    resp = resp.form.submit('submit')  # -> 4th page
1096
    resp = resp.form.submit('submit')  # -> validation
1097
    resp = resp.form.submit('submit').follow()  # -> submit
1098
    assert '<h3>1st page</h3>' in resp.body
1099
    assert '<h3>2nd page</h3>' not in resp.body
1100
    assert '<h3>3rd page</h3>' in resp.body
1101
    assert '<h3>4th page</h3>' not in resp.body
1102

  
1103
    # change condition to have second page never displayed
1104
    formdef.fields[2].condition['value'] = False
1105
    formdef.store()
1106
    formdata = formdef.data_class().get(formdata_id)
1107
    resp = app.get(formdata.get_url())
1108
    # it was filled by user, it should still appear (conditions should not be
1109
    # replayed)
1110
    assert '<h3>1st page</h3>' in resp.body
1111
    assert '<h3>2nd page</h3>' in resp.body
1112
    assert '<h3>3rd page</h3>' in resp.body
1113
    assert '<h3>4th page</h3>' not in resp.body
1114

  
1050 1115
def test_form_visit_existing(pub):
1051 1116
    user = create_user(pub)
1052 1117
    formdef = create_formdef()
wcs/forms/common.py
332 332
        r = TemplateIO(html=True)
333 333
        on_page = False
334 334
        on_disabled_page = False
335
        pages = []
336
        current_page_fields = []
337

  
338
        def get_value(f):
339
            if not self.filled.data.has_key(f.id):
340
                value = None
341
            else:
342
                if f.store_display_value and ('%s_display' % f.id) in self.filled.data:
343
                    value = self.filled.data['%s_display' % f.id]
344
                else:
345
                    value = self.filled.data[f.id]
346

  
347
                if value is None or value == '':
348
                    value = None
349
            return value
350

  
335 351
        for i, f in enumerate(fields):
336 352
            if f.type == 'page':
337
                on_disabled_page = False
338
                if not f.is_visible(self.filled.data, self.formdef):
339
                    on_disabled_page = True
340

  
341
                form_field = False
342
                for f1 in self.formdef.fields[self.formdef.fields.index(f)+1:]:
343
                    if f1.key == 'page':
344
                        break
345
                    if isinstance(f1, WidgetField):
346
                        form_field = True
347
                        break
348
                if form_field is False:
349
                    on_disabled_page = True
350

  
351
            if on_disabled_page:
353
                on_page = f
354
                current_page_fields = []
355
                pages.append({'page': f, 'fields': current_page_fields})
356
                continue
357

  
358
            if f.type == 'title' and on_page and fields[i-1] is on_page and on_page.label == f.label:
359
                # don't include first title of a page if that title has the
360
                # same text as the page.
352 361
                continue
353 362

  
363
            if f.type in ('title', 'subtitle'):
364
                current_page_fields.append({'field': f})
365
                continue
366

  
367
            if not hasattr(f, 'get_view_value'):
368
                continue
369

  
370
            value = get_value(f)
371
            if value is None and not (f.required and include_unset_required_fields):
372
                continue
354 373

  
374
            current_page_fields.append({'field': f, 'value': value})
375

  
376
        if not pages:
377
            fields = [x['field'] for x in current_page_fields]
378
        else:
379
            # ignore empty pages
380
            fields = []
381
            for page in pages:
382
                if not any([x.has_key('value') for x in page['fields']]):
383
                    continue
384
                fields.append(page['page'])
385
                fields.extend([x['field'] for x in page['fields']])
386

  
387
        on_page = None
388
        for f in fields:
355 389
            if f.type == 'page':
356 390
                if on_page:
357 391
                    r += htmltext('</div>')
......
362 396
                on_page = f
363 397
                continue
364 398

  
365
            if f.type == 'title' and on_page and fields[i-1] is on_page and on_page.label == f.label:
366
                # don't include first title of a page if that title has the
367
                # same text as the page.
368
                continue
369

  
370 399
            if f.type == 'title':
371 400
                r += htmltext('<div class="title %s"><h3>%s</h3></div>') % (f.extra_css_class or '', f.label)
372 401
                continue
......
375 404
                r += htmltext('<div class="subtitle %s"><h4>%s</h4></div>') % (f.extra_css_class or '', f.label)
376 405
                continue
377 406

  
378
            if not hasattr(f, str('get_view_value')):
379
                continue
380

  
381
            if not self.filled.data.has_key(f.id):
382
                value = None
383
            else:
384
                if f.store_display_value and ('%s_display' % f.id) in self.filled.data:
385
                    value = self.filled.data['%s_display' % f.id]
386
                else:
387
                    value = self.filled.data[f.id]
388

  
389
                if value is None or value == '':
390
                    value = None
391

  
392
            if value is None and not (f.required and include_unset_required_fields):
393
                continue
394

  
395 407
            css_classes = ['field', 'field-type-%s' % f.key]
396 408
            if f.extra_css_class:
397 409
                css_classes.append(f.extra_css_class)
398 410
            r += htmltext('<div class="%s">' % ' '.join(css_classes))
399 411
            r += htmltext('<span class="label">%s</span> ') % f.label
412
            value = get_value(f)
400 413
            if value is None:
401 414
                r += htmltext('<div class="value"><i>%s</i></div>') % _('Not set')
402 415
            else:
403
-