Ticket #15271: 15271.diff

File 15271.diff, 2.8 KB (added by copelco, 3 years ago)
  • django/contrib/gis/forms/fields.py

    diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py
    index f806dcb..0b71774 100644
    a b class GeometryField(forms.Field): 
    2929        self.null = kwargs.pop('null', True)
    3030        super(GeometryField, self).__init__(**kwargs)
    3131
     32    def to_python(self, value):
     33        """Transforms the value to a Geometry object."""
     34        # Trying to create a Geometry object from the form value.
     35        try:
     36            geom = GEOSGeometry(value)
     37        except:
     38            raise forms.ValidationError(self.error_messages['invalid_geom'])
     39        return geom
     40
    3241    def clean(self, value):
    3342        """
    3443        Validates that the input value can be converted to a Geometry
    class GeometryField(forms.Field): 
    4251            else:
    4352                raise forms.ValidationError(self.error_messages['no_geom'])
    4453
    45         # Trying to create a Geometry object from the form value.
    46         try:
    47             geom = GEOSGeometry(value)
    48         except:
    49             raise forms.ValidationError(self.error_messages['invalid_geom'])
     54        # Transform the value to a python object first
     55        geom = self.to_python(value)
    5056
    5157        # Ensuring that the geometry is of the correct type (indicated
    5258        # using the OGC string label).
  • django/contrib/gis/tests/test_geoforms.py

    diff --git a/django/contrib/gis/tests/test_geoforms.py b/django/contrib/gis/tests/test_geoforms.py
    index 59fab01..35ef83d 100644
    a b class GeometryFieldTest(unittest.TestCase): 
    5151
    5252        pnt_fld = forms.GeometryField(geom_type='POINT')
    5353        self.assertEqual(GEOSGeometry('POINT(5 23)'), pnt_fld.clean('POINT(5 23)'))
     54        # a WKT for any other geom_type will be properly transformed by `to_python`
     55        self.assertEqual(GEOSGeometry('LINESTRING(0 0, 1 1)'), pnt_fld.to_python('LINESTRING(0 0, 1 1)'))
     56        # but rejected by `clean`
    5457        self.assertRaises(forms.ValidationError, pnt_fld.clean, 'LINESTRING(0 0, 1 1)')
    5558
     59    def test04_to_python(self):
     60        """
     61        Testing to_python returns a correct GEOSGeometry object or
     62        a ValidationError
     63        """
     64        fld = forms.GeometryField()
     65        # to_python returns the same GEOSGeometry for a WKT
     66        for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'):
     67            self.assertEqual(GEOSGeometry(wkt), fld.to_python(wkt))
     68        # but raises a ValidationError for any other string
     69        for wkt in ('POINT(5)', 'MULTI   POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'BLAH(0 0, 1 1)'):
     70            self.assertRaises(forms.ValidationError, fld.to_python, wkt)
     71
     72
    5673def suite():
    5774    s = unittest.TestSuite()
    5875    s.addTest(unittest.makeSuite(GeometryFieldTest))
Back to Top