From 5f69f6f19cb6d1cc1f1148323989d0e46f2c210d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 19 Jun 2017 10:37:58 +0200 Subject: [PATCH] maps: add possibility to filter geojson data (#16977) --- combo/apps/maps/models.py | 14 ++++++++++++++ tests/test_maps_cells.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/combo/apps/maps/models.py b/combo/apps/maps/models.py index f142430..f8acda0 100644 --- a/combo/apps/maps/models.py +++ b/combo/apps/maps/models.py @@ -97,6 +97,20 @@ class MapLayer(models.Model): features = data['features'] else: features = data + + if request.GET.get('q'): + query = slugify(request.GET['q']) + + def match(feature): + for geo_property in feature['properties'].values(): + if not isinstance(geo_property, basestring): + continue + if query in slugify(geo_property): + return True + return False + + features = [x for x in features if match(x)] + for feature in features: feature['properties']['layer'] = { 'colour': self.marker_colour, diff --git a/tests/test_maps_cells.py b/tests/test_maps_cells.py index e9a1c1d..ea74217 100644 --- a/tests/test_maps_cells.py +++ b/tests/test_maps_cells.py @@ -22,7 +22,22 @@ SAMPLE_GEOJSON_CONTENT = '''{ "features": [ { "type": "Feature", - "properties": {}, + "properties": { + "name": "Foo" + }, + "geometry": { + "type": "Point", + "coordinates": [ + 2.548828125, + 48.83579746243093 + ] + } + }, + { + "type": "Feature", + "properties": { + "name": "Bar" + }, "geometry": { "type": "Point", "coordinates": [ @@ -147,6 +162,7 @@ def test_get_geojson(layer, user): json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT), status_code=200) resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id})) + assert len(json.loads(resp.content)['features']) == 2 assert requests_get.call_count == 1 resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id})) assert requests_get.call_count == 1 # cache was used @@ -190,3 +206,17 @@ def test_get_geojson(layer, user): resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id})) assert 'orig=combo' in requests_get.call_args[0][1] assert not 'email=admin%40localhost&' in requests_get.call_args[0][1] + + # check query on geojson + layer.geojson_url = 'http://example.org/geojson?t5' + layer.include_user_identifier = False + layer.save() + with mock.patch('combo.utils.RequestsSession.request') as requests_get: + requests_get.return_value = mock.Mock( + content=SAMPLE_GEOJSON_CONTENT, + json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT), + status_code=200) + resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?q=bar') + assert len(json.loads(resp.content)['features']) == 1 + assert 'orig=combo' in requests_get.call_args[0][1] + assert not 'email=admin%40localhost&' in requests_get.call_args[0][1] -- 2.11.0