

_V                  @   s   d  d l  m Z m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m	 Z	 d  d l
 m Z m Z m Z d  d l m Z d d l m Z Gd	 d
   d
 e  Z Gd d   d e  Z Gd d   d e  Z d S)    )print_functionunicode_literalsN)apps)NOT_PROVIDED)datetime_safesixtimezone)input   )MigrationLoaderc               @   ss   e  Z d  Z d Z d d d d d  Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d   Z
 d S)MigrationQuestionerz
    Gives the autodetector responses to questions it might have.
    This base class has a built-in noninteractive mode, but the
    interactive subclass is what the command-line arguments will use.
    Nc             C   s.   | p	 i  |  _  | p t   |  _ | |  _ d  S)N)defaultssetspecified_appsdry_run)selfr   r   r    r   A/tmp/pip-build-ghmbqnp_/Django/django/db/migrations/questioner.py__init__   s    zMigrationQuestioner.__init__c             C   sM  | |  j  k r d Sy t j |  } Wn% t k
 rM |  j j d d  SYn Xt j | j  } | d k r |  j j d d  Sy t	 j
 |  } Wn% t k
 r |  j j d d  SYn Xt | d  r t j t j j | j   } nG t | d  r1t | j  d k rd St j t | j  d  } n  t d	 d
   | D  Sd S)z2Should we create an initial migration for the app?Task_initialFN__file____path__r
   r   c             s   s*   |  ]  } | d  k r | j  d  Vq d S)z__init__.pyz.pyN)endswith).0xr   r   r   	<genexpr>7   s    z2MigrationQuestioner.ask_initial.<locals>.<genexpr>)r   r   Zget_app_configLookupErrorr   getr   migrations_modulelabel	importlibimport_moduleImportErrorhasattroslistdirpathdirnamer   lenr   listany)r   	app_labelZ
app_configZmigrations_import_pathr   	filenamesr   r   r   r      s(    !zMigrationQuestioner.ask_initialc             C   s   d S)z"Adding a NOT NULL field to a modelNr   )r   
field_name
model_namer   r   r   ask_not_null_addition9   s    z)MigrationQuestioner.ask_not_null_additionc             C   s   d S)z!Changing a NULL field to NOT NULLNr   )r   r-   r.   r   r   r   ask_not_null_alteration>   s    z+MigrationQuestioner.ask_not_null_alterationc             C   s   |  j  j d d  S)zWas this field really renamed?
ask_renameF)r   r   )r   r.   old_namenew_namefield_instancer   r   r   r1   C   s    zMigrationQuestioner.ask_renamec             C   s   |  j  j d d  S)zWas this model really renamed?ask_rename_modelF)r   r   )r   old_model_statenew_model_stater   r   r   r5   G   s    z$MigrationQuestioner.ask_rename_modelc             C   s   |  j  j d d  S)z-Do you really want to merge these migrations?	ask_mergeF)r   r   )r   r+   r   r   r   r8   K   s    zMigrationQuestioner.ask_merge)__name__
__module____qualname____doc__r   r   r/   r0   r1   r5   r8   r   r   r   r   r      s   r   c               @   ss   e  Z d  Z d d d  Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z d d   Z	 d d   Z
 d S)InteractiveMigrationQuestionerNc             C   sv   t  d |  } | r' | d  k	 r' | Sx8 t |  d k  sR | d j   d k ra t  d  } q* W| d j   d k S)Nz%s r
   r   ZynzPlease answer yes or no: y)r	   r(   lower)r   questiondefaultresultr   r   r   _boolean_inputR   s    +z-InteractiveMigrationQuestioner._boolean_inputc             C   s   t  |  x2 t |  D]$ \ } } t  d | d | f  q Wt d  } x[ y6 t |  } d | k  oz t |  k n r | SWn t k
 r Yn Xt d  } qN Wd  S)Nz %s) %sr
   zSelect an option: r   zPlease select a valid option: )print	enumerater	   intr(   
ValueError)r   r@   choicesichoicerB   valuer   r   r   _choice_inputZ   s    
"z,InteractiveMigrationQuestioner._choice_inputc             C   s   t  d  t  d  x t j r/ t d  } n t d  j t j j  } | s] t  d  q | d k ry t j d  q y" t	 | i  i t
 d 6t d 6 SWq t t f k
 r } z t  d	 |  WYd  d  } ~ Xq Xq Wd  S)
Nz3Please enter the default value now, as valid Pythonz_The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()z>>> z;Please enter some code, or 'exit' (with no quotes) to exit.exitr
   datetimer   zInvalid input: %s)rD   r   PY3r	   decodesysstdinencodingrM   evalr   r   SyntaxError	NameError)r   codeer   r   r   _ask_defaulth   s    

	"z+InteractiveMigrationQuestioner._ask_defaultc             C   sX   |  j  sT |  j d | | f d d g  } | d k rG t j d  qT |  j   Sn  d S)z"Adding a NOT NULL field to a modelzYou are trying to add a non-nullable field '%s' to %s without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:z@Provide a one-off default now (will be set on all existing rows)z+Quit, and let me add a default in models.py      N)r   rL   rQ   rM   rY   )r   r-   r.   rJ   r   r   r   r/   }   s    	
z4InteractiveMigrationQuestioner.ask_not_null_additionc             C   sk   |  j  sg |  j d | | f d d d g  } | d k r> t S| d k rZ t j d  qg |  j   Sn  d S)z!Changing a NULL field to NOT NULLzYou are trying to change the nullable field '%s' on %s to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:z@Provide a one-off default now (will be set on all existing rows)zIgnore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)z+Quit, and let me add a default in models.pyrZ   r[   N)r   rL   r   rQ   rM   rY   )r   r-   r.   rJ   r   r   r   r0      s    	
z6InteractiveMigrationQuestioner.ask_not_null_alterationc             C   s/   d } |  j  | | | | | | j j f d  S)zWas this field really renamed?z+Did you rename %s.%s to %s.%s (a %s)? [y/N]F)rC   	__class__r9   )r   r.   r2   r3   r4   msgr   r   r   r1      s    z)InteractiveMigrationQuestioner.ask_renamec             C   s,   d } |  j  | | j | j | j f d  S)zWas this model really renamed?z+Did you rename the %s.%s model to %s? [y/N]F)rC   r+   name)r   r6   r7   r]   r   r   r   r5      s    z/InteractiveMigrationQuestioner.ask_rename_modelc             C   s   |  j  d d d d  S)NzH
Merging will only work if the operations printed above do not conflict
z8with each other (working on different fields or models)
z4Do you want to merge these migration branches? [y/N]F)rC   )r   r+   r   r   r   r8      s    z(InteractiveMigrationQuestioner.ask_merge)r9   r:   r;   rC   rL   rY   r/   r0   r1   r5   r8   r   r   r   r   r=   P   s   r=   c               @   s(   e  Z d  Z d d   Z d d   Z d S)!NonInteractiveMigrationQuestionerc             C   s   t  j d  d  S)Nr[   )rQ   rM   )r   r-   r.   r   r   r   r/      s    z7NonInteractiveMigrationQuestioner.ask_not_null_additionc             C   s   t  S)N)r   )r   r-   r.   r   r   r   r0      s    z9NonInteractiveMigrationQuestioner.ask_not_null_alterationN)r9   r:   r;   r/   r0   r   r   r   r   r_      s   r_   )
__future__r   r   r    r$   rQ   Zdjango.appsr   Zdjango.db.models.fieldsr   Zdjango.utilsr   r   r   Zdjango.utils.six.movesr	   loaderr   objectr   r=   r_   r   r   r   r   <module>   s   Al