Ticket #16110: 16110-2.diff

File 16110-2.diff, 3.5 KB (added by Claude Paroz, 12 years ago)

Updated patch

  • django/contrib/gis/forms/fields.py

    diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py
    index cefb683..209dc84 100644
    a b  
    11from __future__ import unicode_literals
    22
     3import warnings
     4
    35from django import forms
     6from django.core import validators
    47from django.utils.translation import ugettext_lazy as _
    58
    69# While this couples the geographic forms to the GEOS library,
    class GeometryField(forms.Field):  
    1619    widget = forms.Textarea
    1720
    1821    default_error_messages = {
    19         'no_geom' : _('No geometry value provided.'),
     22        'required' : _('No geometry value provided.'),
    2023        'invalid_geom' : _('Invalid geometry value.'),
    2124        'invalid_geom_type' : _('Invalid geometry type.'),
    2225        'transform_error' : _('An error occurred when transforming the geometry '
    class GeometryField(forms.Field):  
    2831        # defaults (e.g., allow None).
    2932        self.srid = kwargs.pop('srid', None)
    3033        self.geom_type = kwargs.pop('geom_type', 'GEOMETRY')
    31         self.null = kwargs.pop('null', True)
     34        if 'null' in kwargs:
     35            _ = kwargs.pop('null', True)
     36            warnings.warn("Passing 'null' keyword argument to GeometryField is deprecated.",
     37                DeprecationWarning, stacklevel=2)
    3238        super(GeometryField, self).__init__(**kwargs)
    3339
    3440    def to_python(self, value):
    class GeometryField(forms.Field):  
    4652        object (which is returned).  A ValidationError is raised if
    4753        the value cannot be instantiated as a Geometry.
    4854        """
    49         if not value:
    50             if self.null and not self.required:
    51                 # The geometry column allows NULL and is not required.
    52                 return None
    53             else:
    54                 raise forms.ValidationError(self.error_messages['no_geom'])
     55        self.validate(value)
     56        if value in validators.EMPTY_VALUES:
     57            return value
    5558
    56         # Transform the value to a python object first
    5759        geom = self.to_python(value)
    5860
    5961        # Ensuring that the geometry is of the correct type (indicated
  • 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 ed851df..24bb50c 100644
    a b  
    11from django.forms import ValidationError
    22from django.contrib.gis.gdal import HAS_GDAL
    33from django.contrib.gis.tests.utils import HAS_SPATIALREFSYS
     4from django.utils import six
    45from django.utils import unittest
    56
    67
    class GeometryFieldTest(unittest.TestCase):  
    3738        "Testing GeometryField's handling of null (None) geometries."
    3839        # Form fields, by default, are required (`required=True`)
    3940        fld = forms.GeometryField()
    40         self.assertRaises(forms.ValidationError, fld.clean, None)
    41 
    42         # Still not allowed if `null=False`.
    43         fld = forms.GeometryField(required=False, null=False)
    44         self.assertRaises(forms.ValidationError, fld.clean, None)
     41        with six.assertRaisesRegex(self, forms.ValidationError,
     42                "No geometry value provided."):
     43            fld.clean(None)
    4544
    4645        # This will clean None as a geometry (See #10660).
    4746        fld = forms.GeometryField(required=False)
    48         self.assertEqual(None, fld.clean(None))
     47        self.assertIsNone(fld.clean(None))
    4948
    5049    def test03_geom_type(self):
    5150        "Testing GeometryField's handling of different geometry types."
Back to Top