Projet

Général

Profil

0001-misc-look-up-text-option-using-q-if-available-66695.patch

Frédéric Péters, 28 juin 2022 16:17

Télécharger (5,45 ko)

Voir les différences:

Subject: [PATCH] misc: look up text option using ?q= if available (#66695)

 tests/form_pages/test_live.py | 51 +++++++++++++++++++++++++++++++++++
 wcs/data_sources.py           | 17 ++++++++++++
 wcs/fields.py                 |  5 ++++
 wcs/forms/common.py           |  8 +++---
 4 files changed, 76 insertions(+), 5 deletions(-)
tests/form_pages/test_live.py
1050 1050
    assert live_resp.json['result'] == {'2': {'visible': True, 'content': '2'}}
1051 1051

  
1052 1052

  
1053
@mock.patch('wcs.qommon.misc.urlopen')
1054
def test_field_live_item_datasource_prefill_with_request_with_q(urlopen, pub):
1055
    NamedDataSource.wipe()
1056
    data_source = NamedDataSource(name='foobar')
1057
    data_source.data_source = {
1058
        'type': 'json',
1059
        'value': 'http://remote.example.net/json?plop={{form_var_bar}}',
1060
    }
1061
    data_source.query_parameter = 'q'
1062
    data_source.id_parameter = 'id'
1063
    data_source.store()
1064

  
1065
    data = {'data': [{'id': '1', 'text': 'un'}, {'id': '2', 'text': 'deux', 'x': 'bye'}]}
1066
    urlopen.side_effect = lambda *args: io.StringIO(json.dumps(data))
1067
    ds = {'type': 'foobar'}
1068

  
1069
    FormDef.wipe()
1070
    formdef = FormDef()
1071
    formdef.name = 'Foo'
1072
    formdef.fields = [
1073
        fields.ItemField(
1074
            id='1',
1075
            label='item',
1076
            data_source=ds,
1077
            varname='foo',
1078
            prefill={'type': 'string', 'value': '{{ request.GET.plop }}'},
1079
        ),
1080
        fields.ItemField(
1081
            type='item',
1082
            id='2',
1083
            label='item',
1084
            varname='item',
1085
            prefill={'type': 'string', 'value': '{{ form_var_foo }}'},
1086
            data_source=ds,
1087
        ),
1088
    ]
1089
    formdef.store()
1090

  
1091
    app = get_app(pub)
1092
    resp = app.get('/foo/?plop=2')
1093
    assert resp.html.find('div', {'data-field-id': '1'}).attrs['data-live-source'] == 'true'
1094
    assert resp.pyquery('#var_item.widget-prefilled')  # second field is marked as prefilled
1095
    assert resp.form['f1'].value == '2'
1096
    live_resp = app.post(
1097
        '/foo/live?modified_field_id=init&prefilled_1=on&prefilled_2=on', params=resp.form.submit_fields()
1098
    )
1099
    assert live_resp.json['result'] == {'2': {'visible': True, 'content': '2'}}
1100
    # check it has ?q=
1101
    assert urlopen.call_args[0][0] == 'http://remote.example.net/json?plop=&q=deux'
1102

  
1103

  
1053 1104
def test_field_live_block_string_prefill(pub, http_requests):
1054 1105
    FormDef.wipe()
1055 1106
    BlockDef.wipe()
wcs/data_sources.py
198 198
    return tupled_items
199 199

  
200 200

  
201
def get_id_by_option_text(data_source, text_value):
202
    data_source = get_object(data_source)
203
    if data_source:
204
        if data_source.data_source.get('type') == 'json' and data_source.query_parameter:
205
            url = data_source.get_json_query_url()
206
            url += urllib.parse.quote(text_value)
207
            items = request_json_items(url, data_source.extended_data_source)
208
        else:
209
            items = get_structured_items(data_source.extended_data_source, include_disabled=False)
210

  
211
        # fallback to iterating on all options
212
        for option in items:
213
            # get raw value from display value
214
            if option['text'] == text_value:
215
                return str(option['id'])
216

  
217

  
201 218
def get_json_from_url(url, data_source=None, log_message_part='JSON data source'):
202 219
    url = sign_url_auto_orig(url)
203 220
    data_source = data_source or {}
wcs/fields.py
2121 2121
            return [(x, x) for x in self.items]
2122 2122
        return []
2123 2123

  
2124
    def get_id_by_option_text(self, text_value):
2125
        if self.data_source:
2126
            return data_sources.get_id_by_option_text(self.data_source, text_value)
2127
        return text_value
2128

  
2124 2129
    def get_extended_options(self):
2125 2130
        if self.data_source:
2126 2131
            return data_sources.get_structured_items(
wcs/forms/common.py
861 861
                            text_content = None
862 862
                        entry['text_content'] = text_content
863 863
                    elif field.key == 'item' and value:
864
                        for option in field.get_options():
865
                            # get raw value from display value
866
                            if option[1] == value:
867
                                value = option[0]
868
                                break
864
                        id_value = field.get_id_by_option_text(value)
865
                        if id_value:
866
                            value = id_value
869 867
                    entry['content'] = value
870 868
            elif field.prefill and field.prefill.get('type') == 'user':
871 869
                update_prefill = bool(get_request().form.get('modified_field_id') == 'user')
872
-