Browse Source

Fixed #28195 -- Added OSMWidget.default_zoom attribute.

Danilo Bargen 7 years ago
parent
commit
a7975260b5

+ 2 - 1
django/contrib/gis/forms/widgets.py

@@ -103,10 +103,11 @@ class OSMWidget(OpenLayersWidget):
     template_name = 'gis/openlayers-osm.html'
     default_lon = 5
     default_lat = 47
+    default_zoom = 12
 
     def __init__(self, attrs=None):
         super().__init__()
-        for key in ('default_lon', 'default_lat'):
+        for key in ('default_lon', 'default_lat', 'default_zoom'):
             self.attrs[key] = getattr(self, key)
         if attrs:
             self.attrs.update(attrs)

+ 1 - 0
django/contrib/gis/templates/gis/openlayers-osm.html

@@ -4,6 +4,7 @@
 {% block options %}{{ block.super }}
 options['default_lon'] = {{ default_lon|unlocalize }};
 options['default_lat'] = {{ default_lat|unlocalize }};
+options['default_zoom'] = {{ default_zoom|unlocalize }};
 {% endblock %}
 
 {% block base_layer %}

+ 6 - 0
docs/ref/contrib/gis/forms-api.txt

@@ -185,6 +185,12 @@ Widget classes
         The default center latitude and longitude are ``47`` and ``5``,
         respectively, which is a location in eastern France.
 
+    .. attribute:: default_zoom
+
+        .. versionadded:: 2.0
+
+        The default map zoom is ``12``.
+
     The :class:`OpenLayersWidget` note about JavaScript file hosting above also
     applies here. See also this `FAQ answer`_ about ``https`` access to map
     tiles.

+ 3 - 0
docs/releases/2.0.txt

@@ -75,6 +75,9 @@ Minor features
 * Any :class:`~django.contrib.gis.geos.GEOSGeometry` imported from GeoJSON now
   has its SRID set.
 
+* Added the :attr:`.OSMWidget.default_zoom` attribute to customize the map's
+  default zoom level.
+
 :mod:`django.contrib.messages`
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

+ 5 - 1
tests/gis_tests/test_geoforms.py

@@ -328,11 +328,14 @@ class OSMWidgetTest(SimpleTestCase):
     def test_default_lat_lon(self):
         self.assertEqual(forms.OSMWidget.default_lon, 5)
         self.assertEqual(forms.OSMWidget.default_lat, 47)
+        self.assertEqual(forms.OSMWidget.default_zoom, 12)
 
         class PointForm(forms.Form):
             p = forms.PointField(
                 widget=forms.OSMWidget(attrs={
-                    'default_lon': 20, 'default_lat': 30
+                    'default_lon': 20,
+                    'default_lat': 30,
+                    'default_zoom': 17,
                 }),
             )
 
@@ -341,6 +344,7 @@ class OSMWidgetTest(SimpleTestCase):
 
         self.assertIn("options['default_lon'] = 20;", rendered)
         self.assertIn("options['default_lat'] = 30;", rendered)
+        self.assertIn("options['default_zoom'] = 17;", rendered)
 
 
 class GeometryWidgetTests(SimpleTestCase):