From 34b885003805fde94cc19686f6a9e8ee3206fc60 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 12 May 2020 13:37:41 +0200
Subject: [PATCH 1/2] maps: give common layer properties to map cell (#42765)

---
 combo/apps/maps/models.py                    | 20 ++++++++------------
 combo/apps/maps/static/js/combo.map.js       | 13 +++++++------
 combo/apps/maps/templates/maps/map_cell.html |  5 +----
 tests/test_maps_cells.py                     |  4 ----
 4 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/combo/apps/maps/models.py b/combo/apps/maps/models.py
index f457a95c..d19db35b 100644
--- a/combo/apps/maps/models.py
+++ b/combo/apps/maps/models.py
@@ -24,7 +24,7 @@ from django.utils.encoding import python_2_unicode_compatible
 from django.utils.html import escape
 from django.utils.text import slugify
 from django.utils.translation import ugettext_lazy as _
-from django.urls import reverse_lazy
+from django.urls import reverse
 from django import forms
 from django.conf import settings
 
@@ -280,15 +280,6 @@ class MapLayer(models.Model):
 
             features = [x for x in features if match(x)]
 
-        for feature in features:
-            feature['properties']['layer'] = {
-                'colour': self.marker_colour,
-                'icon_colour': self.icon_colour,
-                'label': self.label,
-                'icon': self.icon,
-                'identifier': self.slug,
-                'properties': properties,
-            }
         return {'type': 'FeatureCollection', 'features': features}
 
 
@@ -381,8 +372,13 @@ class Map(CellBase):
     def get_geojson_layers(self):
         if not self.pk:
             return []
-        return [{'url': reverse_lazy('mapcell-geojson', kwargs={'cell_id': self.pk, 'layer_slug': l.slug}),
-                 'slug': l.slug} for l in self.layers.filter(kind='geojson')]
+        return [{'url': reverse('mapcell-geojson', kwargs={'cell_id': self.pk, 'layer_slug': l.slug}),
+                 'slug': l.slug,
+                 'icon': l.icon,
+                 'icon_colour': l.icon_colour,
+                 'marker_colour': l.marker_colour,
+                 'properties': [x.strip() for x in l.properties.split(',')],
+                 } for l in self.layers.filter(kind='geojson')]
 
     def get_cell_extra_context(self, context):
         ctx = super(Map, self).get_cell_extra_context(context)
diff --git a/combo/apps/maps/static/js/combo.map.js b/combo/apps/maps/static/js/combo.map.js
index b862ebf5..3738e298 100644
--- a/combo/apps/maps/static/js/combo.map.js
+++ b/combo/apps/maps/static/js/combo.map.js
@@ -32,7 +32,8 @@ $(function() {
             function(data) {
               var geo_json = L.geoJson(data, {
                   onEachFeature: function(feature, layer) {
-                      $(cell).trigger('combo:map-feature-prepare', {'feature': feature, 'layer': layer});
+                      $(cell).trigger('combo:map-feature-prepare',
+                              {'feature': feature, 'layer': layer, 'geojson_layer': geojson_layer});
                       var marker_behaviour_onclick = $map_widget.data('marker-behaviour-onclick');
                       if (marker_behaviour_onclick === 'display_data') {
                             var popup = '';
@@ -49,7 +50,7 @@ $(function() {
                                     popup += $popup_field.html();
                                 });
                             } else {
-                                var ordered_keys = feature.properties.layer.properties;
+                                var ordered_keys = geojson_layer.properties;
                                 if (! ordered_keys) {
                                     ordered_keys = Object.keys(properties).sort();
                                 }
@@ -67,15 +68,15 @@ $(function() {
                       }
                   },
                   pointToLayer: function (feature, latlng) {
-                      var markerStyles = "background-color: " + feature.properties.layer.colour + ";";
+                      var markerStyles = "background-color: " + geojson_layer.marker_colour + ";";
                       marker = L.divIcon({
                                           iconAnchor: [0, 0],
                                           popupAnchor: [5, -45],
                                           html: '<span class="layer-' +
-                                              feature.properties.layer.identifier +
+                                              geojson_layer.slug +
                                               '" style="' + markerStyles + '"><i class="leaflet-marker-icon ' +
-                                              feature.properties.layer.icon + '" style="color:' +
-                                              feature.properties.layer.icon_colour +'"></i></span>'
+                                              geojson_layer.icon + '" style="color:' +
+                                              geojson_layer.icon_colour +'"></i></span>'
                                          });
                       return L.marker(latlng, {icon: marker});
                   }
diff --git a/combo/apps/maps/templates/maps/map_cell.html b/combo/apps/maps/templates/maps/map_cell.html
index ba2288fb..959e1c60 100644
--- a/combo/apps/maps/templates/maps/map_cell.html
+++ b/combo/apps/maps/templates/maps/map_cell.html
@@ -32,10 +32,7 @@
   {% endfor %}
   var geojson_{{ cell.pk }} = Object();
   {% for layer in geojson_layers %}
-  geojson_{{ cell.pk }}["{{ layer.slug }}"] = {
-    url: '{{ layer.url }}',
-    slug: '{{ layer.slug }}'
-  };
+    geojson_{{ cell.pk }}[{{ layer.slug|as_json|safe }}] = {{layer|as_json|safe}};
   {% endfor %}
 </script>
 </div>
diff --git a/tests/test_maps_cells.py b/tests/test_maps_cells.py
index a486ceb0..fd334bd1 100644
--- a/tests/test_maps_cells.py
+++ b/tests/test_maps_cells.py
@@ -557,7 +557,6 @@ def test_get_geojson_properties(app, layer, user):
         features = json.loads(resp.text)['features']
         assert 'name' in features[0]['properties']
         assert 'extra' in features[0]['properties']
-        assert features[0]['properties']['layer']['properties'] == []
 
     with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
         layer.geojson_url = 'http://example.org/geojson?t2'
@@ -571,7 +570,6 @@ def test_get_geojson_properties(app, layer, user):
         features = json.loads(resp.text)['features']
         assert 'name' in features[0]['properties']
         assert 'extra' not in features[0]['properties']
-        assert features[0]['properties']['layer']['properties'] == ['name', 'hop']
 
     with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
         layer.geojson_url = 'http://example.org/geojson?t3'
@@ -584,7 +582,6 @@ def test_get_geojson_properties(app, layer, user):
         resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug}))
         features = json.loads(resp.text)['features']
         assert len(features[0]['properties']['display_fields']) == 2
-        assert features[0]['properties']['layer']['properties'] == []
 
     with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
         layer.geojson_url = 'http://example.org/geojson?t4'
@@ -597,7 +594,6 @@ def test_get_geojson_properties(app, layer, user):
         resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug}))
         features = json.loads(resp.text)['features']
         assert len(features[0]['properties']['display_fields']) == 1
-        assert features[0]['properties']['layer']['properties'] == ['id']
 
 
 def test_duplicate(layer):
-- 
2.26.2

