From 102f26c92923d0e5e80de9b473f2343f76b04aa8 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 30 Aug 2013 10:48:36 +0200 Subject: [PATCH] Fixed #20998 -- Allow custom (de)serialization for GIS widgets Thanks Mathieu Leplatre for the report and the initial patch. --- django/contrib/gis/forms/widgets.py | 33 ++++++++++--------- .../contrib/gis/templates/gis/openlayers.html | 6 ++-- django/contrib/gis/tests/test_geoforms.py | 28 ++++++++++++++++ docs/ref/contrib/gis/forms-api.txt | 8 ++--- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py index d50c7c005a..0102ab6745 100644 --- a/django/contrib/gis/forms/widgets.py +++ b/django/contrib/gis/forms/widgets.py @@ -22,51 +22,54 @@ class BaseGeometryWidget(Widget): map_srid = 4326 map_width = 600 map_height = 400 - display_wkt = False + display_raw = False supports_3d = False template_name = '' # set on subclasses def __init__(self, attrs=None): self.attrs = {} - for key in ('geom_type', 'map_srid', 'map_width', 'map_height', 'display_wkt'): + for key in ('geom_type', 'map_srid', 'map_width', 'map_height', 'display_raw'): self.attrs[key] = getattr(self, key) if attrs: self.attrs.update(attrs) + def serialize(self, value): + return value.wkt if value else '' + + def deserialize(self, value): + try: + return GEOSGeometry(value) + except (GEOSException, ValueError) as err: + logger.error( + "Error creating geometry from value '%s' (%s)" % ( + value, err) + ) + return None + def render(self, name, value, attrs=None): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, six.string_types): - try: - value = GEOSGeometry(value) - except (GEOSException, ValueError) as err: - logger.error( - "Error creating geometry from value '%s' (%s)" % ( - value, err) - ) - value = None + value = self.deserialize(value) - wkt = '' if value: # Check that srid of value and map match if value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) - wkt = ogr.wkt + value = ogr except gdal.OGRException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( value.srid, self.map_srid, err) ) - else: - wkt = value.wkt context = self.build_attrs(attrs, name=name, module='geodjango_%s' % name.replace('-','_'), # JS-safe - wkt=wkt, + serialized=self.serialize(value), geom_type=gdal.OGRGeomType(self.attrs['geom_type']), STATIC_URL=settings.STATIC_URL, LANGUAGE_BIDI=translation.get_language_bidi(), diff --git a/django/contrib/gis/templates/gis/openlayers.html b/django/contrib/gis/templates/gis/openlayers.html index 281c0badd6..4884f48106 100644 --- a/django/contrib/gis/templates/gis/openlayers.html +++ b/django/contrib/gis/templates/gis/openlayers.html @@ -2,7 +2,7 @@ #{{ id }}_map { width: {{ map_width }}px; height: {{ map_height }}px; } #{{ id }}_map .aligned label { float: inherit; } #{{ id }}_div_map { position: relative; vertical-align: top; float: {{ LANGUAGE_BIDI|yesno:"right,left" }}; } - {% if not display_wkt %}#{{ id }} { display: none; }{% endif %} + {% if not display_raw %}#{{ id }} { display: none; }{% endif %} .olControlEditingToolbar .olControlModifyFeatureItemActive { background-image: url("{{ STATIC_URL }}admin/img/gis/move_vertex_on.png"); background-repeat: no-repeat; @@ -16,8 +16,8 @@
Delete all Features - {% if display_wkt %}

WKT debugging window:

{% endif %} - + {% if display_raw %}

Debugging window (serialized value):

{% endif %} +