a
    Kcòf–3  ã                   @   s¨   d 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mZ ddlmZ dd	lmZ dd
lmZ G dd„ deƒZdZG dd„ deƒZdS )z*
Management utility to create superusers.
é    N)Úget_user_model)Úget_default_username)Úvalidate_password)Ú
exceptions)ÚBaseCommandÚCommandError)ÚDEFAULT_DB_ALIAS)Úcached_property)Úcapfirstc                   @   s   e Zd ZdS )ÚNotRunningInTTYExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   út/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/contrib/auth/management/commands/createsuperuser.pyr      s   r   Úpasswordc                       sl   e Zd ZdZdZdZ‡ fdd„Zdd„ Z‡ fdd	„Zd
d„ Z	ddd„Z
ddd„Zedd„ ƒZdd„ Z‡  ZS )ÚCommandzUsed to create a superuser.T)Ústdinc                    s2   t ƒ j|i |¤Ž tƒ | _| jj | jj¡| _d S )N)ÚsuperÚ__init__r   Ú	UserModelÚ_metaÚ	get_fieldÚUSERNAME_FIELDÚusername_field)ÚselfÚargsÚkwargs©Ú	__class__r   r   r      s
    ÿzCommand.__init__c                 C   s¼   |j d| jj dd� |j ddddd| jj d	� |j d
tdd� | jjD ]l}| jj |¡}|jr |jj	r„|jj	jj
s„td| ƒ‚q¶|j d| dd| fd� qJ|j d| d| d� qJd S )Nz--%sz&Specifies the login for the superuser.)Úhelpz	--noinputz
--no-inputÚstore_falseÚinteractivezòTells Django to NOT prompt the user for input of any kind. You must use --%s with --noinput, along with an option for any other required field. Superusers created with --noinput will not be able to log in until they're given a valid password.)ÚactionÚdestr    z
--databasez4Specifies the database to use. Default is "default".)Údefaultr    z\Required field '%s' specifies a many-to-many relation through model, which is not supported.Úappendz?Specifies the %s for the superuser. Can be used multiple times.)r#   r    z#Specifies the %s for the superuser.)Úadd_argumentr   r   r   ÚREQUIRED_FIELDSr   r   Úmany_to_manyÚremote_fieldZthroughZauto_createdr   )r   ÚparserÚ
field_nameÚfieldr   r   r   Úadd_arguments%   sT    
þüúýÿ
þÿÿÿÿý	þzCommand.add_argumentsc                    s"   |  dtj¡| _tƒ j|i |¤ŽS )Nr   )ÚgetÚsysr   r   Úexecute)r   r   Úoptionsr   r   r   r1   V   s    zCommand.executec              
   O   s  || j j }|d }i }| jj}z| j j t¡ W n tjyF   Y n
0 d |t< �z.|d �r*i }t	| j
dƒr|| j
 ¡ s|t‚t|d�}|r®|  |||¡}	|	rÆ| j |	¡ d }n|dkrÆtdt|ƒ ƒ‚|d u �r|  | j|¡}
|  | j|
|¡}|rÆ|  |||¡}	|	rÆ| j |	¡ d }qÆqÆ||| j j< | jj�r<| jj |¡n||| j j< | j jD ]ê}| j j |¡}|| ||< || d u�r�| || d ¡||< || d u �r|  |¡}
|  ||
¡}|||< |j�r�|�r�| ¡ �sìd ||< | j d¡ �q�dd	„ | d
¡D ƒ||< �q�|j�s|| ||< |j�rP|j || ¡||< �qPt|v �rJ|t d u �rJt ¡ }t d¡}||k�r€| j d¡ �q<| ¡ dk�rž| j d¡ �q<zt|| j f i |¤Žƒ W n` tj�y } zD| j d  |j!¡¡ t"dƒ}| #¡ dk�rW Y d }~�q<W Y d }~n
d }~0 0 ||t< �q<�n t|v �rNdt$j%v �rNt$j%d |t< |d u �rpt$j% &d| j j '¡  ¡}|d u �rŒtd| j j ƒ‚n|  |||¡}	|	�r¨t|	ƒ‚||| j j< | j jD ]Œ}d| '¡  }|| �pàt$j% &|¡}|�sôtd| ƒ‚| j j |¡}| |d ¡||< |j�r¼t(|| t)ƒ�r¼dd	„ ||  d
¡D ƒ||< �q¼| j j* +|¡j,f i |¤Ž |d dk�r~| j- d¡ W n~ t.�yª   | j d¡ t/ 0d¡ Y nV tj�yà } ztd  |j!¡ƒ‚W Y d }~n(d }~0  t�yþ   | j- d¡ Y n0 d S )NÚdatabaser"   Úisatty)r3   Ú ú%s cannot be blank.z"Error: This field cannot be blank.c                 S   s   g | ]}|  ¡ ‘qS r   ©Ústrip©Ú.0Úpkr   r   r   Ú
<listcomp>ž   s   z"Command.handle.<locals>.<listcomp>ú,zPassword (again): z#Error: Your passwords didn't match.z&Error: Blank passwords aren't allowed.Ú
z:Bypass password validation and create user anyway? [y/N]: ÚyZDJANGO_SUPERUSER_PASSWORDZDJANGO_SUPERUSER_z!You must use --%s with --noinput.c                 S   s   g | ]}|  ¡ ‘qS r   r7   r9   r   r   r   r<   å   s   Ú	verbosityé   zSuperuser created successfully.z
Operation cancelled.ú; z‡Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.)1r   r   r   Úverbose_namer   r   ÚPASSWORD_FIELDr   ZFieldDoesNotExistÚhasattrr   r4   r   r   Ú_validate_usernameÚstderrÚwriter   r
   Ú_get_input_messageÚget_input_datar*   Úmodelr(   Úcleanr)   r8   ÚsplitZmany_to_oneÚgetpassr   ÚValidationErrorÚjoinÚmessagesÚinputÚlowerÚosÚenvironr/   ÚupperÚ
isinstanceÚstrÚ_default_managerÚ
db_managerZcreate_superuserÚstdoutÚKeyboardInterruptr0   Úexit)r   r   r2   Úusernamer3   Ú	user_dataÚverbose_field_nameZfake_user_dataZdefault_usernameÚ	error_msgÚmessager,   r-   Zinput_valuer   Z	password2ÚerrÚresponseÚenv_varÚvalueÚer   r   r   ÚhandleZ   s   

ÿ
ÿ
ÿÿÿÿý


ÿÿ

ÿ$ÿþ
ÿ
ÿÿÿÿÿÿ$ÿzCommand.handleNc              
   C   sp   t |ƒ}|r|dkr|}z| |d¡}W nB tjyj } z(| j dd |j¡ ¡ d}W Y d}~n
d}~0 0 |S )zm
        Override this method if you want to customize data inputs or
        validation exceptions.
        r5   Nz	Error: %srB   )rR   rL   r   rO   rG   rH   rP   rQ   )r   r-   rb   r%   Z	raw_valueÚvalrg   r   r   r   rJ   ú   s    zCommand.get_input_datac                 C   sL   dt |jƒ|rd| nd|jrDd|jjjj|jr8| ¡ n|jjf ndf S )Nz%s%s%s: z (leave blank to use '%s')r5   z (%s.%s))	r
   rC   r*   rK   r   Zobject_namer)   Zm2m_target_field_namer,   )r   r-   r%   r   r   r   rI   
  s    ù
ÿ
üÿõzCommand._get_input_messagec                    s(   ˆ j jrdS t‡ fdd„ˆ jjjD ƒƒS )NTc                 3   s.   | ]&}t |jƒd ko$|jd ˆ jjkV  qdS )rA   r   N)ÚlenÚfieldsr   Úname)r:   Zunique_constraint©r   r   r   Ú	<genexpr>  s   þz-Command.username_is_unique.<locals>.<genexpr>)r   ÚuniqueÚanyr   r   Ztotal_unique_constraintsrm   r   rm   r   Úusername_is_unique  s
    ýzCommand.username_is_uniquec              
   C   s˜   | j r>z| jj |¡ |¡ W n | jjy4   Y n
0 d| S |sNdt|ƒ S z| j |d¡ W n2 t	j
y’ } zd |j¡W  Y d}~S d}~0 0 dS )z=Validate username. If invalid, return a string error message.z Error: That %s is already taken.r6   NrB   )rq   r   rY   rZ   Zget_by_natural_keyZDoesNotExistr
   r   rL   r   rO   rP   rQ   )r   r^   r`   r3   rg   r   r   r   rF   #  s    ÿzCommand._validate_username)N)N)r   r   r   r    Zrequires_migrations_checksZstealth_optionsr   r.   r1   rh   rJ   rI   r	   rq   rF   Ú__classcell__r   r   r   r   r      s   1 !


	r   )Ú__doc__rN   rT   r0   Zdjango.contrib.authr   Zdjango.contrib.auth.managementr   Z'django.contrib.auth.password_validationr   Zdjango.corer   Zdjango.core.management.baser   r   Z	django.dbr   Zdjango.utils.functionalr	   Zdjango.utils.textr
   Ú	Exceptionr   rD   r   r   r   r   r   Ú<module>   s   