Code

Ticket #15277: 15277.3.patch

File 15277.3.patch, 7.2 KB (added by jbronn, 3 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, CommandError 
    2  
    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  
    11import os 
    22from optparse import make_option 
    33from django.contrib.gis import gdal 
    4 from django.contrib.gis.management.base import ArgsCommand, CommandError 
     4from django.core.management.base import LabelCommand, CommandError 
    55 
    66def layer_option(option, opt, value, parser): 
    77    """ 
     
    2626        dest = [s for s in value.split(',')] 
    2727    setattr(parser.values, option.dest, dest) 
    2828     
    29 class Command(ArgsCommand): 
     29class Command(LabelCommand): 
    3030    help = ('Inspects the given OGR-compatible data source (e.g., a shapefile) and outputs\n' 
    3131            'a GeoDjango model with the given model name. For example:\n' 
    3232            ' ./manage.py ogrinspect zipcode.shp Zipcode') 
    3333    args = '[data_source] [model_name]' 
    3434 
    35     option_list = ArgsCommand.option_list + ( 
     35    option_list = LabelCommand.option_list + ( 
    3636        make_option('--blank', dest='blank', type='string', action='callback',   
    3737                    callback=list_option, default=False, 
    3838                    help='Use a comma separated list of OGR field names to add ' 
     
    7272 
    7373    requires_model_validation = False 
    7474 
    75     def handle_args(self, *args, **options): 
     75    def handle(self, *args, **options): 
    7676        try: 
    7777            data_source, model_name = args 
    7878        except ValueError: 
     
    8181        if not gdal.HAS_GDAL: 
    8282            raise CommandError('GDAL is required to inspect geospatial data sources.') 
    8383 
    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          
    8884        # Removing options with `None` values. 
    8985        options = dict([(k, v) for k, v in options.items() if not v is None]) 
    9086 
     
    9793        # Whether the user wants to generate the LayerMapping dictionary as well. 
    9894        show_mapping = options.pop('mapping', False) 
    9995 
    100         # Popping the verbosity global option, as it's not accepted by `_ogrinspect`. 
     96        # Getting rid of settings that `_ogrinspect` doesn't like. 
    10197        verbosity = options.pop('verbosity', False) 
     98        settings = options.pop('settings', False) 
    10299 
    103100        # Returning the output of ogrinspect with the given arguments 
    104101        # and options. 
     
    119116                           '%s_mapping = {' % model_name.lower()]) 
    120117            output.extend(["    '%s' : '%s'," % (rev_mapping[ogr_fld], ogr_fld) for ogr_fld in ds[options['layer_key']].fields]) 
    121118            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  
    6161    from django.contrib.gis.tests import test_measure 
    6262    suite.addTest(test_measure.suite()) 
    6363 
     64    # Adding the ogrinspect tests. 
     65    from django.contrib.gis.tests import test_ogrinspect 
     66    suite.addTest(test_ogrinspect.suite()) 
     67 
    6468    # Adding GDAL tests, and any test suite that depends on GDAL, to the 
    6569    # suite if GDAL is available. 
    6670    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
    - +  
     1import os 
     2from django.contrib.gis.utils.ogrinspect import ogrinspect 
     3from django.contrib.gis.geometry.test_data import TEST_DATA 
     4from django.utils import unittest 
     5 
     6class 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 
     44def suite(): 
     45    s = unittest.TestSuite() 
     46    s.addTest(unittest.makeSuite(OGRInspectTest)) 
     47    return s 
     48 
     49def run(verbosity=2): 
     50    unittest.TextTestRunner(verbosity=verbosity).run(suite()) 
     51 
     52if __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  
    88from itertools import izip 
    99# Requires GDAL to use. 
    1010from django.contrib.gis.gdal import DataSource 
    11 from django.contrib.gis.gdal.field import OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString 
     11from django.contrib.gis.gdal.field import OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString, OFTTime 
    1212 
    1313def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False): 
    1414    """ 
     
    189189            yield '    %s = models.DateField(%s)' % (mfield, kwargs_str[2:]) 
    190190        elif field_type is OFTDateTime: 
    191191            yield '    %s = models.DateTimeField(%s)' % (mfield, kwargs_str[2:]) 
    192         elif field_type is OFTDate: 
     192        elif field_type is OFTTime: 
    193193            yield '    %s = models.TimeField(%s)' % (mfield, kwargs_str[2:]) 
    194194        else: 
    195195            raise TypeError('Unknown field type %s in %s' % (field_type, mfield))