From 9c096ab981d57123a0774f9ddf229084e0bb54a1 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 6 Jun 2012 10:40:11 +0200 Subject: [PATCH] Fixed #17328 -- Added OpenLayersWidget _has_changed method Thanks Will Hardy for the report and the patch. --- AUTHORS | 1 + django/contrib/gis/admin/widgets.py | 24 +++++++++++++++++++++- django/contrib/gis/tests/geoadmin/tests.py | 22 ++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index ad0e886981..5e1314bdd8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -230,6 +230,7 @@ answer newbie questions, and generally made Django that much better: Scot Hacker dAniel hAhler hambaloney + Will Hardy Brian Harring Brant Harris Ronny Haryanto diff --git a/django/contrib/gis/admin/widgets.py b/django/contrib/gis/admin/widgets.py index aaffae8f5f..c7b48e4263 100644 --- a/django/contrib/gis/admin/widgets.py +++ b/django/contrib/gis/admin/widgets.py @@ -4,7 +4,7 @@ from django.templatetags.static import static from django.utils import translation from django.contrib.gis.gdal import OGRException -from django.contrib.gis.geos import GEOSGeometry, GEOSException +from django.contrib.gis.geos import GEOSGeometry, GEOSException, fromstr # Creating a template context that contains Django settings # values needed by admin map templates. @@ -104,3 +104,25 @@ class OpenLayersWidget(Textarea): raise TypeError map_options[js_name] = value return map_options + + 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, basestring): + 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) diff --git a/django/contrib/gis/tests/geoadmin/tests.py b/django/contrib/gis/tests/geoadmin/tests.py index 53c00c6588..6fadebdb9a 100644 --- a/django/contrib/gis/tests/geoadmin/tests.py +++ b/django/contrib/gis/tests/geoadmin/tests.py @@ -2,7 +2,7 @@ from __future__ import absolute_import from django.test import TestCase from django.contrib.gis import admin -from django.contrib.gis.geos import Point +from django.contrib.gis.geos import GEOSGeometry, Point from .models import City @@ -10,7 +10,7 @@ from .models import City class GeoAdminTest(TestCase): urls = 'django.contrib.gis.tests.geoadmin.urls' - def test01_ensure_geographic_media(self): + def test_ensure_geographic_media(self): geoadmin = admin.site._registry[City] admin_js = geoadmin.media.render_js() self.assertTrue(any([geoadmin.openlayers_url in js for js in admin_js])) @@ -33,3 +33,21 @@ class GeoAdminTest(TestCase): self.assertIn( """geodjango_point.layers.base = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: \'basic\', format: 'image/jpeg'});""", result) + + def test_olwidget_has_changed(self): + """ Check that changes are accurately noticed by OpenLayersWidget. """ + geoadmin = admin.site._registry[City] + form = geoadmin.get_changelist_form(None)() + has_changed = form.fields['point'].widget._has_changed + + initial = Point(13.4197458572965953, 52.5194108501149799, srid=4326) + data_same = "SRID=3857;POINT(1493879.2754093995 6894592.019687599)" + data_almost_same = "SRID=3857;POINT(1493879.2754093990 6894592.019687590)" + data_changed = "SRID=3857;POINT(1493884.0527237 6894593.8111804)" + + self.assertTrue(has_changed(None, data_changed)) + self.assertTrue(has_changed(initial, "")) + self.assertFalse(has_changed(None, "")) + self.assertFalse(has_changed(initial, data_same)) + self.assertFalse(has_changed(initial, data_almost_same)) + self.assertTrue(has_changed(initial, data_changed))