PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 1ms

/django/contrib/gis/management/commands/inspectdb.py

https://code.google.com/p/mango-py/
Python | 32 lines | 22 code | 4 blank | 6 comment | 5 complexity | 87d519c48b3f071d11407cf04d70bc0b MD5 | raw file
 1from optparse import make_option
 2
 3from django.core.management.base import CommandError
 4from django.core.management.commands.inspectdb import Command as InspectDBCommand
 5
 6class Command(InspectDBCommand):
 7    db_module = 'django.contrib.gis.db'
 8    gis_tables = {}
 9
10    def get_field_type(self, connection, table_name, row):
11        field_type, field_params, field_notes = super(Command, self).get_field_type(connection, table_name, row)
12        if field_type == 'GeometryField':
13            geo_col = row[0]
14            # Getting a more specific field type and any additional parameters
15            # from the `get_geometry_type` routine for the spatial backend.
16            field_type, geo_params = connection.introspection.get_geometry_type(table_name, geo_col)
17            field_params.update(geo_params)
18            # Adding the table name and column to the `gis_tables` dictionary, this
19            # allows us to track which tables need a GeoManager.
20            if table_name in self.gis_tables:
21                self.gis_tables[table_name].append(geo_col)
22            else:
23                self.gis_tables[table_name] = [geo_col]
24        return field_type, field_params, field_notes
25
26    def get_meta(self, table_name):
27        meta_lines = super(Command, self).get_meta(table_name)
28        if table_name in self.gis_tables:
29            # If the table is a geographic one, then we need make
30            # GeoManager the default manager for the model.
31            meta_lines.insert(0, '    objects = models.GeoManager()')
32        return meta_lines