From ce8f874b88942863be17b4ee02bb27632cc0e65e Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 25 May 2012 21:00:44 +0200 Subject: [PATCH] Fixed #14886 -- Added wms_options dict to GeoModelAdmin. It is now possible to set WMS options by overriding wms_options in a subclass of GeoModelAdmin. Thanks slinkp for the report and the initial patch. --- django/contrib/gis/admin/options.py | 8 ++++++++ .../gis/templates/gis/admin/openlayers.js | 2 +- django/contrib/gis/tests/geoadmin/tests.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/django/contrib/gis/admin/options.py b/django/contrib/gis/admin/options.py index f709056acf..6913bf5870 100644 --- a/django/contrib/gis/admin/options.py +++ b/django/contrib/gis/admin/options.py @@ -36,6 +36,7 @@ class GeoModelAdmin(ModelAdmin): wms_url = 'http://vmap0.tiles.osgeo.org/wms/vmap0' wms_layer = 'basic' wms_name = 'OpenLayers WMS' + wms_options = {'format': 'image/jpeg'} debug = False widget = OpenLayersWidget @@ -76,6 +77,12 @@ class GeoModelAdmin(ModelAdmin): class OLMap(self.widget): template = self.map_template geom_type = db_field.geom_type + + wms_options = '' + if self.wms_options: + wms_options = ["%s: '%s'" % pair for pair in self.wms_options.items()] + wms_options = ', %s' % ', '.join(wms_options) + params = {'default_lon' : self.default_lon, 'default_lat' : self.default_lat, 'default_zoom' : self.default_zoom, @@ -106,6 +113,7 @@ class GeoModelAdmin(ModelAdmin): 'wms_url' : self.wms_url, 'wms_layer' : self.wms_layer, 'wms_name' : self.wms_name, + 'wms_options' : wms_options, 'debug' : self.debug, } return OLMap diff --git a/django/contrib/gis/templates/gis/admin/openlayers.js b/django/contrib/gis/templates/gis/admin/openlayers.js index adca203969..f54b75e258 100644 --- a/django/contrib/gis/templates/gis/admin/openlayers.js +++ b/django/contrib/gis/templates/gis/admin/openlayers.js @@ -111,7 +111,7 @@ OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:3857", OpenLayers.Layer.Sp // The admin map for this geometry field. {{ module }}.map = new OpenLayers.Map('{{ id }}_map', options); // Base Layer - {{ module }}.layers.base = {% block base_layer %}new OpenLayers.Layer.WMS( "{{ wms_name }}", "{{ wms_url }}", {layers: '{{ wms_layer }}'} );{% endblock %} + {{ module }}.layers.base = {% block base_layer %}new OpenLayers.Layer.WMS("{{ wms_name }}", "{{ wms_url }}", {layers: '{{ wms_layer }}'{{ wms_options|safe }}});{% endblock %} {{ module }}.map.addLayer({{ module }}.layers.base); {% block extra_layers %}{% endblock %} {% if is_linestring %}OpenLayers.Feature.Vector.style["default"]["strokeWidth"] = 3; // Default too thin for linestrings. {% endif %} diff --git a/django/contrib/gis/tests/geoadmin/tests.py b/django/contrib/gis/tests/geoadmin/tests.py index aa07c2c500..1f104b22a1 100644 --- a/django/contrib/gis/tests/geoadmin/tests.py +++ b/django/contrib/gis/tests/geoadmin/tests.py @@ -2,6 +2,7 @@ from __future__ import absolute_import from django.test import TestCase from django.contrib.gis import admin +from django.contrib.gis.geos import Point from .models import City @@ -14,3 +15,21 @@ class GeoAdminTest(TestCase): admin_js = geoadmin.media.render_js() self.assertTrue(any([geoadmin.openlayers_url in js for js in admin_js])) + def test_olmap_OSM_rendering(self): + geoadmin = admin.site._registry[City] + result = geoadmin.get_map_widget(City._meta.get_field('point'))( + ).render('point', Point(-79.460734, 40.18476)) + self.assertIn( + """geodjango_point.layers.base = new OpenLayers.Layer.OSM("OpenStreetMap (Mapnik)");""", + result) + + def test_olmap_WMS_rendering(self): + admin.site.unregister(City) + admin.site.register(City, admin.GeoModelAdmin) + + geoadmin = admin.site._registry[City] + result = geoadmin.get_map_widget(City._meta.get_field('point'))( + ).render('point', Point(-79.460734, 40.18476)) + self.assertIn( + """geodjango_point.layers.base = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0", {layers: \'basic\', format: 'image/jpeg'});""", + result)