Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17328 -- Added OpenLayersWidget _has_changed method

Thanks Will Hardy for the report and the patch.
  • Loading branch information...
commit 9c096ab981d57123a0774f9ddf229084e0bb54a1 1 parent 29a8035
@claudep claudep authored
View
1  AUTHORS
@@ -230,6 +230,7 @@ answer newbie questions, and generally made Django that much better:
Scot Hacker <shacker@birdhouse.org>
dAniel hAhler
hambaloney
+ Will Hardy <django@willhardy.com.au>
Brian Harring <ferringb@gmail.com>
Brant Harris
Ronny Haryanto <http://ronny.haryan.to/>
View
24 django/contrib/gis/admin/widgets.py
@@ -4,7 +4,7 @@
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 @@ def ol_projection(srid):
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)
View
22 django/contrib/gis/tests/geoadmin/tests.py
@@ -2,7 +2,7 @@
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 @@
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 @@ def test_olmap_WMS_rendering(self):
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))
Please sign in to comment.
Something went wrong with that request. Please try again.