Projet

Général

Profil

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

Benjamin Dauvergne, 09 décembre 2022 16:25

Télécharger (6,31 ko)

Voir les différences:

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

 tests/form_pages/test_all.py | 38 ++++++++++++++++++++++++++++++++----
 wcs/api.py                   | 28 +++++++++-----------------
 2 files changed, 43 insertions(+), 23 deletions(-)
tests/form_pages/test_all.py
6660 6660
        resp2 = app.get(select2_url + '?q=hell')
6661 6661
        assert len(rsps.calls) == 1
6662 6662
        assert rsps.calls[-1].request.url == 'http://remote.example.net/json?q=hell'
6663
        assert resp2.json == data
6663
        assert resp2.json == dict(data, err=0)
6664 6664

  
6665 6665
        # check unauthorized access
6666 6666
        resp2 = get_app(pub).get(select2_url + '?q=hell', status=403)
......
6680 6680
        resp2 = app.get(select2_url + '?q=hell')
6681 6681
        assert len(rsps.calls) == 1
6682 6682
        assert rsps.calls[-1].request.url == 'http://remote.example.net/json?q=hell'
6683
        assert resp2.json == {'data': [], 'err': '1'}
6683
        assert resp2.json == {'data': [], 'err': 1}
6684 6684
        assert emails.count() == 0
6685 6685

  
6686 6686
        data_source.notify_on_errors = True
......
6739 6739
        resp2 = app.get(select2_url + '?q=hell')
6740 6740
        assert len(rsps.calls) == 1
6741 6741
        assert rsps.calls[-1].request.url == 'http://remote.example.net/json-numeric-id?q=hell'
6742
        assert resp2.json == data
6742
        assert resp2.json == dict(data, err=0)
6743 6743

  
6744 6744
        # check unauthorized access
6745 6745
        resp2 = get_app(pub).get(select2_url + '?q=hell', status=403)
......
6795 6795
        assert rsps.calls[-1].request.url.startswith(
6796 6796
            'http://remote.example.net/json?q=hell&orig=example.net&'
6797 6797
        )
6798
        assert resp2.json == data
6798
        assert resp2.json == dict(data, err=0)
6799 6799

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

  
6865
    # check with data, id and text attribute
6866
    data_source.data_source = {'type': 'json', 'value': 'http://remote.example.net/json'}
6867
    data_source.data_attribute = 'x.results'
6868
    data_source.id_attribute = 'key'
6869
    data_source.text_attribute = 'value'
6870
    data_source.store()
6871

  
6872
    formdef.data_class().wipe()
6873

  
6874
    app = get_app(pub)
6875
    with responses.RequestsMock() as rsps:
6876
        resp = app.get('/test/')
6877
        pq = resp.pyquery.remove_namespaces()
6878
        select2_url = pq('select').attr['data-select2-url']
6879

  
6880
    with responses.RequestsMock() as rsps:
6881
        data = {
6882
            'x': {
6883
                'results': [
6884
                    {'key': '1', 'value': 'hello', 'extra': 'foo'},
6885
                ]
6886
            }
6887
        }
6888
        normalized_data = {
6889
            'data': [{'id': '1', 'text': 'hello', 'extra': 'foo', 'key': '1', 'value': 'hello'}]
6890
        }
6891
        rsps.get('http://remote.example.net/json', json=data)
6892
        resp2 = app.get(select2_url + '?q=hell')
6893
        assert resp2.json == dict(normalized_data, err=0)
6894

  
6865 6895

  
6866 6896
def test_item_field_autocomplete_jsonp_source(http_requests, pub):
6867 6897
    create_user(pub)
wcs/api.py
36 36
from wcs.ctl.hobo_notify import CmdHoboNotify
37 37
from wcs.data_sources import NamedDataSource
38 38
from wcs.data_sources import get_object as get_data_source_object
39
from wcs.data_sources import request_json_items
39 40
from wcs.formdef import FormDef
40 41
from wcs.forms.common import FormStatusPage
41 42
from wcs.qommon import get_cfg
......
48 49
from .backoffice.management import ManagementDirectory
49 50
from .backoffice.submission import SubmissionDirectory
50 51
from .qommon import _, misc
51
from .qommon.errors import (
52
    AccessForbiddenError,
53
    ConnectionError,
54
    TraversalError,
55
    UnknownNameIdAccessForbiddenError,
56
)
52
from .qommon.errors import AccessForbiddenError, TraversalError, UnknownNameIdAccessForbiddenError
57 53
from .qommon.form import ComputedExpressionWidget
58 54
from .qommon.storage import Contains, Equal, Intersects, NotContains, Or, StrictNotEqual
59 55
from .qommon.template import Template, TemplateError
......
1190 1186
        info = token.context
1191 1187

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

  
1211 1201
        # carddef_ref in info
1212 1202
        carddef_ref = info['carddef_ref']
1213
-