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

This commit is contained in:
Claude Paroz 2022-08-12 12:18:51 +02:00 committed by Mariusz Felisiak
parent 8c3046daad
commit 4fcba800b8
8 changed files with 53 additions and 3 deletions

View File

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

View File

@ -1,4 +1,5 @@
import logging import logging
import warnings
from django.conf import settings from django.conf import settings
from django.contrib.gis import gdal 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.contrib.gis.geos import GEOSException, GEOSGeometry
from django.forms.widgets import Widget from django.forms.widgets import Widget
from django.utils import translation from django.utils import translation
from django.utils.deprecation import RemovedInDjango51Warning
logger = logging.getLogger("django.contrib.gis") logger = logging.getLogger("django.contrib.gis")
@ -18,8 +20,8 @@ class BaseGeometryWidget(Widget):
geom_type = "GEOMETRY" geom_type = "GEOMETRY"
map_srid = 4326 map_srid = 4326
map_width = 600 map_width = 600 # RemovedInDjango51Warning
map_height = 400 map_height = 400 # RemovedInDjango51Warning
display_raw = False display_raw = False
supports_3d = False supports_3d = False
@ -29,6 +31,17 @@ class BaseGeometryWidget(Widget):
self.attrs = {} self.attrs = {}
for key in ("geom_type", "map_srid", "map_width", "map_height", "display_raw"): for key in ("geom_type", "map_srid", "map_width", "map_height", "display_raw"):
self.attrs[key] = getattr(self, key) 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: if attrs:
self.attrs.update(attrs) self.attrs.update(attrs)

View File

@ -61,6 +61,8 @@ class MapWidget {
this.options.base_layer = new ol.layer.Tile({source: new ol.source.OSM()}); 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); const mapContainer = document.getElementById(this.options.map_id);
mapContainer.style.width = `${mapContainer.dataset.width}px`; mapContainer.style.width = `${mapContainer.dataset.width}px`;
mapContainer.style.height = `${mapContainer.dataset.height}px`; mapContainer.style.height = `${mapContainer.dataset.height}px`;

View File

@ -1,6 +1,7 @@
{% load i18n l10n %} {% load i18n l10n %}
<div id="{{ id }}_div_map" class="dj_map_wrapper"> <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> <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 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 %} {% if display_raw %}<p>{% translate "Debugging window (serialized value)" %}</p>{% endif %}

View File

@ -32,6 +32,9 @@ details on these changes.
* The ``django.contrib.postgres.fields.CIText`` mixin will be removed. * 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: .. _deprecation-removed-in-5.0:
5.0 5.0

View File

@ -106,6 +106,11 @@ from other Django widget attributes.
Height and width of the widget map (default is 400x600). 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 .. attribute:: BaseGeometryWidget.map_srid
SRID code used by the map (default is 4326). SRID code used by the map (default is 4326).
@ -131,7 +136,7 @@ widget. For example::
class MyGeoForm(forms.Form): class MyGeoForm(forms.Form):
point = forms.PointField(widget= point = forms.PointField(widget=
forms.OSMWidget(attrs={'map_width': 800, 'map_height': 500})) forms.OSMWidget(attrs={'display_raw': True}))
Widget classes Widget classes
-------------- --------------

View File

@ -369,3 +369,6 @@ Miscellaneous
collation. collation.
* ``django.contrib.postgres.fields.CIText`` mixin is deprecated. * ``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.

View File

@ -5,6 +5,7 @@ from django.contrib.gis.forms import BaseGeometryWidget, OpenLayersWidget
from django.contrib.gis.geos import GEOSGeometry from django.contrib.gis.geos import GEOSGeometry
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.test import SimpleTestCase, override_settings from django.test import SimpleTestCase, override_settings
from django.utils.deprecation import RemovedInDjango51Warning
from django.utils.html import escape from django.utils.html import escape
@ -485,3 +486,19 @@ class GeometryWidgetTests(SimpleTestCase):
form = PointForm(data={"p": point.json}) form = PointForm(data={"p": point.json})
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data["p"].srid, 4326) 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})