From 3dd6b422ca658eaeddbf499513fb8b0a52b7f41d Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Fri, 18 Jan 2008 15:52:37 +0000 Subject: [PATCH] gis: `LayerMapping`: Fixed bug that would occur when mapping `OFTReal` fields with precision > 0 to an `IntegerField`. git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7024 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/gis/utils/layermapping.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/django/contrib/gis/utils/layermapping.py b/django/contrib/gis/utils/layermapping.py index c8aae59102..5475dd6c6e 100644 --- a/django/contrib/gis/utils/layermapping.py +++ b/django/contrib/gis/utils/layermapping.py @@ -124,6 +124,7 @@ from django.db import models, transaction class LayerMapError(Exception): pass class InvalidString(LayerMapError): pass class InvalidDecimal(LayerMapError): pass +class InvalidInteger(LayerMapError): pass class MissingForeignKey(LayerMapError): pass class LayerMapping(object): @@ -406,12 +407,12 @@ class LayerMapping(object): if len(val) > model_field.max_length: raise InvalidString('%s model field maximum string length is %s, given %s characters.' % (model_field.name, model_field.max_length, len(val))) - elif isinstance(ogr_field, OFTReal): + elif isinstance(ogr_field, OFTReal) and isinstance(model_field, models.DecimalField): try: # Creating an instance of the Decimal value to use. d = Decimal(str(ogr_field.value)) except: - raise InvalidDecimal('Could not construct decimal from: %s' % ogr_field) + raise InvalidDecimal('Could not construct decimal from: %s' % ogr_field.value) # Getting the decimal value as a tuple. dtup = d.as_tuple() @@ -434,6 +435,13 @@ class LayerMapping(object): raise InvalidDecimal('A DecimalField with max_digits %d, decimal_places %d must round to an absolute value less than 10^%d.' % (model_field.max_digits, model_field.decimal_places, max_prec)) val = d + elif isinstance(ogr_field, OFTReal) and isinstance(model_field, models.IntegerField): + # If there's an OFTReal field with precision greater than 0 is mapped to + # an IntegerField, the decimal places will be truncated. + try: + val = int(ogr_field.value) + except: + raise InvalidInteger('Could not construct integer from: %s' % ogr_field.value) else: val = ogr_field.value return val