Projet

Général

Profil

0001-maps-give-common-layer-properties-to-map-cell-42765.patch

Frédéric Péters, 12 mai 2020 13:56

Télécharger (7,67 ko)

Voir les différences:

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(-)
combo/apps/maps/models.py
24 24
from django.utils.html import escape
25 25
from django.utils.text import slugify
26 26
from django.utils.translation import ugettext_lazy as _
27
from django.urls import reverse_lazy
27
from django.urls import reverse
28 28
from django import forms
29 29
from django.conf import settings
30 30

  
......
280 280

  
281 281
            features = [x for x in features if match(x)]
282 282

  
283
        for feature in features:
284
            feature['properties']['layer'] = {
285
                'colour': self.marker_colour,
286
                'icon_colour': self.icon_colour,
287
                'label': self.label,
288
                'icon': self.icon,
289
                'identifier': self.slug,
290
                'properties': properties,
291
            }
292 283
        return {'type': 'FeatureCollection', 'features': features}
293 284

  
294 285

  
......
381 372
    def get_geojson_layers(self):
382 373
        if not self.pk:
383 374
            return []
384
        return [{'url': reverse_lazy('mapcell-geojson', kwargs={'cell_id': self.pk, 'layer_slug': l.slug}),
385
                 'slug': l.slug} for l in self.layers.filter(kind='geojson')]
375
        return [{'url': reverse('mapcell-geojson', kwargs={'cell_id': self.pk, 'layer_slug': l.slug}),
376
                 'slug': l.slug,
377
                 'icon': l.icon,
378
                 'icon_colour': l.icon_colour,
379
                 'marker_colour': l.marker_colour,
380
                 'properties': [x.strip() for x in l.properties.split(',')],
381
                 } for l in self.layers.filter(kind='geojson')]
386 382

  
387 383
    def get_cell_extra_context(self, context):
388 384
        ctx = super(Map, self).get_cell_extra_context(context)
combo/apps/maps/static/js/combo.map.js
32 32
            function(data) {
33 33
              var geo_json = L.geoJson(data, {
34 34
                  onEachFeature: function(feature, layer) {
35
                      $(cell).trigger('combo:map-feature-prepare', {'feature': feature, 'layer': layer});
35
                      $(cell).trigger('combo:map-feature-prepare',
36
                              {'feature': feature, 'layer': layer, 'geojson_layer': geojson_layer});
36 37
                      var marker_behaviour_onclick = $map_widget.data('marker-behaviour-onclick');
37 38
                      if (marker_behaviour_onclick === 'display_data') {
38 39
                            var popup = '';
......
49 50
                                    popup += $popup_field.html();
50 51
                                });
51 52
                            } else {
52
                                var ordered_keys = feature.properties.layer.properties;
53
                                var ordered_keys = geojson_layer.properties;
53 54
                                if (! ordered_keys) {
54 55
                                    ordered_keys = Object.keys(properties).sort();
55 56
                                }
......
67 68
                      }
68 69
                  },
69 70
                  pointToLayer: function (feature, latlng) {
70
                      var markerStyles = "background-color: " + feature.properties.layer.colour + ";";
71
                      var markerStyles = "background-color: " + geojson_layer.marker_colour + ";";
71 72
                      marker = L.divIcon({
72 73
                                          iconAnchor: [0, 0],
73 74
                                          popupAnchor: [5, -45],
74 75
                                          html: '<span class="layer-' +
75
                                              feature.properties.layer.identifier +
76
                                              geojson_layer.slug +
76 77
                                              '" style="' + markerStyles + '"><i class="leaflet-marker-icon ' +
77
                                              feature.properties.layer.icon + '" style="color:' +
78
                                              feature.properties.layer.icon_colour +'"></i></span>'
78
                                              geojson_layer.icon + '" style="color:' +
79
                                              geojson_layer.icon_colour +'"></i></span>'
79 80
                                         });
80 81
                      return L.marker(latlng, {icon: marker});
81 82
                  }
combo/apps/maps/templates/maps/map_cell.html
32 32
  {% endfor %}
33 33
  var geojson_{{ cell.pk }} = Object();
34 34
  {% for layer in geojson_layers %}
35
  geojson_{{ cell.pk }}["{{ layer.slug }}"] = {
36
    url: '{{ layer.url }}',
37
    slug: '{{ layer.slug }}'
38
  };
35
    geojson_{{ cell.pk }}[{{ layer.slug|as_json|safe }}] = {{layer|as_json|safe}};
39 36
  {% endfor %}
40 37
</script>
41 38
</div>
tests/test_maps_cells.py
557 557
        features = json.loads(resp.text)['features']
558 558
        assert 'name' in features[0]['properties']
559 559
        assert 'extra' in features[0]['properties']
560
        assert features[0]['properties']['layer']['properties'] == []
561 560

  
562 561
    with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
563 562
        layer.geojson_url = 'http://example.org/geojson?t2'
......
571 570
        features = json.loads(resp.text)['features']
572 571
        assert 'name' in features[0]['properties']
573 572
        assert 'extra' not in features[0]['properties']
574
        assert features[0]['properties']['layer']['properties'] == ['name', 'hop']
575 573

  
576 574
    with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
577 575
        layer.geojson_url = 'http://example.org/geojson?t3'
......
584 582
        resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug}))
585 583
        features = json.loads(resp.text)['features']
586 584
        assert len(features[0]['properties']['display_fields']) == 2
587
        assert features[0]['properties']['layer']['properties'] == []
588 585

  
589 586
    with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
590 587
        layer.geojson_url = 'http://example.org/geojson?t4'
......
597 594
        resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug}))
598 595
        features = json.loads(resp.text)['features']
599 596
        assert len(features[0]['properties']['display_fields']) == 1
600
        assert features[0]['properties']['layer']['properties'] == ['id']
601 597

  
602 598

  
603 599
def test_duplicate(layer):
604
-