data sources: allow identifier directly inside geojson feature (#48377)

This commit is contained in:
Frédéric Péters 2020-11-07 23:42:15 +01:00 committed by Thomas NOËL
parent 4a24904f8f
commit c57507d9a4
2 changed files with 23 additions and 2 deletions

View File

@ -532,10 +532,22 @@ def test_geojson_datasource(requests_pub, http_requests):
{'id': '1', 'text': 'foo', 'properties': {'gid': '1', 'text': 'foo'}},
{'id': '2', 'text': 'bar', 'properties': {'gid': '2', 'text': 'bar'}}]
# check with missing id property
datasource = {'type': 'geojson', 'value': ' {{ geojson_url }}', 'id_property': 'id'}
get_request().datasources_cache = {}
assert data_sources.get_structured_items(datasource) == []
# check with feature IDs
get_request().datasources_cache = {}
geojson_file = open(geojson_file_path, 'w')
json.dump({'features': [
{'id': '1', 'properties': {'text': 'foo'}},
{'id': '2', 'properties': {'text': 'bar'}}]}, geojson_file)
geojson_file.close()
assert data_sources.get_structured_items(datasource) == [
{'id': '1', 'text': 'foo', 'properties': {'text': 'foo'}},
{'id': '2', 'text': 'bar', 'properties': {'text': 'bar'}}]
# specify label_template_property
datasource = {'type': 'geojson', 'value': ' {{ geojson_url }}', 'label_template_property': '{{ id }}: {{ text }}'}
get_request().datasources_cache = {}

View File

@ -170,9 +170,18 @@ def request_geojson_items(url, data_source):
items = []
id_property = data_source.get('id_property') or 'id'
for item in entries.get('features'):
if not item.get('properties', {}).get(id_property):
if id_property == 'id' and 'id' in item:
# If a Feature has a commonly used identifier, that identifier
# SHOULD be included as a member of the Feature object with the
# name "id", and the value of this member is either a JSON string
# or number.
# -- https://tools.ietf.org/html/rfc7946#section-3.2
pass
elif item.get('properties', {}).get(id_property):
item['id'] = item['properties'][id_property]
else:
# missing id property, skip entry
continue
item['id'] = item['properties'][id_property]
try:
item['text'] = Template(
data_source.get('label_template_property') or '{{ text }}').render(item['properties'])