From fe64dcf9464595a31a468a86b3fa6b0a72da1001 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 10 Nov 2021 09:44:26 +0100 Subject: [PATCH] maps: keep the marker's colour property (#58072) --- combo/apps/maps/models.py | 7 ++++++- tests/test_maps_cells.py | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/combo/apps/maps/models.py b/combo/apps/maps/models.py index dde62eca..045fccab 100644 --- a/combo/apps/maps/models.py +++ b/combo/apps/maps/models.py @@ -252,10 +252,10 @@ class MapLayer(models.Model): else: features = data - properties = [] if self.properties: properties = [x.strip() for x in self.properties.split(',')] for feature in features: + property_values = feature['properties'] if 'display_fields' in feature['properties']: # w.c.s. content, filter fields on varnames feature['properties']['display_fields'] = [ @@ -266,6 +266,11 @@ class MapLayer(models.Model): feature['properties'] = dict( [x for x in feature['properties'].items() if x[0] in properties] ) + # keep the property for marker's colour + if self.marker_colour and not self.marker_colour.startswith('#'): + first_dotted_name = self.marker_colour.split('.')[0] + if first_dotted_name in property_values: + feature['properties'][first_dotted_name] = property_values[first_dotted_name] if request and not self.geojson_accepts_circle_param and distance_params: geod = pyproj.Geod(ellps='WGS84') diff --git a/tests/test_maps_cells.py b/tests/test_maps_cells.py index fce9c674..ec77191d 100644 --- a/tests/test_maps_cells.py +++ b/tests/test_maps_cells.py @@ -22,7 +22,11 @@ SAMPLE_GEOJSON_CONTENT = '''{ "type": "Feature", "properties": { "name": "Foo", - "extra": "Baz" + "extra": "Baz", + "color": "#0a0a0a", + "subcolor": { + "color": "#0a0a0a" + } }, "geometry": { "type": "Point", @@ -105,9 +109,9 @@ def layer(): layer = MapLayer() layer.label = 'bicycles' layer.geojson_url = 'http://example.org/geojson' - layer.marker_colour = 'FF0000' + layer.marker_colour = '#FF0000' layer.icon = 'fa-bicycle' - layer.icon_colour = '0000FF' + layer.icon_colour = '#0000FF' layer.save() return layer @@ -606,6 +610,35 @@ def test_get_geojson_properties(app, layer, user): assert 'name' in features[0]['properties'] assert 'extra' not in features[0]['properties'] + with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get: + layer.geojson_url = 'http://example.org/geojson?t2' + layer.properties = 'name, hop' + layer.marker_colour = 'color' + layer.save() + requests_get.return_value = mock.Mock( + content=SAMPLE_GEOJSON_CONTENT, json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT), status_code=200 + ) + resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug})) + features = json.loads(resp.text)['features'] + assert 'name' in features[0]['properties'] + assert 'extra' not in features[0]['properties'] + assert 'color' in features[0]['properties'] + + with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get: + layer.geojson_url = 'http://example.org/geojson?t2' + layer.properties = 'name, hop' + layer.marker_colour = 'subcolor.color' + layer.save() + requests_get.return_value = mock.Mock( + content=SAMPLE_GEOJSON_CONTENT, json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT), status_code=200 + ) + resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug})) + features = json.loads(resp.text)['features'] + assert 'name' in features[0]['properties'] + assert 'extra' not in features[0]['properties'] + assert 'subcolor' in features[0]['properties'] + assert 'color' in features[0]['properties']['subcolor'] + with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get: layer.geojson_url = 'http://example.org/geojson?t3' layer.properties = '' -- 2.33.0