Projet

Général

Profil

0001-datasource-data-attribute-can-be-data-foo-bar-result.patch

Lauréline Guérin, 11 mai 2021 10:25

Télécharger (3,26 ko)

Voir les différences:

Subject: [PATCH] datasource: data attribute can be "data/foo/bar/results"
 (#53911)

 tests/test_datasource.py | 12 ++++++++++++
 wcs/data_sources.py      | 16 ++++++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)
tests/test_datasource.py
338 338
    get_request().datasources_cache = {}
339 339
    assert data_sources.get_structured_items(datasource) == []
340 340

  
341
    get_request().datasources_cache = {}
342
    with open(json_file_path, 'w') as json_file:
343
        json.dump({'data': {'results': [{'id': '1', 'text': 'foo'}, {'id': '2', 'text': 'bar'}]}}, json_file)
344
    assert data_sources.get_structured_items(datasource) == []
345

  
346
    datasource = {'type': 'json', 'value': ' {{ json_url }}', 'data_attribute': 'data/results'}
347
    get_request().datasources_cache = {}
348
    assert data_sources.get_structured_items(datasource) == [
349
        {'id': '1', 'text': 'foo'},
350
        {'id': '2', 'text': 'bar'},
351
    ]
352

  
341 353
    # specify id_attribute
342 354
    datasource = {'type': 'json', 'value': ' {{ json_url }}', 'id_attribute': 'pk'}
343 355
    get_request().datasources_cache = {}
wcs/data_sources.py
171 171
    geojson = data_source.get('type') == 'geojson'
172 172
    error_summary = None
173 173
    exc = None
174

  
174 175
    try:
175 176
        entries = misc.json_loads(misc.urlopen(url).read())
176 177
        if not isinstance(entries, dict):
......
181 182
            if not isinstance(entries.get('features'), list):
182 183
                raise ValueError('bad geojson format')
183 184
        else:
184
            if not isinstance(entries.get(data_key), list):
185
            # data_key can be "data/foo/bar/results"
186
            keys = data_key.split('/')
187
            data = entries
188
            for key in keys[:-1]:
189
                if not isinstance(data.get(key), dict):
190
                    raise ValueError('not a json dict with a %s list attribute' % data_key)
191
                data = data[key]
192
            if not isinstance(data.get(keys[-1]), list):
185 193
                raise ValueError('not a json dict with a %s list attribute' % data_key)
186 194
        return entries
187 195
    except misc.ConnectionError as e:
......
213 221
    data_key = data_source.get('data_attribute') or 'data'
214 222
    id_attribute = data_source.get('id_attribute') or 'id'
215 223
    text_attribute = data_source.get('text_attribute') or 'text'
224
    # data_key can be "data/foo/bar/results"
225
    keys = data_key.split('/')
226
    for key in keys:
227
        entries = entries[key]
216 228
    items = []
217
    for item in entries.get(data_key):
229
    for item in entries:
218 230
        # skip malformed items
219 231
        if not isinstance(item, dict):
220 232
            continue
221
-