Ticket #21003: 21003-3.diff

File 21003-3.diff, 3.1 KB (added by Claude Paroz, 11 years ago)
  • django/contrib/gis/forms/fields.py

    diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py
    index 59e7259..9b8b3cc 100644
    a b class GeometryField(forms.Field):  
    4747        """
    4848        if value in self.empty_values:
    4949            return None
    50         try:
    51             return GEOSGeometry(value)
    52         except (GEOSException, ValueError, TypeError):
    53             raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')
     50
     51        if not isinstance(value, GEOSGeometry):
     52            try:
     53                value = GEOSGeometry(value)
     54                if not value.srid:
     55                    value.srid = self.widget.map_srid
     56            except (GEOSException, ValueError, TypeError):
     57                raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')
     58        return value
    5459
    5560    def clean(self, value):
    5661        """
    class GeometryField(forms.Field):  
    8388    def _has_changed(self, initial, data):
    8489        """ Compare geographic value of data with its initial value. """
    8590
    86         # Ensure we are dealing with a geographic object
    87         if isinstance(initial, six.string_types):
    88             try:
    89                 initial = GEOSGeometry(initial)
    90             except (GEOSException, ValueError):
    91                 initial = None
     91        try:
     92            data = self.to_python(data)
     93            initial = self.to_python(initial)
     94        except ValidationError:
     95            return True
    9296
    9397        # Only do a geographic comparison if both values are available
    9498        if initial and data:
    95             data = fromstr(data)
    9699            data.transform(initial.srid)
    97100            # If the initial value was not added by the browser, the geometry
    98101            # provided may be slightly different, the first time it is saved.
  • django/contrib/gis/forms/widgets.py

    diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py
    index 0102ab6..b6f5f92 100644
    a b class BaseGeometryWidget(Widget):  
    3939
    4040    def deserialize(self, value):
    4141        try:
    42             return GEOSGeometry(value)
     42            return GEOSGeometry(value, self.map_srid)
    4343        except (GEOSException, ValueError) as err:
    4444            logger.error(
    4545                "Error creating geometry from value '%s' (%s)" % (
  • 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 b4f6087..5d9f9f0 100644
    a b class CustomGeometryWidgetTest(SimpleTestCase):  
    269269        # Force deserialize use due to a string value
    270270        self.assertIn(escape(point.json), widget.render('p', point.json))
    271271        self.assertEqual(widget.deserialize_called, 1)
     272
     273        form = PointForm(data={'p': point.json})
     274        self.assertTrue(form.is_valid())
     275        # Ensure that resulting geometry has srid set
     276        self.assertEqual(form.cleaned_data['p'].srid, 4326)
Back to Top