o
    [h 5                     @   s   d dl 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	 d dl
mZ d dlmZ ddlmZ G d	d
 d
ZG dd deZG dd deZdS )    N)apps)OutputWrapper)NOT_PROVIDED)timezone)get_docs_version   )MigrationLoaderc                   @   sZ   e Zd 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d ZdS )MigrationQuestionerz
    Give 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   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/django/db/migrations/questioner.py__init__   s   

zMigrationQuestioner.__init__c                 C   s   || j v rdS zt|}W n ty   | jdd Y S w t|j\}}|du r2| jddS zt	
|}W n tyI   | jdd Y S w t|ddr[ttj|j}nt|drst|jdkridS tt|jd }td	d
 |D  S )z2Should we create an initial migration for the app?Task_initialFN__file____path__r   r   c                 s   s"    | ]}|d kr| 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getattroslistdirpathdirnamer   hasattrlenr   listany)r   	app_labelZ
app_configZmigrations_import_path_r   	filenamesr   r   r   r      s,   

zMigrationQuestioner.ask_initialc                 C      dS )#Adding a NOT NULL field to a model.Nr   r   
field_name
model_namer   r   r   ask_not_null_addition9      z)MigrationQuestioner.ask_not_null_additionc                 C   r-   )"Changing a NULL field to NOT NULL.Nr   r/   r   r   r   ask_not_null_alteration>   r3   z+MigrationQuestioner.ask_not_null_alterationc                 C      | j ddS )Was this field really renamed?
ask_renameFr   r   )r   r1   old_namenew_namefield_instancer   r   r   r8   C      zMigrationQuestioner.ask_renamec                 C   r6   )Was this model really renamed?ask_rename_modelFr9   )r   old_model_statenew_model_stater   r   r   r?   G   r=   z$MigrationQuestioner.ask_rename_modelc                 C   r6   )z)Should these migrations really be merged?	ask_mergeFr9   r   r*   r   r   r   rB   K   r=   zMigrationQuestioner.ask_mergec                 C   r-   )(Adding an auto_now_add field to a model.Nr   r/   r   r   r   ask_auto_now_add_additionO   r3   z-MigrationQuestioner.ask_auto_now_add_additionc                 C   r-   ).Adding a unique field with a callable default.Nr   r/   r   r   r   $ask_unique_callable_default_additionT   r3   z8MigrationQuestioner.ask_unique_callable_default_addition)NNN)__name__
__module____qualname____doc__r   r   r2   r5   r8   r?   rB   rE   rG   r   r   r   r   r	      s    
r	   c                       st   e Zd Z	d fdd	ZdddZd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  ZS )InteractiveMigrationQuestionerNc                    s&   t  j|||d |pttj| _d S N)r   r   r   )superr   r   sysstdoutprompt_output)r   r   r   r   rQ   	__class__r   r   r   [   s   z'InteractiveMigrationQuestioner.__init__c                 C   sz   | j j| ddd t }|s|d ur|S |r |d  dvr5| j jddd t }|r |d  dvs |d  dkS )N  endingr   ZynzPlease answer yes or no: y)rQ   writeinputlower)r   questiondefaultresultr   r   r   _boolean_inputc   s   z-InteractiveMigrationQuestioner._boolean_inputc                 C   s   | j |  t|D ]\}}| j d|d |f  q| j jddd 	 z	t }t|}W n ty7   Y n" tyJ   | j d td Y nw d|  k rXt	|krY|S  | j jd	dd q%)
Nz %s) %sr   zSelect an option: rU   rV   T
Cancelled.r   zPlease select a valid option: )
rQ   rY   	enumeraterZ   int
ValueErrorKeyboardInterruptrO   exitr'   )r   r\   choicesichoicer^   valuer   r   r   _choice_inputm   s&   z,InteractiveMigrationQuestioner._choice_inputrU   c              
   C   s  | j d |r| j d| d | j d | j d 	 |r'd|}nd}| j j|d	d
 zt }W n tyJ   | j d td Y nw |sQ|rQ|}|sZ| j d n4|dkrdtd n*z
t|i tt	dW S  t
y } z| j |jj d|  W Y d}~nd}~ww q)z
        Prompt for a default value.

        The ``default`` argument allows providing a custom default value (as a
        string) which will be shown to the user and used as the return value
        if the user doesn't provide any other input.
        z/Please enter the default value as valid Python.zAccept the default 'z/' by pressing 'Enter' or provide another value.zxThe datetime and django.utils.timezone modules are available, so it is possible to provide e.g. timezone.now as a value.zType 'exit' to exit this promptTz[default: {}] >>> z>>> rU   rV   r`   r   z;Please enter some code, or 'exit' (without quotes) to exit.re   )datetimer   z: N)rQ   rY   formatrZ   rd   rO   re   evalrk   r   	ExceptionrS   rH   )r   r]   promptcodeer   r   r   _ask_default   sF   

&z+InteractiveMigrationQuestioner._ask_defaultc                 C   sF   | j s!| d| d| dddg}|dkrtd dS |  S dS )	r.   z.It is impossible to add a non-nullable field 'z' to z{ without specifying a default. This is because the database needs something to populate existing rows.
Please select a fix:bProvide a one-off default now (will be set on all existing rows with a null value for this column)6Quit and manually define a default value in models.py.      Nr   rj   rO   re   rr   r   r0   r1   rh   r   r   r   r2      s   
z4InteractiveMigrationQuestioner.ask_not_null_additionc                 C   sR   | j s'| d| d| dg d}|dkrtS |dkr#td dS |  S dS )r4   z-It is impossible to change a nullable field 'z' on z to non-nullable without providing a default. This is because the database needs something to populate existing rows.
Please select a fix:)rs   zIgnore for now. Existing rows that contain NULL values will have to be handled manually, for example with a RunPython or RunSQL operation.rt   ru   rv   N)r   rj   r   rO   re   rr   rx   r   r   r   r5      s   
z6InteractiveMigrationQuestioner.ask_not_null_alterationc                 C   s"   d}|  ||||||jjf dS )r7   z(Was %s.%s renamed to %s.%s (a %s)? [y/N]F)r_   rS   rH   )r   r1   r:   r;   r<   msgr   r   r   r8      s   z)InteractiveMigrationQuestioner.ask_renamec                 C   s    d}|  ||j|j|jf dS )r>   z(Was the model %s.%s renamed to %s? [y/N]F)r_   r*   name)r   r@   rA   ry   r   r   r   r?      s   z/InteractiveMigrationQuestioner.ask_rename_modelc                 C   s   |  ddS )Nz
Merging will only work if the operations printed above do not conflict
with each other (working on different fields or models)
Should these migration branches be merged? [y/N]F)r_   rC   r   r   r   rB      s   z(InteractiveMigrationQuestioner.ask_mergec                 C   sJ   | j s#| d| d| dddg}|dkrtd d
S | jdd	S d
S )rD   z#It is impossible to add the field 'z' with 'auto_now_add=True' to zf without providing a default. This is because the database needs something to populate existing rows.
zDProvide a one-off default now which will be set on all existing rowsrt   ru   rv   ztimezone.now)r]   Nrw   rx   r   r   r   rE      s   
z8InteractiveMigrationQuestioner.ask_auto_now_add_additionc                 C   sH   | j s"t }| d| d| dd| ddg}|dkr"td d	S )
rF   z!Callable default on unique field .zO will not generate unique values upon migrating.
Please choose how to proceed:
zContinue making this migration as the first step in writing a manual migration to generate unique values described here: https://docs.djangoproject.com/en/z=/howto/writing-migrations/#migrations-that-add-unique-fields.z)Quit and edit field options in models.py.ru   rv   N)r   r   rj   rO   re   )r   r0   r1   versionrh   r   r   r   rG     s   
zCInteractiveMigrationQuestioner.ask_unique_callable_default_addition)NNNNr
   )rU   )rH   rI   rJ   r   r_   rj   rr   r2   r5   r8   r?   rB   rE   rG   __classcell__r   r   rR   r   rL   Z   s    


,	rL   c                       sH   e Zd Z					d fdd	Zdd Zdd Zd	d
 Zdd Z  ZS )!NonInteractiveMigrationQuestionerNr   c                    s"   || _ || _t j|||d d S rM   )	verbositylogrN   r   )r   r   r   r   r   r   rR   r   r   r   *  s   
z*NonInteractiveMigrationQuestioner.__init__c              	   C   s0   | j dkr| d| d| d| d d S d S )Nr   Field '' on model 'z' not migrated: r{   )r   r   )r   r0   r1   reasonr   r   r   log_lack_of_migration:  s   
z7NonInteractiveMigrationQuestioner.log_lack_of_migrationc                 C      |  ||d td d S )NzIit is impossible to add a non-nullable field without specifying a defaultrv   r   rO   re   r/   r   r   r   r2   A     z7NonInteractiveMigrationQuestioner.ask_not_null_additionc                 C   s   |  d| d| d tS )Nr   r   z8' given a default of NOT PROVIDED and must be corrected.)r   r   r/   r   r   r   r5   K  s   z9NonInteractiveMigrationQuestioner.ask_not_null_alterationc                 C   r   )NzUit is impossible to add a field with 'auto_now_add=True' without specifying a defaultrv   r   r/   r   r   r   rE   S  r   z;NonInteractiveMigrationQuestioner.ask_auto_now_add_addition)NNNr   N)	rH   rI   rJ   r   r   r2   r5   rE   r}   r   r   rR   r   r~   )  s    
r~   )rk   r   r"   rO   Zdjango.appsr   Zdjango.core.management.baser   Zdjango.db.modelsr   Zdjango.utilsr   Zdjango.utils.versionr   loaderr   r	   rL   r~   r   r   r   r   <module>   s    K P