o 3a&@sddlZddlZddlZddlZddlmZddlmZddlm Z ddl m Z GdddZ Gd d d e Z Gd d d e ZdS) N)apps NOT_PROVIDED)timezone)MigrationLoaderc@sReZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ dS)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. NcCs |pi|_|p t|_||_dSN)defaultssetspecified_appsdry_run)selfr r r rA/usr/lib/python3/dist-packages/django/db/migrations/questioner.py__init__s   zMigrationQuestioner.__init__cCs||jvrdSzt|}Wnty|jddYSwt|j\}}|dur2|jddSzt |}Wnt yI|jddYSwt |ddr[t t j|j}nt|drst|jdkridSt t|jd}td d |D S) z2Should we create an initial migration for the app?T ask_initialFN__file____path__rrcss"|] }|dkr|dVqdS)z __init__.pyz.pyN)endswith).0xrrr 6s z2MigrationQuestioner.ask_initial..)r rget_app_config LookupErrorr getrmigrations_modulelabel importlib import_module ImportErrorgetattroslistdirpathdirnamerhasattrlenrlistany)r app_label app_configmigrations_import_path_r filenamesrrrrs,     zMigrationQuestioner.ask_initialcCdS)#Adding a NOT NULL field to a model.Nrr field_name model_namerrrask_not_null_addition8z)MigrationQuestioner.ask_not_null_additioncCr/)"Changing a NULL field to NOT NULL.Nrr1rrrask_not_null_alteration=r5z+MigrationQuestioner.ask_not_null_alterationcC|jddS)Was this field really renamed? ask_renameFr r)rr3old_namenew_namefield_instancerrrr:BzMigrationQuestioner.ask_renamecCr8)Was this model really renamed?ask_rename_modelFr;)rold_model_statenew_model_staterrrrAFr?z$MigrationQuestioner.ask_rename_modelcCr8)z-Do you really want to merge these migrations? ask_mergeFr;rr*rrrrDJr?zMigrationQuestioner.ask_mergecCr/)(Adding an auto_now_add field to a model.Nrr1rrrask_auto_now_add_additionNr5z-MigrationQuestioner.ask_auto_now_add_addition)NNN) __name__ __module__ __qualname____doc__rrr4r7r:rArDrGrrrrr s  rc@sXeZdZdddZddZdddZd d Zd d Zd dZddZ ddZ ddZ dS)InteractiveMigrationQuestionerNcCs\td|}|s|dur|S|r|ddvr&td}|r|ddvs|ddkS)Nz%s rynzPlease answer yes or no: y)inputlower)rquestiondefaultresultrrr_boolean_inputVs  z-InteractiveMigrationQuestioner._boolean_inputcCs~t|t|D]\}}td|d|fqtd} zt|}Wn ty+Ynwd|kr9t|kr:|Std}q)Nz %s) %srzSelect an option: TrzPlease select a valid option: )print enumeraterOint ValueErrorr')rrQchoicesichoicerSvaluerrr _choice_input^s  z,InteractiveMigrationQuestioner._choice_inputc Cstd|r td|tdtd |rd|}nd}t|}|s*|r*|}|s1tdn/|d kr;td n%z t|ittd WStt fy_}z td |WYd }~nd }~wwq)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. z3Please enter the default value now, as valid PythonzUYou can accept the default '{}' by pressing 'Enter' or you can provide another value.z]The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.nowzType 'exit' to exit this promptTz[default: {}] >>> z>>> z;Please enter some code, or 'exit' (with no quotes) to exit.exitr)datetimerzInvalid input: %sN) rUformatrOsysr_evalr`r SyntaxError NameError)rrRpromptcodeerrr _ask_defaultms4   z+InteractiveMigrationQuestioner._ask_defaultcCs@|js|d||fddg}|dkrtddS|SdS)r0zYou 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:bProvide a one-off default now (will be set on all existing rows with a null value for this column)+Quit, and let me add a default in models.pyN)r r]rbr_rirr2r3r[rrrr4s z4InteractiveMigrationQuestioner.ask_not_null_additioncCsL|js$|d||fgd}|dkrtS|dkr tddS|SdS)r6zYou 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:)rjzIgnore 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)rkrlrmN)r r]rrbr_rirnrrrr7s z6InteractiveMigrationQuestioner.ask_not_null_alterationcCs"d}|||||||jjfdS)r9z+Did you rename %s.%s to %s.%s (a %s)? [y/N]F)rT __class__rH)rr3r<r=r>msgrrrr:s z)InteractiveMigrationQuestioner.ask_renamecCs d}|||j|j|jfdS)r@z+Did you rename the %s.%s model to %s? [y/N]F)rTr*name)rrBrCrprrrrAs z/InteractiveMigrationQuestioner.ask_rename_modelcCs |ddS)Nz Merging will only work if the operations printed above do not conflict with each other (working on different fields or models) Do you want to merge these migration branches? [y/N]F)rTrErrrrDsz(InteractiveMigrationQuestioner.ask_mergecCsD|js |d||ddg}|dkrtddS|jddSdS) rFzYou are trying to add the field '{}' with 'auto_now_add=True' to {} without a default; the database needs something to populate existing rows. z@Provide a one-off default now (will be set on all existing rows)rkrlrmz timezone.now)rRN)r r]rarbr_rirnrrrrGs  z8InteractiveMigrationQuestioner.ask_auto_now_add_additionr )r^) rHrIrJrTr]rir4r7r:rArDrGrrrrrLTs  " rLc@s$eZdZddZddZddZdS)!NonInteractiveMigrationQuestionercCtddSNrmrbr_r1rrrr4r?z7NonInteractiveMigrationQuestioner.ask_not_null_additioncCstSr rr1rrrr7sz9NonInteractiveMigrationQuestioner.ask_not_null_alterationcCrsrtrur1rrrrGr?z;NonInteractiveMigrationQuestioner.ask_auto_now_add_additionN)rHrIrJr4r7rGrrrrrrs rr)r`rr"rb django.appsrdjango.db.modelsr django.utilsrloaderrrrLrrrrrrs    G