From 4fcba800b84fc5fb53cdc70e4bea37ec958516fd Mon Sep 17 00:00:00 2001
From: Claude Paroz <claude@2xlibre.net>
Date: Fri, 12 Aug 2022 12:18:51 +0200
Subject: [PATCH] Fixed #33924 -- Deprecated
 BaseGeometryWidget.map_height/map_width attributes.

---
 .../contrib/admin/static/admin/css/widgets.css  |  6 ++++++
 django/contrib/gis/forms/widgets.py             | 17 +++++++++++++++--
 django/contrib/gis/static/gis/js/OLMapWidget.js |  2 ++
 .../contrib/gis/templates/gis/openlayers.html   |  1 +
 docs/internals/deprecation.txt                  |  3 +++
 docs/ref/contrib/gis/forms-api.txt              |  7 ++++++-
 docs/releases/4.2.txt                           |  3 +++
 tests/gis_tests/test_geoforms.py                | 17 +++++++++++++++++
 8 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/django/contrib/admin/static/admin/css/widgets.css b/django/contrib/admin/static/admin/css/widgets.css
index cd1d6b4145..0e14efe50d 100644
--- a/django/contrib/admin/static/admin/css/widgets.css
+++ b/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;
+}
diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py
index 0f53ee2e96..5f169e9cb3 100644
--- a/django/contrib/gis/forms/widgets.py
+++ b/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)
 
diff --git a/django/contrib/gis/static/gis/js/OLMapWidget.js b/django/contrib/gis/static/gis/js/OLMapWidget.js
index 839c8cd7cc..fe944a4a87 100644
--- a/django/contrib/gis/static/gis/js/OLMapWidget.js
+++ b/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`;
diff --git a/django/contrib/gis/templates/gis/openlayers.html b/django/contrib/gis/templates/gis/openlayers.html
index bde2650ca2..2849813c6e 100644
--- a/django/contrib/gis/templates/gis/openlayers.html
+++ b/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 %}
diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt
index ff10c9de5a..a5b76c13ab 100644
--- a/docs/internals/deprecation.txt
+++ b/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
diff --git a/docs/ref/contrib/gis/forms-api.txt b/docs/ref/contrib/gis/forms-api.txt
index 15369ed0e2..0d7651f20e 100644
--- a/docs/ref/contrib/gis/forms-api.txt
+++ b/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
 --------------
diff --git a/docs/releases/4.2.txt b/docs/releases/4.2.txt
index 6760848976..cb64750009 100644
--- a/docs/releases/4.2.txt
+++ b/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.
diff --git a/tests/gis_tests/test_geoforms.py b/tests/gis_tests/test_geoforms.py
index d1bc43b9da..b8105645bf 100644
--- a/tests/gis_tests/test_geoforms.py
+++ b/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})