Ticket #15277: 15277.3.patch
File 15277.3.patch, 7.2 KB (added by , 13 years ago) |
---|
-
deleted file django/contrib/gis/management/base.py
diff --git a/django/contrib/gis/management/base.py b/django/contrib/gis/management/base.py deleted file mode 100644
+ - 1 from django.core.management.base import BaseCommand, CommandError2 3 class ArgsCommand(BaseCommand):4 """5 Command class for commands that take multiple arguments.6 """7 args = '<arg arg ...>'8 9 def handle(self, *args, **options):10 if not args:11 raise CommandError('Must provide the following arguments: %s' % self.args)12 return self.handle_args(*args, **options)13 14 def handle_args(self, *args, **options):15 raise NotImplementedError() -
django/contrib/gis/management/commands/ogrinspect.py
diff --git a/django/contrib/gis/management/commands/ogrinspect.py b/django/contrib/gis/management/commands/ogrinspect.py
a b 1 1 import os 2 2 from optparse import make_option 3 3 from django.contrib.gis import gdal 4 from django.co ntrib.gis.management.base import ArgsCommand, CommandError4 from django.core.management.base import LabelCommand, CommandError 5 5 6 6 def layer_option(option, opt, value, parser): 7 7 """ … … 26 26 dest = [s for s in value.split(',')] 27 27 setattr(parser.values, option.dest, dest) 28 28 29 class Command( ArgsCommand):29 class Command(LabelCommand): 30 30 help = ('Inspects the given OGR-compatible data source (e.g., a shapefile) and outputs\n' 31 31 'a GeoDjango model with the given model name. For example:\n' 32 32 ' ./manage.py ogrinspect zipcode.shp Zipcode') 33 33 args = '[data_source] [model_name]' 34 34 35 option_list = ArgsCommand.option_list + (35 option_list = LabelCommand.option_list + ( 36 36 make_option('--blank', dest='blank', type='string', action='callback', 37 37 callback=list_option, default=False, 38 38 help='Use a comma separated list of OGR field names to add ' … … 72 72 73 73 requires_model_validation = False 74 74 75 def handle _args(self, *args, **options):75 def handle(self, *args, **options): 76 76 try: 77 77 data_source, model_name = args 78 78 except ValueError: … … 81 81 if not gdal.HAS_GDAL: 82 82 raise CommandError('GDAL is required to inspect geospatial data sources.') 83 83 84 # TODO: Support non file-based OGR datasources.85 if not os.path.isfile(data_source):86 raise CommandError('The given data source cannot be found: "%s"' % data_source)87 88 84 # Removing options with `None` values. 89 85 options = dict([(k, v) for k, v in options.items() if not v is None]) 90 86 … … 97 93 # Whether the user wants to generate the LayerMapping dictionary as well. 98 94 show_mapping = options.pop('mapping', False) 99 95 100 # Popping the verbosity global option, as it's not accepted by `_ogrinspect`.96 # Getting rid of settings that `_ogrinspect` doesn't like. 101 97 verbosity = options.pop('verbosity', False) 98 settings = options.pop('settings', False) 102 99 103 100 # Returning the output of ogrinspect with the given arguments 104 101 # and options. … … 119 116 '%s_mapping = {' % model_name.lower()]) 120 117 output.extend([" '%s' : '%s'," % (rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options['layer_key']].fields]) 121 118 output.extend([" '%s' : '%s'," % (options['geom_name'], mapping_dict[options['geom_name']]), '}']) 122 return '\n'.join(output) 119 return '\n'.join(output) + '\n' -
django/contrib/gis/tests/__init__.py
diff --git a/django/contrib/gis/tests/__init__.py b/django/contrib/gis/tests/__init__.py
a b 61 61 from django.contrib.gis.tests import test_measure 62 62 suite.addTest(test_measure.suite()) 63 63 64 # Adding the ogrinspect tests. 65 from django.contrib.gis.tests import test_ogrinspect 66 suite.addTest(test_ogrinspect.suite()) 67 64 68 # Adding GDAL tests, and any test suite that depends on GDAL, to the 65 69 # suite if GDAL is available. 66 70 from django.contrib.gis.gdal import HAS_GDAL -
new file django/contrib/gis/tests/test_ogrinspect.py
diff --git a/django/contrib/gis/tests/test_ogrinspect.py b/django/contrib/gis/tests/test_ogrinspect.py new file mode 100644
- + 1 import os 2 from django.contrib.gis.utils.ogrinspect import ogrinspect 3 from django.contrib.gis.geometry.test_data import TEST_DATA 4 from django.utils import unittest 5 6 class OGRInspectTest(unittest.TestCase): 7 def test_poly(self): 8 shp_file = os.path.join(TEST_DATA, 'test_poly', 'test_poly.shp') 9 model_def = ogrinspect(shp_file, 'MyModel') 10 11 expected = [ 12 '# This is an auto-generated Django model module created by ogrinspect.', 13 'from django.contrib.gis.db import models', 14 '', 15 'class MyModel(models.Model):', 16 ' float = models.FloatField()', 17 ' int = models.FloatField()', 18 ' str = models.CharField(max_length=80)', 19 ' geom = models.PolygonField(srid=-1)', 20 ' objects = models.GeoManager()', 21 ] 22 23 self.assertEqual(model_def, '\n'.join(expected)) 24 25 def test_date_field(self): 26 shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp') 27 model_def = ogrinspect(shp_file, 'City') 28 29 expected = [ 30 '# This is an auto-generated Django model module created by ogrinspect.', 31 'from django.contrib.gis.db import models', 32 '', 33 'class City(models.Model):', 34 ' name = models.CharField(max_length=80)', 35 ' population = models.FloatField()', 36 ' density = models.FloatField()', 37 ' created = models.DateField()', 38 ' geom = models.PointField(srid=-1)', 39 ' objects = models.GeoManager()', 40 ] 41 42 self.assertEqual(model_def, '\n'.join(expected)) 43 44 def suite(): 45 s = unittest.TestSuite() 46 s.addTest(unittest.makeSuite(OGRInspectTest)) 47 return s 48 49 def run(verbosity=2): 50 unittest.TextTestRunner(verbosity=verbosity).run(suite()) 51 52 if __name__=="__main__": 53 run() -
django/contrib/gis/utils/ogrinspect.py
diff --git a/django/contrib/gis/utils/ogrinspect.py b/django/contrib/gis/utils/ogrinspect.py
a b 8 8 from itertools import izip 9 9 # Requires GDAL to use. 10 10 from django.contrib.gis.gdal import DataSource 11 from django.contrib.gis.gdal.field import OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString 11 from django.contrib.gis.gdal.field import OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString, OFTTime 12 12 13 13 def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False): 14 14 """ … … 189 189 yield ' %s = models.DateField(%s)' % (mfield, kwargs_str[2:]) 190 190 elif field_type is OFTDateTime: 191 191 yield ' %s = models.DateTimeField(%s)' % (mfield, kwargs_str[2:]) 192 elif field_type is OFT Date:192 elif field_type is OFTTime: 193 193 yield ' %s = models.TimeField(%s)' % (mfield, kwargs_str[2:]) 194 194 else: 195 195 raise TypeError('Unknown field type %s in %s' % (field_type, mfield))