Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #21003 -- Ensured geometry widget return value has SRID

Thanks Mathieu Leplatre for the report and initial patch.
  • Loading branch information...
commit dd656073ad80d0e8ec9e9dfea30da75a660d759c 1 parent 3550b27
Claude Paroz claudep authored
25 django/contrib/gis/forms/fields.py
View
@@ -41,10 +41,15 @@ def to_python(self, value):
"""
if value in self.empty_values:
return None
- try:
- return GEOSGeometry(value)
- except (GEOSException, ValueError, TypeError):
- raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')
+
+ if not isinstance(value, GEOSGeometry):
+ try:
+ value = GEOSGeometry(value)
+ if not value.srid:
+ value.srid = self.widget.map_srid
+ except (GEOSException, ValueError, TypeError):
+ raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')
+ return value
def clean(self, value):
"""
@@ -77,16 +82,14 @@ def clean(self, value):
def _has_changed(self, initial, data):
""" Compare geographic value of data with its initial value. """
- # Ensure we are dealing with a geographic object
- if isinstance(initial, six.string_types):
- try:
- initial = GEOSGeometry(initial)
- except (GEOSException, ValueError):
- initial = None
+ try:
+ data = self.to_python(data)
+ initial = self.to_python(initial)
+ except ValidationError:
+ return True
# Only do a geographic comparison if both values are available
if initial and data:
- data = fromstr(data)
data.transform(initial.srid)
# If the initial value was not added by the browser, the geometry
# provided may be slightly different, the first time it is saved.
2  django/contrib/gis/forms/widgets.py
View
@@ -39,7 +39,7 @@ def serialize(self, value):
def deserialize(self, value):
try:
- return GEOSGeometry(value)
+ return GEOSGeometry(value, self.map_srid)
except (GEOSException, ValueError) as err:
logger.error(
"Error creating geometry from value '%s' (%s)" % (
5 django/contrib/gis/tests/test_geoforms.py
View
@@ -282,3 +282,8 @@ class PointForm(forms.Form):
# Force deserialize use due to a string value
self.assertIn(escape(point.json), widget.render('p', point.json))
self.assertEqual(widget.deserialize_called, 1)
+
+ form = PointForm(data={'p': point.json})
+ self.assertTrue(form.is_valid())
+ # Ensure that resulting geometry has srid set
+ self.assertEqual(form.cleaned_data['p'].srid, 4326)
Please sign in to comment.
Something went wrong with that request. Please try again.