diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py index 60041cd75f..eca2c67a8d 100644 --- a/django/contrib/gis/forms/widgets.py +++ b/django/contrib/gis/forms/widgets.py @@ -103,6 +103,13 @@ class OSMWidget(BaseGeometryWidget): 'gis/js/OLMapWidget.js', ) + def __init__(self, attrs=None): + super(OSMWidget, self).__init__() + for key in ('default_lon', 'default_lat'): + self.attrs[key] = getattr(self, key) + if attrs: + self.attrs.update(attrs) + @property def map_srid(self): # Use the official spherical mercator projection SRID on versions @@ -111,12 +118,3 @@ class OSMWidget(BaseGeometryWidget): return 3857 else: return 900913 - - def render(self, name, value, attrs=None): - default_attrs = { - 'default_lon': self.default_lon, - 'default_lat': self.default_lat, - } - if attrs: - default_attrs.update(attrs) - return super(OSMWidget, self).render(name, value, default_attrs) diff --git a/django/contrib/gis/tests/test_geoforms.py b/django/contrib/gis/tests/test_geoforms.py index bb2f65a962..2eb91ec527 100644 --- a/django/contrib/gis/tests/test_geoforms.py +++ b/django/contrib/gis/tests/test_geoforms.py @@ -257,6 +257,15 @@ class SpecializedFieldTest(SimpleTestCase): for invalid in [geo for key, geo in self.geometries.items() if key != 'geometrycollection']: self.assertFalse(GeometryForm(data={'g': invalid.wkt}).is_valid()) + +@skipUnless(HAS_GDAL and HAS_SPATIALREFSYS, + "OSMWidgetTest needs gdal support and a spatial database") +class OSMWidgetTest(SimpleTestCase): + def setUp(self): + self.geometries = { + 'point': GEOSGeometry("SRID=4326;POINT(9.052734375 42.451171875)"), + } + def test_osm_widget(self): class PointForm(forms.Form): p = forms.PointField(widget=forms.OSMWidget) @@ -264,9 +273,32 @@ class SpecializedFieldTest(SimpleTestCase): geom = self.geometries['point'] form = PointForm(data={'p': geom}) rendered = form.as_p() + self.assertIn("OpenStreetMap (Mapnik)", rendered) self.assertIn("id: 'id_p',", rendered) + def test_default_lat_lon(self): + class PointForm(forms.Form): + p = forms.PointField( + widget=forms.OSMWidget(attrs={ + 'default_lon': 20, 'default_lat': 30 + }), + ) + + form = PointForm() + rendered = form.as_p() + + self.assertIn("options['default_lon'] = 20;", rendered) + self.assertIn("options['default_lat'] = 30;", rendered) + if forms.OSMWidget.default_lon != 20: + self.assertNotIn( + "options['default_lon'] = %d;" % forms.OSMWidget.default_lon, + rendered) + if forms.OSMWidget.default_lat != 30: + self.assertNotIn( + "options['default_lat'] = %d;" % forms.OSMWidget.default_lat, + rendered) + @skipUnless(HAS_GDAL and HAS_SPATIALREFSYS, "CustomGeometryWidgetTest needs gdal support and a spatial database")