Code

Ticket #14886: 14886-2.diff

File 14886-2.diff, 3.9 KB (added by claudep, 2 years ago)

Patch with tests

Line 
1diff --git a/django/contrib/gis/admin/options.py b/django/contrib/gis/admin/options.py
2index 4732e73..42e5f53 100644
3--- a/django/contrib/gis/admin/options.py
4+++ b/django/contrib/gis/admin/options.py
5@@ -36,6 +36,7 @@ class GeoModelAdmin(ModelAdmin):
6     wms_url = 'http://labs.metacarta.com/wms/vmap0'
7     wms_layer = 'basic'
8     wms_name = 'OpenLayers WMS'
9+    wms_options = {'format': 'image/jpeg'}
10     debug = False
11     widget = OpenLayersWidget
12 
13@@ -76,6 +77,12 @@ class GeoModelAdmin(ModelAdmin):
14         class OLMap(self.widget):
15             template = self.map_template
16             geom_type = db_field.geom_type
17+
18+            wms_options = ''
19+            if self.wms_options:
20+                wms_options = ["%s: '%s'" % pair for pair in self.wms_options.items()]
21+                wms_options = ', %s' % ', '.join(wms_options)
22+
23             params = {'default_lon' : self.default_lon,
24                       'default_lat' : self.default_lat,
25                       'default_zoom' : self.default_zoom,
26@@ -106,6 +113,7 @@ class GeoModelAdmin(ModelAdmin):
27                       'wms_url' : self.wms_url,
28                       'wms_layer' : self.wms_layer,
29                       'wms_name' : self.wms_name,
30+                      'wms_options' : wms_options,
31                       'debug' : self.debug,
32                       }
33         return OLMap
34diff --git a/django/contrib/gis/templates/gis/admin/openlayers.js b/django/contrib/gis/templates/gis/admin/openlayers.js
35index 4f34b6b..102adfb 100644
36--- a/django/contrib/gis/templates/gis/admin/openlayers.js
37+++ b/django/contrib/gis/templates/gis/admin/openlayers.js
38@@ -111,7 +111,7 @@ OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:3857", OpenLayers.Layer.Sp
39     // The admin map for this geometry field.
40     {{ module }}.map = new OpenLayers.Map('{{ id }}_map', options);
41     // Base Layer
42-    {{ module }}.layers.base = {% block base_layer %}new OpenLayers.Layer.WMS( "{{ wms_name }}", "{{ wms_url }}", {layers: '{{ wms_layer }}'} );{% endblock %}
43+    {{ module }}.layers.base = {% block base_layer %}new OpenLayers.Layer.WMS("{{ wms_name }}", "{{ wms_url }}", {layers: '{{ wms_layer }}'{{ wms_options|safe }}});{% endblock %}
44     {{ module }}.map.addLayer({{ module }}.layers.base);
45     {% block extra_layers %}{% endblock %}
46     {% if is_linestring %}OpenLayers.Feature.Vector.style["default"]["strokeWidth"] = 3; // Default too thin for linestrings. {% endif %}
47diff --git a/django/contrib/gis/tests/geoadmin/tests.py b/django/contrib/gis/tests/geoadmin/tests.py
48index aa07c2c..1f104b2 100644
49--- a/django/contrib/gis/tests/geoadmin/tests.py
50+++ b/django/contrib/gis/tests/geoadmin/tests.py
51@@ -2,6 +2,7 @@ from __future__ import absolute_import
52 
53 from django.test import TestCase
54 from django.contrib.gis import admin
55+from django.contrib.gis.geos import Point
56 
57 from .models import City
58 
59@@ -14,3 +15,21 @@ class GeoAdminTest(TestCase):
60         admin_js = geoadmin.media.render_js()
61         self.assertTrue(any([geoadmin.openlayers_url in js for js in admin_js]))
62 
63+    def test_olmap_OSM_rendering(self):
64+        geoadmin = admin.site._registry[City]
65+        result = geoadmin.get_map_widget(City._meta.get_field('point'))(
66+            ).render('point', Point(-79.460734, 40.18476))
67+        self.assertIn(
68+            """geodjango_point.layers.base = new OpenLayers.Layer.OSM("OpenStreetMap (Mapnik)");""",
69+            result)
70+
71+    def test_olmap_WMS_rendering(self):
72+        admin.site.unregister(City)
73+        admin.site.register(City, admin.GeoModelAdmin)
74+
75+        geoadmin = admin.site._registry[City]
76+        result = geoadmin.get_map_widget(City._meta.get_field('point'))(
77+            ).render('point', Point(-79.460734, 40.18476))
78+        self.assertIn(
79+            """geodjango_point.layers.base = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0", {layers: \'basic\', format: 'image/jpeg'});""",
80+            result)