Projet

Général

Profil

0001-fields-fix-live-prefill-for-items-with-carddef-datas.patch

Lauréline Guérin, 07 juillet 2022 10:36

Télécharger (7,21 ko)

Voir les différences:

Subject: [PATCH] fields: fix live prefill for items with carddef datasource
 (#59052)

 tests/form_pages/test_live.py                 | 83 +++++++++++++++++++
 wcs/fields.py                                 |  7 ++
 wcs/forms/common.py                           |  4 +
 wcs/qommon/static/js/qommon.forms.js          |  8 +-
 .../qommon/forms/widgets/checkboxes.html      |  1 +
 5 files changed, 102 insertions(+), 1 deletion(-)
tests/form_pages/test_live.py
1184 1184
    assert urlopen.call_args[0][0] == 'http://remote.example.net/json?plop=&q=deux'
1185 1185

  
1186 1186

  
1187
def test_field_live_items_datasource_carddef_prefill(pub, http_requests):
1188
    CardDef.wipe()
1189

  
1190
    carddef_related = CardDef()
1191
    carddef_related.name = 'bar'
1192
    carddef_related.digest_templates = {'default': '{{ form_var_bar }}'}
1193
    carddef_related.fields = [
1194
        fields.StringField(id='1', type='string', label='string', varname='bar'),
1195
    ]
1196
    carddef_related.store()
1197
    carddef_related.data_class().wipe()
1198
    for value in ['A', 'B', 'C']:
1199
        carddata = carddef_related.data_class()()
1200
        carddata.data = {
1201
            '1': value,
1202
        }
1203
        carddata.just_created()
1204
        carddata.store()
1205

  
1206
    carddef = CardDef()
1207
    carddef.name = 'foo'
1208
    carddef.digest_templates = {'default': '{{ form_var_foo }}'}
1209
    carddef.fields = [
1210
        fields.StringField(id='1', type='string', label='string', varname='foo'),
1211
        fields.ItemsField(
1212
            type='items',
1213
            id='2',
1214
            varname='items',
1215
            data_source={'type': 'carddef:bar'},
1216
        ),
1217
    ]
1218
    carddef.store()
1219
    carddef.data_class().wipe()
1220
    carddata1 = carddef.data_class()()
1221
    carddata1.data = {
1222
        '1': 'bar',
1223
        '2': ['1'],
1224
        '2_display': ['A'],
1225
    }
1226
    carddata1.just_created()
1227
    carddata1.store()
1228
    carddata2 = carddef.data_class()()
1229
    carddata2.data = {
1230
        '1': 'baz',
1231
        '2': ['2', '3'],
1232
        '2_display': 'B,C',
1233
    }
1234
    carddata2.just_created()
1235
    carddata2.store()
1236

  
1237
    FormDef.wipe()
1238
    formdef = FormDef()
1239
    formdef.name = 'Foo'
1240
    formdef.fields = [
1241
        fields.ItemField(
1242
            type='item', id='1', label='foo', varname='foo', data_source={'type': 'carddef:foo'}
1243
        ),
1244
        fields.ItemsField(
1245
            type='items',
1246
            id='2',
1247
            label='items',
1248
            varname='items',
1249
            prefill={'type': 'string', 'value': '{{ form_var_foo_live_var_items }}'},
1250
            data_source={'type': 'carddef:bar'},
1251
        ),
1252
    ]
1253
    formdef.store()
1254
    formdef.data_class().wipe()
1255

  
1256
    app = get_app(pub)
1257
    resp = app.get('/foo/')
1258
    assert resp.html.find('div', {'data-field-id': '1'}).attrs['data-live-source'] == 'true'
1259
    assert resp.pyquery('#var_items.widget-prefilled')  # second field is marked as prefilled
1260
    resp.form['f1'] = str(carddata2.id)
1261
    live_resp = app.post('/foo/live?modified_field_id=1&prefilled_2=on', params=resp.form.submit_fields())
1262
    assert live_resp.json['result']['2'] == {'visible': True, 'content': ['2', '3']}
1263

  
1264
    resp.form['f2$element1'] = True  # manually changed -> widget-prefilled class will be removed
1265
    resp.form['f1'] = str(carddata1.id)
1266
    live_resp = app.post('/foo/live?modified_field_id=1', params=resp.form.submit_fields())
1267
    assert live_resp.json['result']['2'] == {'visible': True}
1268

  
1269

  
1187 1270
def test_field_live_block_string_prefill(pub, http_requests):
1188 1271
    FormDef.wipe()
1189 1272
    BlockDef.wipe()
wcs/fields.py
2483 2483
        else:
2484 2484
            return []
2485 2485

  
2486
    def get_ids_by_option_text(self, text_values):
2487
        if self.data_source:
2488
            ids = [data_sources.get_id_by_option_text(self.data_source, v) for v in text_values]
2489
            ids = [i for i in ids if i]
2490
            return ids
2491
        return text_values
2492

  
2486 2493
    def perform_more_widget_changes(self, form, kwargs, edit=True):
2487 2494
        kwargs['options'] = self.get_options()
2488 2495
        kwargs['min_choices'] = self.min_choices
wcs/forms/common.py
864 864
                        id_value = field.get_id_by_option_text(value)
865 865
                        if id_value:
866 866
                            value = id_value
867
                    elif field.key == 'items' and value:
868
                        id_values = field.get_ids_by_option_text([v.strip() for v in value.split(',')])
869
                        if id_values:
870
                            value = id_values
867 871
                    entry['content'] = value
868 872
            elif field.prefill and field.prefill.get('type') == 'user':
869 873
                update_prefill = bool(get_request().form.get('modified_field_id') == 'user')
wcs/qommon/static/js/qommon.forms.js
588 588
                    // replace checkbox input value
589 589
                    $widget.find('input[type=checkbox]').prop('checked', value.content);
590 590
                  }
591
                  if ($widget.hasClass('CheckboxesWidget') && Array.isArray(value.content)) {
592
                    $($widget).find('input[type=checkbox]').prop('checked', false);
593
                    $.each(value.content, function() {
594
                      $($widget).find('input[type=checkbox][data-value="' + this + '"]').prop('checked', true);
595
                    });
596
                  }
591 597
                  // replace select value
592
                  $(widget).find('select').val(value.content);
598
                  $(widget).find('select').val(value.content).trigger('change.select2');
593 599
                  // replace radio value
594 600
                  $(widget).find('input[type=radio]').prop('checked', false);
595 601
                  $(widget).find('input[type=radio][value="'+value.content+'"]').prop('checked', true);
wcs/qommon/templates/qommon/forms/widgets/checkboxes.html
8 8
   {% if option.selected %}checked="checked"
9 9
   {% elif widget.readonly or option.disabled %}disabled{% endif %}
10 10
   {% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}" {% endfor %}
11
   data-value="{{ option.value }}"
11 12
   type="checkbox"
12 13
   {% if widget.readonly %}value="yes" disabled {% else %}name="{{ option.name }}" value="yes"{% endif %}
13 14
   ><span>{{ option.label }}</span></label>
14
-