Code

Ticket #15271: 15271.diff

File 15271.diff, 2.8 KB (added by copelco, 2 years ago)
Line 
1diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py
2index f806dcb..0b71774 100644
3--- a/django/contrib/gis/forms/fields.py
4+++ b/django/contrib/gis/forms/fields.py
5@@ -29,6 +29,15 @@ class GeometryField(forms.Field):
6         self.null = kwargs.pop('null', True)
7         super(GeometryField, self).__init__(**kwargs)
8 
9+    def to_python(self, value):
10+        """Transforms the value to a Geometry object."""
11+        # Trying to create a Geometry object from the form value.
12+        try:
13+            geom = GEOSGeometry(value)
14+        except:
15+            raise forms.ValidationError(self.error_messages['invalid_geom'])
16+        return geom
17+
18     def clean(self, value):
19         """
20         Validates that the input value can be converted to a Geometry
21@@ -42,11 +51,8 @@ class GeometryField(forms.Field):
22             else:
23                 raise forms.ValidationError(self.error_messages['no_geom'])
24 
25-        # Trying to create a Geometry object from the form value.
26-        try:
27-            geom = GEOSGeometry(value)
28-        except:
29-            raise forms.ValidationError(self.error_messages['invalid_geom'])
30+        # Transform the value to a python object first
31+        geom = self.to_python(value)
32 
33         # Ensuring that the geometry is of the correct type (indicated
34         # using the OGC string label).
35diff --git a/django/contrib/gis/tests/test_geoforms.py b/django/contrib/gis/tests/test_geoforms.py
36index 59fab01..35ef83d 100644
37--- a/django/contrib/gis/tests/test_geoforms.py
38+++ b/django/contrib/gis/tests/test_geoforms.py
39@@ -51,8 +51,25 @@ class GeometryFieldTest(unittest.TestCase):
40 
41         pnt_fld = forms.GeometryField(geom_type='POINT')
42         self.assertEqual(GEOSGeometry('POINT(5 23)'), pnt_fld.clean('POINT(5 23)'))
43+        # a WKT for any other geom_type will be properly transformed by `to_python`
44+        self.assertEqual(GEOSGeometry('LINESTRING(0 0, 1 1)'), pnt_fld.to_python('LINESTRING(0 0, 1 1)'))
45+        # but rejected by `clean`
46         self.assertRaises(forms.ValidationError, pnt_fld.clean, 'LINESTRING(0 0, 1 1)')
47 
48+    def test04_to_python(self):
49+        """
50+        Testing to_python returns a correct GEOSGeometry object or
51+        a ValidationError
52+        """
53+        fld = forms.GeometryField()
54+        # to_python returns the same GEOSGeometry for a WKT
55+        for wkt in ('POINT(5 23)', 'MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'LINESTRING(0 0, 1 1)'):
56+            self.assertEqual(GEOSGeometry(wkt), fld.to_python(wkt))
57+        # but raises a ValidationError for any other string
58+        for wkt in ('POINT(5)', 'MULTI   POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'BLAH(0 0, 1 1)'):
59+            self.assertRaises(forms.ValidationError, fld.to_python, wkt)
60+
61+
62 def suite():
63     s = unittest.TestSuite()
64     s.addTest(unittest.makeSuite(GeometryFieldTest))