#NEWLINE# def handle_inspection( self , options ) : #NEWLINE# #INDENT# connection = connections [ options . get ( 'database' ) ] #NEWLINE# table_name_filter = options . get ( 'table_name_filter' ) #NEWLINE# table2model = ( lambda table_name : table_name . title ( ) . replace ( '_' , '' ) . replace ( ' ' , '' ) . replace ( '-' , '' ) ) #NEWLINE# strip_prefix = ( lambda s : ( ( s . startswith ( "u'" ) and s [ 1 : ] ) or s ) ) #NEWLINE# cursor = connection . cursor ( ) #NEWLINE# ( yield '# This is an auto-generated Django model module.' ) #NEWLINE# ( yield "# You'll have to do the following manually to clean this up:" ) #NEWLINE# ( yield "#     * Rearrange models' order" ) #NEWLINE# ( yield '#     * Make sure each model has one field with primary_key=True' ) #NEWLINE# ( yield "# Feel free to rename the models, but don't rename db_table values or field names." ) #NEWLINE# ( yield '#' ) #NEWLINE# ( yield "# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'" ) #NEWLINE# ( yield '# into your database.' ) #NEWLINE# ( yield 'from __future__ import unicode_literals' ) #NEWLINE# ( yield '' ) #NEWLINE# ( yield ( 'from %s import models' % self . db_module ) ) #NEWLINE# ( yield '' ) #NEWLINE# known_models = [ ] #NEWLINE# for table_name in connection . introspection . table_names ( cursor ) : #NEWLINE# #INDENT# if ( ( table_name_filter is not None ) and callable ( table_name_filter ) ) : #NEWLINE# #INDENT# if ( not table_name_filter ( table_name ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# ( yield ( 'class %s(models.Model):' % table2model ( table_name ) ) ) #NEWLINE# known_models . append ( table2model ( table_name ) ) #NEWLINE# try : #NEWLINE# #INDENT# relations = connection . introspection . get_relations ( cursor , table_name ) #NEWLINE# #UNINDENT# except NotImplementedError : #NEWLINE# #INDENT# relations = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# indexes = connection . introspection . get_indexes ( table_name_filter , table_name ) #NEWLINE# #UNINDENT# except NotImplementedError : #NEWLINE# #INDENT# indexes = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# used_column_names = [ ] #NEWLINE# for ( i , row ) in enumerate ( connection . introspection . get_table_description ( cursor , table_name ) ) : #NEWLINE# #INDENT# comment_notes = [ ] #NEWLINE# extra_params = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# column_name = row [ 0 ] #NEWLINE# is_relation = ( i in relations ) #NEWLINE# ( att_name , params , notes ) = self . normalize_col_name ( column_name , used_column_names , is_relation ) #NEWLINE# extra_params . update ( params ) #NEWLINE# comment_notes . extend ( notes ) #NEWLINE# used_column_names . append ( att_name ) #NEWLINE# if ( column_name in indexes ) : #NEWLINE# #INDENT# if indexes [ column_name ] [ 'primary_key' ] : #NEWLINE# #INDENT# extra_params [ 'primary_key' ] = True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if indexes [ column_name ] [ 'unique' ] : #NEWLINE# #INDENT# extra_params [ 'unique' ] = True #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if is_relation : #NEWLINE# #INDENT# rel_to = ( ( ( relations [ i ] [ 1 ] == table_name ) and "'self'" ) or table2model ( relations [ i ] [ 1 ] ) ) #NEWLINE# if ( rel_to in known_models ) : #NEWLINE# #INDENT# field_type = ( 'ForeignKey(%s' % rel_to ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# field_type = ( "ForeignKey('%s'" % rel_to ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# ( field_type , field_params , field_notes ) = self . get_field_type ( connection , table_name , row ) #NEWLINE# extra_params . update ( field_params ) #NEWLINE# comment_notes . extend ( field_notes ) #NEWLINE# field_type += '(' #NEWLINE# #UNINDENT# if ( ( att_name == 'id' ) and ( field_type == 'AutoField(' ) and ( extra_params == { #NEWLINE# #INDENT# 'primary_key' : True , #NEWLINE# #UNINDENT# } ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if row [ 6 ] : #NEWLINE# #INDENT# extra_params [ 'blank' ] = True #NEWLINE# if ( not ( field_type in ( 'TextField(' , 'CharField(' ) ) ) : #NEWLINE# #INDENT# extra_params [ 'null' ] = True #NEWLINE# #UNINDENT# #UNINDENT# field_desc = ( '%s = models.%s' % ( att_name , field_type ) ) #NEWLINE# if extra_params : #NEWLINE# #INDENT# if ( not field_desc . endswith ( '(' ) ) : #NEWLINE# #INDENT# field_desc += ', ' #NEWLINE# #UNINDENT# field_desc += ', ' . join ( [ ( '%s=%s' % ( k , strip_prefix ( repr ( v ) ) ) ) for ( k , v ) in extra_params . items ( ) ] ) #NEWLINE# #UNINDENT# field_desc += ')' #NEWLINE# if comment_notes : #NEWLINE# #INDENT# field_desc += ( ' # ' + ' ' . join ( comment_notes ) ) #NEWLINE# #UNINDENT# ( yield ( '    %s' % field_desc ) ) #NEWLINE# #UNINDENT# for meta_line in self . get_meta ( table_name ) : #NEWLINE# #INDENT# ( yield meta_line )