
 XJ2                 @   s   d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z m Z d  d l	 m
 Z
 m Z d  d l m Z Gd d   d e  Z d S)	    )unicode_literalsN)OrderedDict)BaseCommandCommandError)DEFAULT_DB_ALIASconnections)
force_textc               @   sj   e  Z d  Z d Z d Z d Z d d   Z d d   Z d d	   Z d
 d   Z	 d d   Z
 d d   Z d S)CommandzXIntrospects the database tables in the given database and outputs a Django model module.Fz	django.dbc          
   C   sN   | j  d d d d d d t d d | j  d	 d d d
 d d t d d d  S)Ntableactionstorenargs*typehelpz4Selects what tables or views should be introspected.z
--databasedestdatabasedefaultzMNominates a database to introspect. Defaults to using the "default" database.)add_argumentstrr   )selfparser r   W/home/ubuntu/projects/ifolica/build/django/django/core/management/commands/inspectdb.pyadd_arguments   s    zCommand.add_argumentsc             K   sW   y2 x+ |  j  |  D] } |  j j d |  q WWn t k
 rR t d   Yn Xd  S)Nz%s
zPDatabase inspection isn't supported for the currently selected database backend.)handle_inspectionstdoutwriteNotImplementedErrorr   )r   optionsliner   r   r   handle   s
    zCommand.handlec          ,   #   s  t  | d } | j d  } d d   } d d     | j   } d Vd Vd	 Vd
 Vd Vd Vd Vd Vd Vd |  j Vg  } | d p | j j |  } xN| D]F} | d  k	 r t |  r | |  s q q n  y y | j j | |  }	 Wn t k
 ri  }	 Yn Xy | j j	 | |  }
 Wn t k
 rCi  }
 Yn Xy | j j
 | |  } Wn t k
 rwi  } Yn X| j j | |  } Wn@ t k
 r} z  d | Vd t |  Vw WYd  d  } ~ Xn Xd Vd Vd | |  V| j | |   g  } i  } x| D]} g  } t   } | d } | |	 k } |  j | | |  \ } } } | j |  | j |  | j |  | | | <| |
 k r|
 | d rd | d <q|
 | d rd | d <qn  | r)|	 | d | k rd n | |	 | d  } | | k rd | } qkd | } nB |  j | | |  \ } } } | j |  | j |  | d 7} | d k r| i d d 6k r| d k rqq| d  k r| j j r| j d!  qn  | d" r| d# k rd$ } qd | d% <d | d& <n  d' | d( | k rd n d) | f } | j d*  r?| d+ 7} n  | r| j d  sa| d, 7} n  | d, j   f d- d.   | j   D  7} n  | d/ 7} | r| d0 d1 j |  7} n  d2 | VqWx" |  j | | |  D] } | VqWq WWd  QXd  S)3Nr   table_name_filterc             S   s   t  j d d |  j    S)Nz[^a-zA-Z0-9] )resubtitle)
table_namer   r   r   table2model)   s    z.Command.handle_inspection.<locals>.table2modelc             S   s!   |  j  d  r |  d d   S|  S)Nzu'   )
startswith)sr   r   r   strip_prefix,   s    z/Command.handle_inspection.<locals>.strip_prefixz0# This is an auto-generated Django model module.z<# You'll have to do the following manually to clean this up:z#   * Rearrange models' orderz>#   * Make sure each model has one field with primary_key=TruezL#   * Make sure each ForeignKey has `on_delete` set to the desired behavior.zh#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the tablezR# Feel free to rename the models, but don't rename db_table values or field names.z'from __future__ import unicode_literalsr#   zfrom %s import modelsr
   z# Unable to inspect table '%s'z# The error was: %szclass %s(models.Model):r   Zprimary_keyTuniquer)   r   zForeignKey(%szForeignKey('%s'(idz
AutoField(zIntegerField(z
AutoField?   zBooleanField(zNullBooleanField(Zblanknullz	%s = %s%s.zmodels.zForeignKey(z, models.DO_NOTHINGz, c             3   s1   |  ]' \ } } d  |   t  |   f Vq d S)z%s=%sN)repr).0kv)r,   r   r   	<genexpr>   s   z,Command.handle_inspection.<locals>.<genexpr>)z  #  z    %s)r   getcursor	db_moduleintrospectionZtable_namescallableZget_relationsr   Zget_indexesZget_constraintsZget_table_description	Exceptionr   appendr   normalize_col_nameupdateextendget_field_typefeaturesZcan_introspect_autofieldr*   endswithjoinitemsget_meta)r   r   
connectionr"   r(   r;   Zknown_modelsZtables_to_introspectr'   Z	relationsindexesconstraintsZtable_descriptioneused_column_namescolumn_to_field_namerowZcomment_notesextra_paramsZcolumn_nameis_relationZatt_nameparamsZnotesrel_to
field_typefield_paramsfield_notesZ
field_descZ	meta_liner   )r,   r   r   $   s    			



	

	
zCommand.handle_inspectionc       	      C   s;  i  } g  } | j    } | | k r4 | j d  n  | ri | j d  r\ | d d  } qi | | d <n  t j d d |  \ } } | d k r | j d	  n  | j d
  d k rx+ | j d
  d k r | j d
 d  } q W| j    j d
  d k r| j d  qn  | j d  r:d | } | j d  n  | j d  rcd | } | j d  n  t j	 |  r| d 7} | j d  n  | d j
   rd | } | j d  n  | | k rd } x$ d | | f | k r| d 7} qWd | | f } | j d  n  | | k r.| r.| | d <n  | | | f S)zU
        Modify the column name to make it Python-compatible as a field name
        zField name made lowercase._idN   Z	db_columnz\W_r   z.Field renamed to remove unsuitable characters.__z>Field renamed because it contained more than one '_' in a row.zfield%sz*Field renamed because it started with '_'.z%sfieldz(Field renamed because it ended with '_'.Z_fieldz4Field renamed because it was a Python reserved word.z	number_%sz:Field renamed because it wasn't a valid Python identifier.z%s_%dr)   z'Field renamed because of name conflict.)lowerr@   rF   r$   subnfindreplacer*   keyword	iskeywordisdigit)	r   Zcol_namerN   rR   rV   rW   new_nameZnum_replnumr   r   r   rA      sL    



zCommand.normalize_col_namec             C   s_  t    } g  } y | j j | d |  } Wn% t k
 rS d } | j d  Yn Xt |  t k r | \ } } | j |  n  | d k r | d r t | d  | d <n  | d k rR| d d	 k s | d
 d	 k r3| j d  | d d	 k	 r| d n d | d <| d
 d	 k	 r&| d
 n d
 | d <qR| d | d <| d
 | d <n  | | | f S)z
        Given the database connection, the table name, and the cursor row
        description, this routine will return the given field type name, as
        well as any additional keyword parameters and notes for the field.
        r)   Z	TextFieldzThis field type is a guess.Z	CharFieldrY   
max_lengthZDecimalField   N   zamax_digits and decimal_places have been guessed, as this database handles decimal fields as float
   Z
max_digitsZdecimal_places)	r   r=   rD   KeyErrorr@   r   tuplerB   int)r   rJ   r'   rP   rV   rW   rU   
new_paramsr   r   r   rD      s*    	 $'zCommand.get_field_typec       
         s   g  } x} | j    D]o \ } } | d r | d } t |  d k r d d j   f d d   | D  d } | j |  q q q Wd	 d
 d d | g }	 | r d d j |  d } |	 d | g 7}	 n  |	 S)z
        Return a sequence comprising the lines of code necessary
        to construct the inner Meta class for the model corresponding
        to the given database table name.
        r-   columnsr)   r.   z, c             3   s   |  ] } d    | Vq d S)z'%s'Nr   )r4   c)rO   r   r   r7     s    z#Command.get_meta.<locals>.<genexpr>r8   r#   z    class Meta:z        managed = Falsez        db_table = '%s'z,)z        unique_together = %s)rH   lenrG   r@   )
r   r'   rL   rO   Zunique_togetherindexrS   rn   tupmetar   )rO   r   rI   	  s    

*zCommand.get_metaN)__name__
__module____qualname__r   Zrequires_system_checksr<   r   r!   r   rA   rD   rI   r   r   r   r   r	      s   
8&r	   )
__future__r   ra   r$   collectionsr   Zdjango.core.management.baser   r   Z	django.dbr   r   Zdjango.utils.encodingr   r	   r   r   r   r   <module>   s   