From 24ef0f870cad87d0f61e1ba97d2bc40401426c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Mon, 19 Oct 2020 16:58:39 +0200 Subject: [PATCH] fields: autocomplete for string field with json datasource (#45230) --- tests/test_form_pages.py | 46 ++++++++++++++++++++++++++++++++++++++++ wcs/fields.py | 6 +++--- wcs/qommon/form.py | 5 +++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py index 093c27425..0fde1fabe 100644 --- a/tests/test_form_pages.py +++ b/tests/test_form_pages.py @@ -5236,6 +5236,52 @@ def test_form_string_field_autocomplete(pub): assert 'http://example.net' in resp.text +def test_form_string_field_autocomplete_named_datasource(pub): + FormDef.wipe() + formdef = FormDef() + formdef.name = 'test' + formdef.fields = [fields.StringField(id='0', label='string', type='string', required=False, data_source={'type': 'foobar'})] + formdef.store() + + # jsonp datasource + NamedDataSource.wipe() + data_source = NamedDataSource(name='foobar') + data_source.data_source = {'type': 'jsonp', 'value': 'http://remote.example.net/json'} + data_source.store() + + resp = get_app(pub).get('/test/') + assert ').autocomplete({' in resp.text + assert "options.url = 'http://remote.example.net/json'" in resp.text + assert "options.url = '/api/autocomplete/" not in resp.text + assert 'dataType: "jsonp",' in resp.text + + # json datasource + data_source.data_source['type'] = 'json' + data_source.query_parameter = 'q' + data_source.store() + + resp = get_app(pub).get('/test/') + assert ').autocomplete({' in resp.text + assert "options.url = 'http://remote.example.net/json'" not in resp.text + assert "options.url = '/api/autocomplete/" in resp.text + assert 'dataType: "json",' in resp.text + + # card datasource + CardDef.wipe() + carddef = CardDef() + carddef.name = 'Foo' + carddef.fields = [] + carddef.store() + + data_source.data_source['type'] = 'carddef:foo' + data_source.store() + resp = get_app(pub).get('/test/') + assert ').autocomplete({' in resp.text + assert "options.url = 'http://remote.example.net/json'" not in resp.text + assert "options.url = '/api/autocomplete/" in resp.text + assert 'dataType: "json",' in resp.text + + def test_form_workflow_trigger(pub): user = create_user(pub) diff --git a/wcs/fields.py b/wcs/fields.py index 877f448b9..2ce6fe67b 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -805,9 +805,9 @@ class StringField(WidgetField): def perform_more_widget_changes(self, form, kwargs, edit=True): if self.data_source: - real_data_source = data_sources.get_real(self.data_source) - if real_data_source.get('type') == 'jsonp': - kwargs['url'] = real_data_source.get('value') + data_source = data_sources.get_object(self.data_source) + if data_source.can_jsonp(): + kwargs['url'] = data_source.get_jsonp_url() self.widget_class = AutocompleteStringWidget def fill_admin_form(self, form): diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index bbcc88858..0365d081d 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -2141,6 +2141,7 @@ class AutocompleteStringWidget(WcsExtraStringWidget): # there's no autocomplete URL, get out now. return r.getvalue() + data_type = 'json' if url.startswith('/api/autocomplete/') else 'jsonp' r += htmltext("""