Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.6.x] Fixed #21496 -- Fixed crash when GeometryField uses TextInput

Thanks Rhett Garber for the report and initial patch.
Backport of 34b8a38 from master.
  • Loading branch information...
commit 14ddc1b517266916d9e729bc20234edec1d7bd4c 1 parent 02f9e90
@claudep claudep authored
View
25 django/contrib/gis/forms/fields.py
@@ -51,10 +51,16 @@ def to_python(self, value):
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')
+
+ # Try to set the srid
+ if not value.srid:
+ try:
+ value.srid = self.widget.map_srid
+ except AttributeError:
+ if self.srid:
+ value.srid = self.srid
return value
def clean(self, value):
@@ -73,15 +79,12 @@ def clean(self, value):
raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type')
# Transforming the geometry if the SRID was set.
- if self.srid:
- if not geom.srid:
- # Should match that of the field if not given.
- geom.srid = self.srid
- elif self.srid != -1 and self.srid != geom.srid:
- try:
- geom.transform(self.srid)
- except:
- raise forms.ValidationError(self.error_messages['transform_error'], code='transform_error')
+ if self.srid and self.srid != -1 and self.srid != geom.srid:
+ try:
+ geom.transform(self.srid)
+ except:
+ raise forms.ValidationError(
+ self.error_messages['transform_error'], code='transform_error')
return geom
View
13 django/contrib/gis/tests/test_geoforms.py
@@ -74,6 +74,19 @@ def test_to_python(self):
for wkt in ('POINT(5)', 'MULTI POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'BLAH(0 0, 1 1)'):
self.assertRaises(forms.ValidationError, fld.to_python, wkt)
+ def test_field_with_text_widget(self):
+ class PointForm(forms.Form):
+ pt = forms.PointField(srid=4326, widget=forms.TextInput)
+
+ form = PointForm()
+ cleaned_pt = form.fields['pt'].clean('POINT(5 23)')
+ self.assertEqual(cleaned_pt, GEOSGeometry('POINT(5 23)'))
+ self.assertEqual(4326, cleaned_pt.srid)
+
+ point = GEOSGeometry('SRID=4326;POINT(5 23)')
+ form = PointForm(data={'pt': 'POINT(5 23)'}, initial={'pt': point})
+ self.assertFalse(form.has_changed())
+
@skipUnless(HAS_GDAL and HAS_SPATIALREFSYS,
"SpecializedFieldTest needs gdal support and a spatial database")
View
1  docs/releases/1.6.1.txt
@@ -39,3 +39,4 @@ Bug fixes
importing ``get_wsgi_application`` (#21486).
* Fixed test client ``logout()`` method when using the cookie-based session
backend (#21448).
+* Fixed a crash when a ``GeometryField`` uses a non-geometric widget (#21496).
Please sign in to comment.
Something went wrong with that request. Please try again.