Fixed #17328 -- Added OpenLayersWidget _has_changed method

Thanks Will Hardy for the report and the patch.
This commit is contained in:
Claude Paroz 2012-06-06 10:40:11 +02:00
parent 29a80354ab
commit 9c096ab981
3 changed files with 44 additions and 3 deletions

View File

@ -230,6 +230,7 @@ answer newbie questions, and generally made Django that much better:
Scot Hacker <shacker@birdhouse.org> Scot Hacker <shacker@birdhouse.org>
dAniel hAhler dAniel hAhler
hambaloney hambaloney
Will Hardy <django@willhardy.com.au>
Brian Harring <ferringb@gmail.com> Brian Harring <ferringb@gmail.com>
Brant Harris Brant Harris
Ronny Haryanto <http://ronny.haryan.to/> Ronny Haryanto <http://ronny.haryan.to/>

View File

@ -4,7 +4,7 @@ from django.templatetags.static import static
from django.utils import translation from django.utils import translation
from django.contrib.gis.gdal import OGRException 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 # Creating a template context that contains Django settings
# values needed by admin map templates. # values needed by admin map templates.
@ -104,3 +104,25 @@ class OpenLayersWidget(Textarea):
raise TypeError raise TypeError
map_options[js_name] = value map_options[js_name] = value
return map_options 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 File

@ -2,7 +2,7 @@ from __future__ import absolute_import
from django.test import TestCase from django.test import TestCase
from django.contrib.gis import admin 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 from .models import City
@ -10,7 +10,7 @@ from .models import City
class GeoAdminTest(TestCase): class GeoAdminTest(TestCase):
urls = 'django.contrib.gis.tests.geoadmin.urls' urls = 'django.contrib.gis.tests.geoadmin.urls'
def test01_ensure_geographic_media(self): def test_ensure_geographic_media(self):
geoadmin = admin.site._registry[City] geoadmin = admin.site._registry[City]
admin_js = geoadmin.media.render_js() admin_js = geoadmin.media.render_js()
self.assertTrue(any([geoadmin.openlayers_url in js for js in admin_js])) self.assertTrue(any([geoadmin.openlayers_url in js for js in admin_js]))
@ -33,3 +33,21 @@ class GeoAdminTest(TestCase):
self.assertIn( self.assertIn(
"""geodjango_point.layers.base = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: \'basic\', format: 'image/jpeg'});""", """geodjango_point.layers.base = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: \'basic\', format: 'image/jpeg'});""",
result) 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))