Browse Source

Fixed #33924 -- Deprecated BaseGeometryWidget.map_height/map_width attributes.

Claude Paroz 2 years ago
parent
commit
4fcba800b8

+ 6 - 0
django/contrib/admin/static/admin/css/widgets.css

@@ -578,3 +578,9 @@ select + .related-widget-wrapper-link,
 .related-widget-wrapper-link + .related-widget-wrapper-link {
     margin-left: 7px;
 }
+
+/* GIS MAPS */
+.dj_map {
+    width: 600px;
+    height: 400px;
+}

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

@@ -1,4 +1,5 @@
 import logging
+import warnings
 
 from django.conf import settings
 from django.contrib.gis import gdal
@@ -6,6 +7,7 @@ from django.contrib.gis.geometry import json_regex
 from django.contrib.gis.geos import GEOSException, GEOSGeometry
 from django.forms.widgets import Widget
 from django.utils import translation
+from django.utils.deprecation import RemovedInDjango51Warning
 
 logger = logging.getLogger("django.contrib.gis")
 
@@ -18,8 +20,8 @@ class BaseGeometryWidget(Widget):
 
     geom_type = "GEOMETRY"
     map_srid = 4326
-    map_width = 600
-    map_height = 400
+    map_width = 600  # RemovedInDjango51Warning
+    map_height = 400  # RemovedInDjango51Warning
     display_raw = False
 
     supports_3d = False
@@ -29,6 +31,17 @@ class BaseGeometryWidget(Widget):
         self.attrs = {}
         for key in ("geom_type", "map_srid", "map_width", "map_height", "display_raw"):
             self.attrs[key] = getattr(self, key)
+        if (
+            (attrs and ("map_width" in attrs or "map_height" in attrs))
+            or self.map_width != 600
+            or self.map_height != 400
+        ):
+            warnings.warn(
+                "The map_height and map_width widget attributes are deprecated. Please "
+                "use CSS to size map widgets.",
+                category=RemovedInDjango51Warning,
+                stacklevel=2,
+            )
         if attrs:
             self.attrs.update(attrs)
 

+ 2 - 0
django/contrib/gis/static/gis/js/OLMapWidget.js

@@ -61,6 +61,8 @@ class MapWidget {
             this.options.base_layer = new ol.layer.Tile({source: new ol.source.OSM()});
         }
 
+        // RemovedInDjango51Warning: when the deprecation ends, remove setting
+        // width/height (3 lines below).
         const mapContainer = document.getElementById(this.options.map_id);
         mapContainer.style.width = `${mapContainer.dataset.width}px`;
         mapContainer.style.height = `${mapContainer.dataset.height}px`;

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

@@ -1,6 +1,7 @@
 {% load i18n l10n %}
 
 <div id="{{ id }}_div_map" class="dj_map_wrapper">
+    {# RemovedInDjango51Warning: when the deprecation ends, remove data-width and data-height attributes. #}
     <div id="{{ id }}_map" class="dj_map" data-width="{{ map_width }}" data-height="{{ map_height }}"></div>
     {% if not disabled %}<span class="clear_features"><a href="">{% translate "Delete all Features" %}</a></span>{% endif %}
     {% if display_raw %}<p>{% translate "Debugging window (serialized value)" %}</p>{% endif %}

+ 3 - 0
docs/internals/deprecation.txt

@@ -32,6 +32,9 @@ details on these changes.
 
 * The ``django.contrib.postgres.fields.CIText`` mixin will be removed.
 
+* The ``map_width`` and ``map_height`` attributes of ``BaseGeometryWidget``
+  will be removed.
+
 .. _deprecation-removed-in-5.0:
 
 5.0

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

@@ -106,6 +106,11 @@ from other Django widget attributes.
 
     Height and width of the widget map (default is 400x600).
 
+    .. deprecated:: 4.2
+
+        ``map_height`` and ``map_width`` attributes are deprecated, use CSS to
+        size map widgets instead.
+
 .. attribute:: BaseGeometryWidget.map_srid
 
     SRID code used by the map (default is 4326).
@@ -131,7 +136,7 @@ widget. For example::
 
     class MyGeoForm(forms.Form):
         point = forms.PointField(widget=
-            forms.OSMWidget(attrs={'map_width': 800, 'map_height': 500}))
+            forms.OSMWidget(attrs={'display_raw': True}))
 
 Widget classes
 --------------

+ 3 - 0
docs/releases/4.2.txt

@@ -369,3 +369,6 @@ Miscellaneous
   collation.
 
 * ``django.contrib.postgres.fields.CIText`` mixin is deprecated.
+
+* The ``map_height`` and ``map_width`` attributes of ``BaseGeometryWidget`` are
+  deprecated, use CSS to size map widgets instead.

+ 17 - 0
tests/gis_tests/test_geoforms.py

@@ -5,6 +5,7 @@ from django.contrib.gis.forms import BaseGeometryWidget, OpenLayersWidget
 from django.contrib.gis.geos import GEOSGeometry
 from django.core.exceptions import ValidationError
 from django.test import SimpleTestCase, override_settings
+from django.utils.deprecation import RemovedInDjango51Warning
 from django.utils.html import escape
 
 
@@ -485,3 +486,19 @@ class GeometryWidgetTests(SimpleTestCase):
         form = PointForm(data={"p": point.json})
         self.assertTrue(form.is_valid())
         self.assertEqual(form.cleaned_data["p"].srid, 4326)
+
+    def test_deprecated_width_and_height(self):
+        class CustomGeometryWidget(forms.BaseGeometryWidget):
+            map_height = 300
+            map_width = 550
+
+        msg = (
+            "The map_height and map_width widget attributes are deprecated. Please use "
+            "CSS to size map widgets."
+        )
+        with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
+            CustomGeometryWidget()
+        with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
+            forms.BaseGeometryWidget({"map_width": 400})
+        with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
+            forms.BaseGeometryWidget({"map_height": 600})