From 4dcfacb5b92dc06c4a351119cf501d842d46d192 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 18 Sep 2014 18:54:38 +0200 Subject: [PATCH] Fixed string length check in LayerMapping --- django/contrib/gis/tests/layermap/models.py | 1 + django/contrib/gis/tests/layermap/tests.py | 9 ++++++++- django/contrib/gis/utils/layermapping.py | 6 +++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/django/contrib/gis/tests/layermap/models.py b/django/contrib/gis/tests/layermap/models.py index 96481ced8b..52483867fd 100644 --- a/django/contrib/gis/tests/layermap/models.py +++ b/django/contrib/gis/tests/layermap/models.py @@ -31,6 +31,7 @@ class CountyFeat(NamedModel): class City(NamedModel): name_txt = models.TextField(default='') + name_short = models.CharField(max_length=5) population = models.IntegerField() density = models.DecimalField(max_digits=7, decimal_places=1) dt = models.DateField() diff --git a/django/contrib/gis/tests/layermap/tests.py b/django/contrib/gis/tests/layermap/tests.py index 2461c7be8c..b9b1404b3c 100644 --- a/django/contrib/gis/tests/layermap/tests.py +++ b/django/contrib/gis/tests/layermap/tests.py @@ -15,7 +15,7 @@ from django.utils._os import upath if HAS_GDAL: from django.contrib.gis.utils.layermapping import (LayerMapping, - LayerMapError, InvalidDecimal, MissingForeignKey) + LayerMapError, InvalidDecimal, InvalidString, MissingForeignKey) from django.contrib.gis.gdal import DataSource from .models import ( @@ -287,6 +287,13 @@ class LayerMapTest(TestCase): source_srs=4326) lm.save(silent=True) + def test_charfield_too_short(self): + mapping = copy(city_mapping) + mapping['name_short'] = 'Name' + lm = LayerMapping(City, city_shp, mapping) + with self.assertRaises(InvalidString): + lm.save(silent=True, strict=True) + def test_textfield(self): "Tests that String content fits also in a TextField" mapping = copy(city_mapping) diff --git a/django/contrib/gis/utils/layermapping.py b/django/contrib/gis/utils/layermapping.py index 0aea0c2e3a..49a5f5f274 100644 --- a/django/contrib/gis/utils/layermapping.py +++ b/django/contrib/gis/utils/layermapping.py @@ -346,9 +346,9 @@ class LayerMapping(object): val = force_text(ogr_field.value, self.encoding) else: val = ogr_field.value - if model_field.max_length and 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))) + if model_field.max_length and 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) and isinstance(model_field, models.DecimalField): try: # Creating an instance of the Decimal value to use.