From 75aeebebfe3df3ea46b8e12dd5e7719f98664d3a Mon Sep 17 00:00:00 2001 From: Dylan Verheul Date: Sat, 29 Apr 2017 15:35:46 +0200 Subject: [PATCH] Fixed #28105 -- Fixed crash in BaseGeometryWidget.get_context() when overriding existing attrs. --- django/contrib/gis/forms/widgets.py | 19 ++++++++++--------- docs/releases/1.11.1.txt | 3 +++ tests/gis_tests/test_geoforms.py | 7 +++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py index 82d9f26b1c7..df5cd9b3f03 100644 --- a/django/contrib/gis/forms/widgets.py +++ b/django/contrib/gis/forms/widgets.py @@ -63,15 +63,16 @@ class BaseGeometryWidget(Widget): if attrs is None: attrs = {} - context.update(self.build_attrs(self.attrs, dict( - name=name, - module='geodjango_%s' % name.replace('-', '_'), # JS-safe - serialized=self.serialize(value), - geom_type=gdal.OGRGeomType(self.attrs['geom_type']), - STATIC_URL=settings.STATIC_URL, - LANGUAGE_BIDI=translation.get_language_bidi(), - **attrs - ))) + build_attrs_kwargs = { + 'name': name, + 'module': 'geodjango_%s' % name.replace('-', '_'), # JS-safe + 'serialized': self.serialize(value), + 'geom_type': gdal.OGRGeomType(self.attrs['geom_type']), + 'STATIC_URL': settings.STATIC_URL, + 'LANGUAGE_BIDI': translation.get_language_bidi(), + } + build_attrs_kwargs.update(attrs) + context.update(self.build_attrs(self.attrs, build_attrs_kwargs)) return context diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index fd54a603a2c..1d0b70f76b9 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -66,3 +66,6 @@ Bugfixes * Updated the ``contrib.postgres`` ``SplitArrayWidget`` to use template-based widget rendering (:ticket:`28040`). + +* Fixed crash in ``BaseGeometryWidget.get_context()`` when overriding existing + ``attrs`` (:ticket:`28105`). diff --git a/tests/gis_tests/test_geoforms.py b/tests/gis_tests/test_geoforms.py index 73521bc2e6b..91bfc173f5f 100644 --- a/tests/gis_tests/test_geoforms.py +++ b/tests/gis_tests/test_geoforms.py @@ -1,6 +1,7 @@ import re from django.contrib.gis import forms +from django.contrib.gis.forms import BaseGeometryWidget from django.contrib.gis.geos import GEOSGeometry from django.forms import ValidationError from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature @@ -353,6 +354,12 @@ class OSMWidgetTest(SimpleTestCase): @skipUnlessDBFeature("gis_enabled") class GeometryWidgetTests(SimpleTestCase): + def test_get_context_attrs(self): + """The Widget.get_context() attrs argument overrides self.attrs.""" + widget = BaseGeometryWidget(attrs={'geom_type': 'POINT'}) + context = widget.get_context('point', None, attrs={'geom_type': 'POINT2'}) + self.assertEqual(context['geom_type'], 'POINT2') + def test_subwidgets(self): widget = forms.BaseGeometryWidget() self.assertEqual(