Code

Ticket #16110: 16110-2.diff

File 16110-2.diff, 3.5 KB (added by claudep, 19 months ago)

Updated patch

Line 
1diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py
2index cefb683..209dc84 100644
3--- a/django/contrib/gis/forms/fields.py
4+++ b/django/contrib/gis/forms/fields.py
5@@ -1,6 +1,9 @@
6 from __future__ import unicode_literals
7 
8+import warnings
9+
10 from django import forms
11+from django.core import validators
12 from django.utils.translation import ugettext_lazy as _
13 
14 # While this couples the geographic forms to the GEOS library,
15@@ -16,7 +19,7 @@ class GeometryField(forms.Field):
16     widget = forms.Textarea
17 
18     default_error_messages = {
19-        'no_geom' : _('No geometry value provided.'),
20+        'required' : _('No geometry value provided.'),
21         'invalid_geom' : _('Invalid geometry value.'),
22         'invalid_geom_type' : _('Invalid geometry type.'),
23         'transform_error' : _('An error occurred when transforming the geometry '
24@@ -28,7 +31,10 @@ class GeometryField(forms.Field):
25         # defaults (e.g., allow None).
26         self.srid = kwargs.pop('srid', None)
27         self.geom_type = kwargs.pop('geom_type', 'GEOMETRY')
28-        self.null = kwargs.pop('null', True)
29+        if 'null' in kwargs:
30+            _ = kwargs.pop('null', True)
31+            warnings.warn("Passing 'null' keyword argument to GeometryField is deprecated.",
32+                DeprecationWarning, stacklevel=2)
33         super(GeometryField, self).__init__(**kwargs)
34 
35     def to_python(self, value):
36@@ -46,14 +52,10 @@ class GeometryField(forms.Field):
37         object (which is returned).  A ValidationError is raised if
38         the value cannot be instantiated as a Geometry.
39         """
40-        if not value:
41-            if self.null and not self.required:
42-                # The geometry column allows NULL and is not required.
43-                return None
44-            else:
45-                raise forms.ValidationError(self.error_messages['no_geom'])
46+        self.validate(value)
47+        if value in validators.EMPTY_VALUES:
48+            return value
49 
50-        # Transform the value to a python object first
51         geom = self.to_python(value)
52 
53         # Ensuring that the geometry is of the correct type (indicated
54diff --git a/django/contrib/gis/tests/test_geoforms.py b/django/contrib/gis/tests/test_geoforms.py
55index ed851df..24bb50c 100644
56--- a/django/contrib/gis/tests/test_geoforms.py
57+++ b/django/contrib/gis/tests/test_geoforms.py
58@@ -1,6 +1,7 @@
59 from django.forms import ValidationError
60 from django.contrib.gis.gdal import HAS_GDAL
61 from django.contrib.gis.tests.utils import HAS_SPATIALREFSYS
62+from django.utils import six
63 from django.utils import unittest
64 
65 
66@@ -37,15 +38,13 @@ class GeometryFieldTest(unittest.TestCase):
67         "Testing GeometryField's handling of null (None) geometries."
68         # Form fields, by default, are required (`required=True`)
69         fld = forms.GeometryField()
70-        self.assertRaises(forms.ValidationError, fld.clean, None)
71-
72-        # Still not allowed if `null=False`.
73-        fld = forms.GeometryField(required=False, null=False)
74-        self.assertRaises(forms.ValidationError, fld.clean, None)
75+        with six.assertRaisesRegex(self, forms.ValidationError,
76+                "No geometry value provided."):
77+            fld.clean(None)
78 
79         # This will clean None as a geometry (See #10660).
80         fld = forms.GeometryField(required=False)
81-        self.assertEqual(None, fld.clean(None))
82+        self.assertIsNone(fld.clean(None))
83 
84     def test03_geom_type(self):
85         "Testing GeometryField's handling of different geometry types."