diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py
index cefb683..209dc84 100644
|
a
|
b
|
|
| 1 | 1 | from __future__ import unicode_literals |
| 2 | 2 | |
| | 3 | import warnings |
| | 4 | |
| 3 | 5 | from django import forms |
| | 6 | from django.core import validators |
| 4 | 7 | from django.utils.translation import ugettext_lazy as _ |
| 5 | 8 | |
| 6 | 9 | # While this couples the geographic forms to the GEOS library, |
| … |
… |
class GeometryField(forms.Field):
|
| 16 | 19 | widget = forms.Textarea |
| 17 | 20 | |
| 18 | 21 | default_error_messages = { |
| 19 | | 'no_geom' : _('No geometry value provided.'), |
| | 22 | 'required' : _('No geometry value provided.'), |
| 20 | 23 | 'invalid_geom' : _('Invalid geometry value.'), |
| 21 | 24 | 'invalid_geom_type' : _('Invalid geometry type.'), |
| 22 | 25 | 'transform_error' : _('An error occurred when transforming the geometry ' |
| … |
… |
class GeometryField(forms.Field):
|
| 28 | 31 | # defaults (e.g., allow None). |
| 29 | 32 | self.srid = kwargs.pop('srid', None) |
| 30 | 33 | 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) |
| 32 | 38 | super(GeometryField, self).__init__(**kwargs) |
| 33 | 39 | |
| 34 | 40 | def to_python(self, value): |
| … |
… |
class GeometryField(forms.Field):
|
| 46 | 52 | object (which is returned). A ValidationError is raised if |
| 47 | 53 | the value cannot be instantiated as a Geometry. |
| 48 | 54 | """ |
| 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 |
| 55 | 58 | |
| 56 | | # Transform the value to a python object first |
| 57 | 59 | geom = self.to_python(value) |
| 58 | 60 | |
| 59 | 61 | # Ensuring that the geometry is of the correct type (indicated |
diff --git a/django/contrib/gis/tests/test_geoforms.py b/django/contrib/gis/tests/test_geoforms.py
index ed851df..24bb50c 100644
|
a
|
b
|
|
| 1 | 1 | from django.forms import ValidationError |
| 2 | 2 | from django.contrib.gis.gdal import HAS_GDAL |
| 3 | 3 | from django.contrib.gis.tests.utils import HAS_SPATIALREFSYS |
| | 4 | from django.utils import six |
| 4 | 5 | from django.utils import unittest |
| 5 | 6 | |
| 6 | 7 | |
| … |
… |
class GeometryFieldTest(unittest.TestCase):
|
| 37 | 38 | "Testing GeometryField's handling of null (None) geometries." |
| 38 | 39 | # Form fields, by default, are required (`required=True`) |
| 39 | 40 | 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) |
| 45 | 44 | |
| 46 | 45 | # This will clean None as a geometry (See #10660). |
| 47 | 46 | fld = forms.GeometryField(required=False) |
| 48 | | self.assertEqual(None, fld.clean(None)) |
| | 47 | self.assertIsNone(fld.clean(None)) |
| 49 | 48 | |
| 50 | 49 | def test03_geom_type(self): |
| 51 | 50 | "Testing GeometryField's handling of different geometry types." |