Fixed #21496 -- Fixed crash when GeometryField uses TextInput

Thanks Rhett Garber for the report and initial patch.
This commit is contained in:
Claude Paroz 2013-11-23 10:53:09 +01:00
parent c8b637d88e
commit 34b8a38558
3 changed files with 28 additions and 11 deletions

View File

@ -44,10 +44,16 @@ class GeometryField(forms.Field):
if not isinstance(value, GEOSGeometry): if not isinstance(value, GEOSGeometry):
try: try:
value = GEOSGeometry(value) value = GEOSGeometry(value)
if not value.srid:
value.srid = self.widget.map_srid
except (GEOSException, ValueError, TypeError): except (GEOSException, ValueError, TypeError):
raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom') 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 return value
def clean(self, value): def clean(self, value):
@ -66,15 +72,12 @@ class GeometryField(forms.Field):
raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type') raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type')
# Transforming the geometry if the SRID was set. # Transforming the geometry if the SRID was set.
if self.srid: if self.srid and self.srid != -1 and self.srid != geom.srid:
if not geom.srid: try:
# Should match that of the field if not given. geom.transform(self.srid)
geom.srid = self.srid except GEOSException:
elif self.srid != -1 and self.srid != geom.srid: raise forms.ValidationError(
try: self.error_messages['transform_error'], code='transform_error')
geom.transform(self.srid)
except GEOSException:
raise forms.ValidationError(self.error_messages['transform_error'], code='transform_error')
return geom return geom

View File

@ -76,6 +76,19 @@ class GeometryFieldTest(SimpleTestCase):
for wkt in ('POINT(5)', 'MULTI POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'BLAH(0 0, 1 1)'): 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) 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, @skipUnless(HAS_GDAL and HAS_SPATIALREFSYS,
"SpecializedFieldTest needs gdal support and a spatial database") "SpecializedFieldTest needs gdal support and a spatial database")

View File

@ -39,3 +39,4 @@ Bug fixes
importing ``get_wsgi_application`` (#21486). importing ``get_wsgi_application`` (#21486).
* Fixed test client ``logout()`` method when using the cookie-based session * Fixed test client ``logout()`` method when using the cookie-based session
backend (#21448). backend (#21448).
* Fixed a crash when a ``GeometryField`` uses a non-geometric widget (#21496).