Projet

Général

Profil

0001-fields-autocomplete-for-string-field-with-json-datas.patch

Lauréline Guérin, 20 octobre 2020 10:53

Télécharger (4,14 ko)

Voir les différences:

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(-)
tests/test_form_pages.py
5201 5201
    assert 'http://example.net' in resp.text
5202 5202

  
5203 5203

  
5204
def test_form_string_field_autocomplete_named_datasource(pub):
5205
    FormDef.wipe()
5206
    formdef = FormDef()
5207
    formdef.name = 'test'
5208
    formdef.fields = [fields.StringField(id='0', label='string', type='string', required=False, data_source={'type': 'foobar'})]
5209
    formdef.store()
5210

  
5211
    # jsonp datasource
5212
    NamedDataSource.wipe()
5213
    data_source = NamedDataSource(name='foobar')
5214
    data_source.data_source = {'type': 'jsonp', 'value': 'http://remote.example.net/json'}
5215
    data_source.store()
5216

  
5217
    resp = get_app(pub).get('/test/')
5218
    assert ').autocomplete({' in resp.text
5219
    assert "options.url = 'http://remote.example.net/json'" in resp.text
5220
    assert "options.url = '/api/autocomplete/" not in resp.text
5221
    assert 'dataType: "jsonp",' in resp.text
5222

  
5223
    # json datasource
5224
    data_source.data_source['type'] = 'json'
5225
    data_source.query_parameter = 'q'
5226
    data_source.store()
5227

  
5228
    resp = get_app(pub).get('/test/')
5229
    assert ').autocomplete({' in resp.text
5230
    assert "options.url = 'http://remote.example.net/json'" not in resp.text
5231
    assert "options.url = '/api/autocomplete/" in resp.text
5232
    assert 'dataType: "json",' in resp.text
5233

  
5234
    # card datasource
5235
    CardDef.wipe()
5236
    carddef = CardDef()
5237
    carddef.name = 'Foo'
5238
    carddef.fields = []
5239
    carddef.store()
5240

  
5241
    data_source.data_source['type'] = 'carddef:foo'
5242
    data_source.store()
5243
    resp = get_app(pub).get('/test/')
5244
    assert ').autocomplete({' in resp.text
5245
    assert "options.url = 'http://remote.example.net/json'" not in resp.text
5246
    assert "options.url = '/api/autocomplete/" in resp.text
5247
    assert 'dataType: "json",' in resp.text
5248

  
5249

  
5204 5250
def test_form_workflow_trigger(pub):
5205 5251
    user = create_user(pub)
5206 5252

  
wcs/fields.py
805 805

  
806 806
    def perform_more_widget_changes(self, form, kwargs, edit=True):
807 807
        if self.data_source:
808
            real_data_source = data_sources.get_real(self.data_source)
809
            if real_data_source.get('type') == 'jsonp':
810
                kwargs['url'] = real_data_source.get('value')
808
            data_source = data_sources.get_object(self.data_source)
809
            if data_source.can_jsonp():
810
                kwargs['url'] = data_source.get_jsonp_url()
811 811
                self.widget_class = AutocompleteStringWidget
812 812

  
813 813
    def fill_admin_form(self, form):
wcs/qommon/form.py
2134 2134
            # there's no autocomplete URL, get out now.
2135 2135
            return r.getvalue()
2136 2136

  
2137
        data_type = 'json' if url.startswith('/api/autocomplete/') else 'jsonp'
2137 2138
        r += htmltext("""
2138 2139
<script id="script_%(id)s">
2139 2140
$(function() {
......
2141 2142
    source: function( request, response ) {
2142 2143
      $.ajax({
2143 2144
        url: $("#form_%(id)s").data('uiAutocomplete').options.url,
2144
        dataType: "jsonp",
2145
        dataType: "%(data_type)s",
2145 2146
        data: {
2146 2147
          q: request.term
2147 2148
        },
......
2160 2161
      $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
2161 2162
    }
2162 2163
  });
2163
""" % {'id': self.name})
2164
""" % {'id': self.name, 'data_type': data_type})
2164 2165

  
2165 2166
        if not '[var_' in url:
2166 2167
            r += htmltext("""
2167
-