Fixed #36246 -- Caught GDALException in BaseGeometryWidget.deserialize.

Signed-off-by: JaeHyuck Sa <wogur981208@gmail.com>
This commit is contained in:
JaeHyuck Sa
2026-01-18 02:39:05 +09:00
committed by Jacob Walls
parent 6380e3f01e
commit 7c54fee776
3 changed files with 16 additions and 6 deletions

View File

@@ -1,5 +1,4 @@
from django import forms from django import forms
from django.contrib.gis.gdal import GDALException
from django.contrib.gis.geos import GEOSException, GEOSGeometry from django.contrib.gis.geos import GEOSException, GEOSGeometry
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@@ -41,10 +40,7 @@ class GeometryField(forms.Field):
if not isinstance(value, GEOSGeometry): if not isinstance(value, GEOSGeometry):
if hasattr(self.widget, "deserialize"): if hasattr(self.widget, "deserialize"):
try: value = self.widget.deserialize(value)
value = self.widget.deserialize(value)
except GDALException:
value = None
else: else:
try: try:
value = GEOSGeometry(value) value = GEOSGeometry(value)

View File

@@ -1,6 +1,7 @@
import logging import logging
from django.contrib.gis import gdal from django.contrib.gis import gdal
from django.contrib.gis.gdal import GDALException
from django.contrib.gis.geometry import json_regex 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
@@ -36,7 +37,7 @@ class BaseGeometryWidget(Widget):
def deserialize(self, value): def deserialize(self, value):
try: try:
return GEOSGeometry(value) return GEOSGeometry(value)
except (GEOSException, ValueError, TypeError) as err: except (GEOSException, GDALException, ValueError, TypeError) as err:
logger.error("Error creating geometry from value '%s' (%s)", value, err) logger.error("Error creating geometry from value '%s' (%s)", value, err)
return None return None

View File

@@ -494,6 +494,19 @@ class GeometryWidgetTests(SimpleTestCase):
context = widget.get_context("geometry", None, None) context = widget.get_context("geometry", None, None)
self.assertEqual(context["widget"]["attrs"]["geom_name"], "Geometry") self.assertEqual(context["widget"]["attrs"]["geom_name"], "Geometry")
def test_invalid_values(self):
bad_inputs = [
"POINT(5)",
"MULTI POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))",
"BLAH(0 0, 1 1)",
'{"type": "FeatureCollection", "features": ['
'{"geometry": {"type": "Point", "coordinates": [508375, 148905]}, '
'"type": "Feature"}]}',
]
for input in bad_inputs:
with self.subTest(input=input):
self.assertIsNone(BaseGeometryWidget().deserialize(input))
def test_subwidgets(self): def test_subwidgets(self):
widget = forms.BaseGeometryWidget() widget = forms.BaseGeometryWidget()
self.assertEqual( self.assertEqual(