Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #21496 -- Fixed crash when GeometryField uses TextInput

Thanks Rhett Garber for the report and initial patch.
  • Loading branch information...
commit 34b8a385588a051814f77481c875047c8bd23b37 1 parent c8b637d
Claude Paroz authored November 23, 2013
25  django/contrib/gis/forms/fields.py
@@ -44,10 +44,16 @@ def to_python(self, value):
44 44
         if not isinstance(value, GEOSGeometry):
45 45
             try:
46 46
                 value = GEOSGeometry(value)
47  
-                if not value.srid:
48  
-                    value.srid = self.widget.map_srid
49 47
             except (GEOSException, ValueError, TypeError):
50 48
                 raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')
  49
+
  50
+        # Try to set the srid
  51
+        if not value.srid:
  52
+            try:
  53
+                value.srid = self.widget.map_srid
  54
+            except AttributeError:
  55
+                if self.srid:
  56
+                    value.srid = self.srid
51 57
         return value
52 58
 
53 59
     def clean(self, value):
@@ -66,15 +72,12 @@ def clean(self, value):
66 72
             raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type')
67 73
 
68 74
         # Transforming the geometry if the SRID was set.
69  
-        if self.srid:
70  
-            if not geom.srid:
71  
-                # Should match that of the field if not given.
72  
-                geom.srid = self.srid
73  
-            elif self.srid != -1 and self.srid != geom.srid:
74  
-                try:
75  
-                    geom.transform(self.srid)
76  
-                except GEOSException:
77  
-                    raise forms.ValidationError(self.error_messages['transform_error'], code='transform_error')
  75
+        if self.srid and self.srid != -1 and self.srid != geom.srid:
  76
+            try:
  77
+                geom.transform(self.srid)
  78
+            except GEOSException:
  79
+                raise forms.ValidationError(
  80
+                    self.error_messages['transform_error'], code='transform_error')
78 81
 
79 82
         return geom
80 83
 
13  django/contrib/gis/tests/test_geoforms.py
@@ -76,6 +76,19 @@ def test_to_python(self):
76 76
         for wkt in ('POINT(5)', 'MULTI   POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))', 'BLAH(0 0, 1 1)'):
77 77
             self.assertRaises(forms.ValidationError, fld.to_python, wkt)
78 78
 
  79
+    def test_field_with_text_widget(self):
  80
+        class PointForm(forms.Form):
  81
+            pt = forms.PointField(srid=4326, widget=forms.TextInput)
  82
+
  83
+        form = PointForm()
  84
+        cleaned_pt = form.fields['pt'].clean('POINT(5 23)')
  85
+        self.assertEqual(cleaned_pt, GEOSGeometry('POINT(5 23)'))
  86
+        self.assertEqual(4326, cleaned_pt.srid)
  87
+
  88
+        point = GEOSGeometry('SRID=4326;POINT(5 23)')
  89
+        form = PointForm(data={'pt': 'POINT(5 23)'}, initial={'pt': point})
  90
+        self.assertFalse(form.has_changed())
  91
+
79 92
 
80 93
 @skipUnless(HAS_GDAL and HAS_SPATIALREFSYS,
81 94
     "SpecializedFieldTest needs gdal support and a spatial database")
1  docs/releases/1.6.1.txt
@@ -39,3 +39,4 @@ Bug fixes
39 39
   importing ``get_wsgi_application`` (#21486).
40 40
 * Fixed test client ``logout()`` method when using the cookie-based session
41 41
   backend (#21448).
  42
+* Fixed a crash when a ``GeometryField`` uses a non-geometric widget (#21496).

0 notes on commit 34b8a38

Please sign in to comment.
Something went wrong with that request. Please try again.