Projet

Général

Profil

0001-fields-fix-live-prefill-for-checkbox-fields-57860.patch

Lauréline Guérin, 15 octobre 2021 11:24

Télécharger (4,9 ko)

Voir les différences:

Subject: [PATCH] fields: fix live prefill for checkbox fields (#57860)

 tests/form_pages/test_live.py        | 62 ++++++++++++++++++++++++++++
 wcs/forms/common.py                  |  2 +
 wcs/qommon/static/js/qommon.forms.js |  6 ++-
 3 files changed, 68 insertions(+), 2 deletions(-)
tests/form_pages/test_live.py
799 799
    live_resp = app.post('/foo/live?modified_field_id=user', params=resp.form.submit_fields(), status=403)
800 800

  
801 801

  
802
def test_field_live_bool_prefill(pub, http_requests):
803
    CardDef.wipe()
804
    carddef = CardDef()
805
    carddef.name = 'foo'
806
    carddef.digest_templates = {'default': '{{ form_var_foo }}'}
807
    carddef.fields = [
808
        fields.StringField(id='1', type='string', label='string', varname='foo'),
809
        fields.BoolField(
810
            type='bool',
811
            id='2',
812
            varname='bool',
813
        ),
814
    ]
815
    carddef.store()
816
    carddef.data_class().wipe()
817
    carddata1 = carddef.data_class()()
818
    carddata1.data = {
819
        '1': 'bar',
820
        '2': True,
821
    }
822
    carddata1.just_created()
823
    carddata1.store()
824
    carddata2 = carddef.data_class()()
825
    carddata2.data = {
826
        '1': 'baz',
827
        '2': False,
828
    }
829
    carddata2.just_created()
830
    carddata2.store()
831
    FormDef.wipe()
832
    formdef = FormDef()
833
    formdef.name = 'Foo'
834
    formdef.fields = [
835
        fields.ItemField(
836
            type='item', id='1', label='foo', varname='foo', data_source={'type': 'carddef:foo'}
837
        ),
838
        fields.BoolField(
839
            type='bool',
840
            id='2',
841
            label='bool',
842
            varname='bool',
843
            prefill={'type': 'string', 'value': '{{ form_var_foo_live_var_bool }}'},
844
        ),
845
    ]
846
    formdef.store()
847
    formdef.data_class().wipe()
848

  
849
    app = get_app(pub)
850
    resp = app.get('/foo/')
851
    assert resp.html.find('div', {'data-field-id': '1'}).attrs['data-live-source'] == 'true'
852
    assert resp.pyquery('#var_bool.widget-prefilled')  # second field is marked as prefilled
853
    assert resp.form['f2'].value is None
854
    resp.form['f1'] = str(carddata1.id)
855
    live_resp = app.post('/foo/live?modified_field_id=1&prefilled_2=on', params=resp.form.submit_fields())
856
    assert live_resp.json['result']['2'] == {'visible': True, 'content': True}
857

  
858
    resp.form['f2'] = False  # manually changed -> widget-prefilled class will be removed
859
    resp.form['f1'] = str(carddata2.id)
860
    live_resp = app.post('/foo/live?modified_field_id=1', params=resp.form.submit_fields())
861
    assert live_resp.json['result']['2'] == {'visible': True}
862

  
863

  
802 864
def test_field_live_block_string_prefill(pub, http_requests):
803 865
    FormDef.wipe()
804 866
    BlockDef.wipe()
wcs/forms/common.py
798 798
                update_prefill = bool('prefilled_%s' % field.id in get_request().form)
799 799
                if update_prefill:
800 800
                    value = field.get_prefill_value()[0]
801
                    if field.key == 'bool':
802
                        value = field.convert_value_from_str(value)
801 803
                    entry['content'] = value
802 804
            elif field.prefill and field.prefill.get('type') == 'user':
803 805
                update_prefill = bool(get_request().form.get('modified_field_id') == 'user')
wcs/qommon/static/js/qommon.forms.js
471 471
            }
472 472
            $select.trigger('wcs:options-change', {items: value.items});
473 473
          }
474
          if (value.content) {
474
          if (typeof value.content !== 'undefined') {
475 475
            $widget.each(function(idx, widget) {
476 476
              if ($widget.hasClass('comment-field')) {
477 477
                // replace comment content
478 478
                $widget.html(value.content);
479 479
              } else {
480
                // replace text input value
481 480
                if ($(widget).is('.widget-prefilled') || $(widget).is('.widget-readonly') || data.modified_field == 'user') {
481
                  // replace text input value
482 482
                  $(widget).find('input[type=text], input[type=tel], input[type=numeric], input[type=email], textarea').val(value.content);
483
                  // replace checkbox input value
484
                  $(widget).find('input[type=checkbox]').prop('checked', value.content);
483 485
                }
484 486
              }
485 487
            });
486
-