Projet

Général

Profil

0001-api-apply-named-ds-parameters-on-autocomplete-result.patch

Benjamin Dauvergne, 29 octobre 2022 19:05

Télécharger (5,66 ko)

Voir les différences:

Subject: [PATCH] api: apply named ds parameters on autocomplete results
 (#39723)

 tests/form_pages/test_all.py | 38 ++++++++++++++++++++++++++++++++----
 wcs/api.py                   | 21 ++++++++------------
 2 files changed, 42 insertions(+), 17 deletions(-)
tests/form_pages/test_all.py
6562 6562
        resp2 = app.get(select2_url + '?q=hell')
6563 6563
        assert len(rsps.calls) == 1
6564 6564
        assert rsps.calls[-1].request.url == 'http://remote.example.net/json?q=hell'
6565
        assert resp2.json == data
6565
        assert resp2.json == dict(data, err=0)
6566 6566

  
6567 6567
        # check unauthorized access
6568 6568
        resp2 = get_app(pub).get(select2_url + '?q=hell', status=403)
......
6582 6582
        resp2 = app.get(select2_url + '?q=hell')
6583 6583
        assert len(rsps.calls) == 1
6584 6584
        assert rsps.calls[-1].request.url == 'http://remote.example.net/json?q=hell'
6585
        assert resp2.json == {'data': [], 'err': '1'}
6585
        assert resp2.json == {'data': [], 'err': 1}
6586 6586
        assert emails.count() == 0
6587 6587

  
6588 6588
        data_source.notify_on_errors = True
......
6641 6641
        resp2 = app.get(select2_url + '?q=hell')
6642 6642
        assert len(rsps.calls) == 1
6643 6643
        assert rsps.calls[-1].request.url == 'http://remote.example.net/json-numeric-id?q=hell'
6644
        assert resp2.json == data
6644
        assert resp2.json == dict(data, err=0)
6645 6645

  
6646 6646
        # check unauthorized access
6647 6647
        resp2 = get_app(pub).get(select2_url + '?q=hell', status=403)
......
6697 6697
        assert rsps.calls[-1].request.url.startswith(
6698 6698
            'http://remote.example.net/json?q=hell&orig=example.net&'
6699 6699
        )
6700
        assert resp2.json == data
6700
        assert resp2.json == dict(data, err=0)
6701 6701

  
6702 6702
    # simulate select2 mode, with qommon.forms.js adding an extra hidden widget
6703 6703
    resp.form.fields['f0_display'] = Hidden(form=resp.form, tag='input', name='f0_display', pos=10)
......
6764 6764
        resp2 = app.get(select2_url + '?q=hell', status=403)
6765 6765
        assert len(rsps.calls) == 0
6766 6766

  
6767
    # check with data, id and text attribute
6768
    data_source.data_source = {'type': 'json', 'value': 'http://remote.example.net/json'}
6769
    data_source.data_attribute = 'x.results'
6770
    data_source.id_attribute = 'key'
6771
    data_source.text_attribute = 'value'
6772
    data_source.store()
6773

  
6774
    formdef.data_class().wipe()
6775

  
6776
    app = get_app(pub)
6777
    with responses.RequestsMock() as rsps:
6778
        resp = app.get('/test/')
6779
        pq = resp.pyquery.remove_namespaces()
6780
        select2_url = pq('select').attr['data-select2-url']
6781

  
6782
    with responses.RequestsMock() as rsps:
6783
        data = {
6784
            'x': {
6785
                'results': [
6786
                    {'key': '1', 'value': 'hello', 'extra': 'foo'},
6787
                ]
6788
            }
6789
        }
6790
        normalized_data = {
6791
            'data': [{'id': '1', 'text': 'hello', 'extra': 'foo', 'key': '1', 'value': 'hello'}]
6792
        }
6793
        rsps.get('http://remote.example.net/json', json=data)
6794
        resp2 = app.get(select2_url + '?q=hell')
6795
        assert resp2.json == dict(normalized_data, err=0)
6796

  
6767 6797

  
6768 6798
def test_item_field_autocomplete_jsonp_source(http_requests, pub):
6769 6799
    create_user(pub)
wcs/api.py
35 35
from wcs.ctl.hobo_notify import CmdHoboNotify
36 36
from wcs.data_sources import NamedDataSource
37 37
from wcs.data_sources import get_object as get_data_source_object
38
from wcs.data_sources import request_json_items
38 39
from wcs.formdef import FormDef
39 40
from wcs.forms.common import FormStatusPage
40 41
from wcs.qommon import get_cfg
......
1180 1181
        info = token.context
1181 1182

  
1182 1183
        if 'url' in info:
1184
            named_data_source = None
1185
            if info.get('data_source'):
1186
                named_data_source = NamedDataSource.get(info['data_source'])
1183 1187
            url = info['url']
1184 1188
            url += urllib.parse.quote(get_request().form.get('q', ''))
1185 1189
            url = sign_url_auto_orig(url)
1186 1190
            get_response().set_content_type('application/json')
1187
            try:
1188
                return misc.urlopen(url).read()
1189
            except ConnectionError as e:
1190
                if 'data_source' in info:
1191
                    error_summary = 'Error loading JSON data source (%s)' % str(e)
1192
                    data_source = NamedDataSource.get(info['data_source'])
1193
                    get_publisher().record_error(
1194
                        error_summary,
1195
                        context='[DATASOURCE]',
1196
                        notify=data_source.notify_on_errors,
1197
                        record=data_source.record_on_errors,
1198
                    )
1199
                return json.dumps({'data': [], 'err': '1'})
1191
            entries = request_json_items(url, named_data_source and named_data_source.extended_data_source)
1192
            if entries is not None:
1193
                return json.dumps({'err': 0, 'data': entries})
1194
            return json.dumps({'err': 1, 'data': []})
1200 1195

  
1201 1196
        # carddef_ref in info
1202 1197
        carddef_ref = info['carddef_ref']
1203
-