From ac4ff6295289455e2cc3648db9f01c83ec505708 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 10 Feb 2020 16:42:08 +0100
Subject: [PATCH 4/5] maps: default tiles layer (#22639)

---
 combo/apps/maps/models.py | 13 +++++++++++--
 tests/test_maps_cells.py  | 35 +++++++++++++++++++++++++++++++----
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/combo/apps/maps/models.py b/combo/apps/maps/models.py
index d5f1569..782dd3e 100644
--- a/combo/apps/maps/models.py
+++ b/combo/apps/maps/models.py
@@ -153,6 +153,10 @@ class MapLayer(models.Model):
     def natural_key(self):
         return (self.slug, )
 
+    @classmethod
+    def get_default_tiles_layer(cls):
+        return cls.objects.filter(kind='tiles', tiles_default=True).first()
+
     @classmethod
     def export_all_for_json(cls):
         return [x.get_as_serialized_object() for x in MapLayer.objects.all()]
@@ -340,8 +344,13 @@ class Map(CellBase):
         ctx['min_zoom'] = self.min_zoom
         ctx['max_zoom'] = self.max_zoom
         ctx['geojson_url'] = reverse_lazy('mapcell-geojson', kwargs={'cell_id': self.pk})
-        ctx['tile_urltemplate'] = settings.COMBO_MAP_TILE_URLTEMPLATE
-        ctx['map_attribution'] = settings.COMBO_MAP_ATTRIBUTION
+        default_tiles_layer = MapLayer.get_default_tiles_layer()
+        if default_tiles_layer is not None:
+            ctx['tile_urltemplate'] = default_tiles_layer.tiles_template_url
+            ctx['map_attribution'] = default_tiles_layer.tiles_attribution
+        else:
+            ctx['tile_urltemplate'] = settings.COMBO_MAP_TILE_URLTEMPLATE
+            ctx['map_attribution'] = settings.COMBO_MAP_ATTRIBUTION
         ctx['max_bounds'] = settings.COMBO_MAP_MAX_BOUNDS
         ctx['group_markers'] = self.group_markers
         ctx['marker_behaviour_onclick'] = self.marker_behaviour_onclick
diff --git a/tests/test_maps_cells.py b/tests/test_maps_cells.py
index bee63f9..6032d80 100644
--- a/tests/test_maps_cells.py
+++ b/tests/test_maps_cells.py
@@ -3,10 +3,12 @@ import json
 import mock
 import pytest
 
+from django.conf import settings
 from django.contrib.auth.models import User
-from django.test.client import RequestFactory
-from django.core.urlresolvers import reverse
 from django.contrib.auth.models import Group
+from django.core.urlresolvers import reverse
+from django.test.client import RequestFactory
+from django.utils.html import escape
 
 from combo.data.models import Page
 from combo.apps.maps.models import MapLayer, Map, MapLayerOptions
@@ -88,7 +90,6 @@ SAMPLE_WCS_GEOJSON_CONTENT = '''{
 }'''
 
 
-
 @pytest.fixture
 def user():
     try:
@@ -97,6 +98,7 @@ def user():
         user = User.objects.create_user('admin', email='admin@localhost', password='admin')
     return user
 
+
 @pytest.fixture
 def layer():
     try:
@@ -111,6 +113,18 @@ def layer():
         layer.save()
     return layer
 
+
+@pytest.fixture
+def tiles_layer():
+    return MapLayer.objects.create(
+        label='Test2',
+        kind='tiles',
+        tiles_template_url='http://somedomain.com/blabla/{z}/{x}/{y}{r}.png',
+        tiles_attribution='Foo bar',
+        tiles_default=True,
+    )
+
+
 def test_cell_disabled():
     MapLayer.objects.all().delete()
     assert Map.is_enabled() is False
@@ -118,7 +132,7 @@ def test_cell_disabled():
 def test_cell_enabled(layer):
     assert Map.is_enabled() is True
 
-def test_cell_rendering(app, layer):
+def test_cell_rendering(app, layer, tiles_layer):
     page = Page(title='xxx', slug='test_map_cell', template_name='standard')
     page.save()
     cell = Map(page=page, placeholder='content', order=0, title='Map with points')
@@ -133,6 +147,8 @@ def test_cell_rendering(app, layer):
     assert 'data-init-lng="2.3233688436448574"' in rendered
     assert 'data-geojson-url="/ajax/mapcell/geojson/1/"' in rendered
     assert 'data-group-markers="1"' not in rendered
+    assert 'data-tile-urltemplate="%s"' % tiles_layer.tiles_template_url in rendered
+    assert 'data-map-attribution="%s"' % tiles_layer.tiles_attribution in rendered
     resp = app.get('/test_map_cell/')
     assert 'xstatic/leaflet.js' in resp.text
     assert 'js/combo.map.js' in resp.text
@@ -144,6 +160,17 @@ def test_cell_rendering(app, layer):
     rendered = cell.render(context)
     assert 'data-group-markers="1"' in rendered
 
+    tiles_layer.tiles_default = False
+    tiles_layer.save()
+    rendered = cell.render(context)
+    assert 'data-tile-urltemplate="%s"' % settings.COMBO_MAP_TILE_URLTEMPLATE in rendered
+    assert 'data-map-attribution="%s"' % escape(settings.COMBO_MAP_ATTRIBUTION) in rendered
+
+    tiles_layer.delete()
+    rendered = cell.render(context)
+    assert 'data-tile-urltemplate="%s"' % settings.COMBO_MAP_TILE_URLTEMPLATE in rendered
+    assert 'data-map-attribution="%s"' % escape(settings.COMBO_MAP_ATTRIBUTION) in rendered
+
 
 def test_get_geojson_on_non_public_page(app, layer):
     page = Page(title='xxx', slug='new', template_name='standard',
-- 
2.20.1

