Fixed #21716 -- Only passed arguments supported by ogrinspect
Thanks Marco Badan for the report.
This commit is contained in:
parent
75220d3b5d
commit
d0eeddd6fc
|
@ -1,4 +1,6 @@
|
|||
import inspect
|
||||
from optparse import make_option
|
||||
|
||||
from django.contrib.gis import gdal
|
||||
from django.core.management.base import LabelCommand, CommandError
|
||||
|
||||
|
@ -83,27 +85,21 @@ class Command(LabelCommand):
|
|||
if not gdal.HAS_GDAL:
|
||||
raise CommandError('GDAL is required to inspect geospatial data sources.')
|
||||
|
||||
# Removing options with `None` values.
|
||||
options = dict((k, v) for k, v in options.items() if not v is None)
|
||||
|
||||
# Getting the OGR DataSource from the string parameter.
|
||||
try:
|
||||
ds = gdal.DataSource(data_source)
|
||||
except gdal.OGRException as msg:
|
||||
raise CommandError(msg)
|
||||
|
||||
# Whether the user wants to generate the LayerMapping dictionary as well.
|
||||
show_mapping = options.pop('mapping', False)
|
||||
|
||||
# Getting rid of settings that `_ogrinspect` doesn't like.
|
||||
options.pop('verbosity', False)
|
||||
options.pop('settings', False)
|
||||
|
||||
# Returning the output of ogrinspect with the given arguments
|
||||
# and options.
|
||||
from django.contrib.gis.utils.ogrinspect import _ogrinspect, mapping
|
||||
output = [s for s in _ogrinspect(ds, model_name, **options)]
|
||||
if show_mapping:
|
||||
# Filter options to params accepted by `_ogrinspect`
|
||||
ogr_options = dict((k, v) for k, v in options.items()
|
||||
if k in inspect.getargspec(_ogrinspect).args and v is not None)
|
||||
output = [s for s in _ogrinspect(ds, model_name, **ogr_options)]
|
||||
|
||||
if options['mapping']:
|
||||
# Constructing the keyword arguments for `mapping`, and
|
||||
# calling it on the data source.
|
||||
kwargs = {'geom_name': options['geom_name'],
|
||||
|
|
|
@ -115,6 +115,13 @@ class OGRInspectTest(TestCase):
|
|||
' objects = models.GeoManager()'
|
||||
))
|
||||
|
||||
def test_management_command(self):
|
||||
shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp')
|
||||
out = StringIO()
|
||||
call_command('ogrinspect', shp_file, 'City', stdout=out)
|
||||
output = out.getvalue()
|
||||
self.assertIn('class City(models.Model):', output)
|
||||
|
||||
|
||||
def get_ogr_db_string():
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue