Commit 34b8a385 authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #21496 -- Fixed crash when GeometryField uses TextInput

Thanks Rhett Garber for the report and initial patch.
parent c8b637d8
Loading
Loading
Loading
Loading
+14 −11
Original line number Diff line number Diff line
@@ -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:
        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')
                raise forms.ValidationError(
                    self.error_messages['transform_error'], code='transform_error')

        return geom

+13 −0
Original line number Diff line number Diff line
@@ -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")
+1 −0
Original line number Diff line number Diff line
@@ -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).