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.
This commit is contained in:
Claude Paroz 2012-05-25 21:00:44 +02:00
parent edfa95c22f
commit ce8f874b88
3 changed files with 28 additions and 1 deletions

View File

@ -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

View File

@ -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 %}

View File

@ -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)