1
0
mirror of https://github.com/django/django.git synced 2025-10-27 15:46:10 +00:00

Moved has_changed logic from widget to form field

Refs #16612. Thanks Aymeric Augustin for the suggestion.
This commit is contained in:
Claude Paroz
2013-01-25 20:50:46 +01:00
parent ce27fb198d
commit ebb504db69
14 changed files with 230 additions and 251 deletions

View File

@@ -1,11 +1,13 @@
from __future__ import unicode_literals
from django import forms
from django.utils import six
from django.utils.translation import ugettext_lazy as _
# While this couples the geographic forms to the GEOS library,
# it decouples from database (by not importing SpatialBackend).
from django.contrib.gis.geos import GEOSException, GEOSGeometry
from django.contrib.gis.geos import GEOSException, GEOSGeometry, fromstr
class GeometryField(forms.Field):
"""
@@ -73,3 +75,25 @@ class GeometryField(forms.Field):
raise forms.ValidationError(self.error_messages['transform_error'])
return geom
def _has_changed(self, initial, data):
""" Compare geographic value of data with its initial value. """
# Ensure we are dealing with a geographic object
if isinstance(initial, six.string_types):
try:
initial = GEOSGeometry(initial)
except (GEOSException, ValueError):
initial = None
# Only do a geographic comparison if both values are available
if initial and data:
data = fromstr(data)
data.transform(initial.srid)
# If the initial value was not added by the browser, the geometry
# provided may be slightly different, the first time it is saved.
# The comparison is done with a very low tolerance.
return not initial.equals_exact(data, tolerance=0.000001)
else:
# Check for change of state of existence
return bool(initial) != bool(data)