fields: autocomplete for string field with json datasource (#45230)
This commit is contained in:
parent
8b4f0fbbdc
commit
24ef0f870c
|
@ -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)
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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("""
|
||||
<script id="script_%(id)s">
|
||||
$(function() {
|
||||
|
@ -2148,7 +2149,7 @@ $(function() {
|
|||
source: function( request, response ) {
|
||||
$.ajax({
|
||||
url: $("#form_%(id)s").data('uiAutocomplete').options.url,
|
||||
dataType: "jsonp",
|
||||
dataType: "%(data_type)s",
|
||||
data: {
|
||||
q: request.term
|
||||
},
|
||||
|
@ -2167,7 +2168,7 @@ $(function() {
|
|||
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
|
||||
}
|
||||
});
|
||||
""" % {'id': self.name})
|
||||
""" % {'id': self.name, 'data_type': data_type})
|
||||
|
||||
if not '[var_' in url:
|
||||
r += htmltext("""
|
||||
|
|
Loading…
Reference in New Issue