From 34b8a385588a051814f77481c875047c8bd23b37 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 23 Nov 2013 10:53:09 +0100 Subject: [PATCH] Fixed #21496 -- Fixed crash when GeometryField uses TextInput Thanks Rhett Garber for the report and initial patch. --- django/contrib/gis/forms/fields.py | 25 +++++++++++++---------- django/contrib/gis/tests/test_geoforms.py | 13 ++++++++++++ docs/releases/1.6.1.txt | 1 + 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py index 151f66c39c..d0eee9324e 100644 --- a/django/contrib/gis/forms/fields.py +++ b/django/contrib/gis/forms/fields.py @@ -44,10 +44,16 @@ class GeometryField(forms.Field): 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): @@ -66,15 +72,12 @@ class GeometryField(forms.Field): 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 GEOSException: - 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 GEOSException: + raise forms.ValidationError( + self.error_messages['transform_error'], code='transform_error') return geom diff --git a/django/contrib/gis/tests/test_geoforms.py b/django/contrib/gis/tests/test_geoforms.py index ca28fb503c..bb2f65a962 100644 --- a/django/contrib/gis/tests/test_geoforms.py +++ b/django/contrib/gis/tests/test_geoforms.py @@ -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)'): 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") diff --git a/docs/releases/1.6.1.txt b/docs/releases/1.6.1.txt index 91c6a9e261..f7c76afbb1 100644 --- a/docs/releases/1.6.1.txt +++ b/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).