Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17328 -- Added OpenLayersWidget _has_changed method

Thanks Will Hardy for the report and the patch.
  • Loading branch information...
commit 9c096ab981d57123a0774f9ddf229084e0bb54a1 1 parent 29a8035
Claude Paroz authored June 06, 2012
1  AUTHORS
@@ -230,6 +230,7 @@ answer newbie questions, and generally made Django that much better:
230 230
     Scot Hacker <shacker@birdhouse.org>
231 231
     dAniel hAhler
232 232
     hambaloney
  233
+    Will Hardy <django@willhardy.com.au>
233 234
     Brian Harring <ferringb@gmail.com>
234 235
     Brant Harris
235 236
     Ronny Haryanto <http://ronny.haryan.to/>
24  django/contrib/gis/admin/widgets.py
@@ -4,7 +4,7 @@
4 4
 from django.utils import translation
5 5
 
6 6
 from django.contrib.gis.gdal import OGRException
7  
-from django.contrib.gis.geos import GEOSGeometry, GEOSException
  7
+from django.contrib.gis.geos import GEOSGeometry, GEOSException, fromstr
8 8
 
9 9
 # Creating a template context that contains Django settings
10 10
 # values needed by admin map templates.
@@ -104,3 +104,25 @@ def ol_projection(srid):
104 104
                     raise TypeError
105 105
                 map_options[js_name] = value
106 106
         return map_options
  107
+
  108
+    def _has_changed(self, initial, data):
  109
+        """ Compare geographic value of data with its initial value. """
  110
+
  111
+        # Ensure we are dealing with a geographic object
  112
+        if isinstance(initial, basestring):
  113
+            try:
  114
+                initial = GEOSGeometry(initial)
  115
+            except (GEOSException, ValueError):
  116
+                initial = None
  117
+
  118
+        # Only do a geographic comparison if both values are available
  119
+        if initial and data:
  120
+            data = fromstr(data)
  121
+            data.transform(initial.srid)
  122
+            # If the initial value was not added by the browser, the geometry
  123
+            # provided may be slightly different, the first time it is saved.
  124
+            # The comparison is done with a very low tolerance.
  125
+            return not initial.equals_exact(data, tolerance=0.000001)
  126
+        else:
  127
+            # Check for change of state of existence
  128
+            return bool(initial) != bool(data)
22  django/contrib/gis/tests/geoadmin/tests.py
@@ -2,7 +2,7 @@
2 2
 
3 3
 from django.test import TestCase
4 4
 from django.contrib.gis import admin
5  
-from django.contrib.gis.geos import Point
  5
+from django.contrib.gis.geos import GEOSGeometry, Point
6 6
 
7 7
 from .models import City
8 8
 
@@ -10,7 +10,7 @@
10 10
 class GeoAdminTest(TestCase):
11 11
     urls = 'django.contrib.gis.tests.geoadmin.urls'
12 12
 
13  
-    def test01_ensure_geographic_media(self):
  13
+    def test_ensure_geographic_media(self):
14 14
         geoadmin = admin.site._registry[City]
15 15
         admin_js = geoadmin.media.render_js()
16 16
         self.assertTrue(any([geoadmin.openlayers_url in js for js in admin_js]))
@@ -33,3 +33,21 @@ def test_olmap_WMS_rendering(self):
33 33
         self.assertIn(
34 34
             """geodjango_point.layers.base = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: \'basic\', format: 'image/jpeg'});""",
35 35
             result)
  36
+
  37
+    def test_olwidget_has_changed(self):
  38
+        """ Check that changes are accurately noticed by OpenLayersWidget. """
  39
+        geoadmin = admin.site._registry[City]
  40
+        form = geoadmin.get_changelist_form(None)()
  41
+        has_changed = form.fields['point'].widget._has_changed
  42
+
  43
+        initial = Point(13.4197458572965953, 52.5194108501149799, srid=4326)
  44
+        data_same = "SRID=3857;POINT(1493879.2754093995 6894592.019687599)"
  45
+        data_almost_same = "SRID=3857;POINT(1493879.2754093990 6894592.019687590)"
  46
+        data_changed = "SRID=3857;POINT(1493884.0527237 6894593.8111804)"
  47
+
  48
+        self.assertTrue(has_changed(None, data_changed))
  49
+        self.assertTrue(has_changed(initial, ""))
  50
+        self.assertFalse(has_changed(None, ""))
  51
+        self.assertFalse(has_changed(initial, data_same))
  52
+        self.assertFalse(has_changed(initial, data_almost_same))
  53
+        self.assertTrue(has_changed(initial, data_changed))

0 notes on commit 9c096ab

Please sign in to comment.
Something went wrong with that request. Please try again.