o
    <g5                     @   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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connections)cached_property)capfirstc                   @   s   e Zd ZdS )NotRunningInTTYExceptionN)__name__
__module____qualname__ r   r   j/var/www/html/venv/lib/python3.10/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ttdd | jjD ]5}| jj|}|j	rR|j
jrD|j
jjjsDtd| |j d| dd| fd q(|j d| d| d q(d S )Nz--%sz&Specifies the login for the superuser.)helpz	--noinputz
--no-inputstore_falseinteractivezTells 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".)defaultchoicesr"   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   tupler	   REQUIRED_FIELDSr   r   many_to_manyremote_fieldthroughauto_createdr   )r   parser
field_namefieldr   r   r   add_arguments&   sX   

	zCommand.add_argumentsc                    s"   | dtj| _t j|i |S )Nr   )getsysr   r   execute)r   r   optionsr    r   r   r7   X   s   zCommand.executec              
   O   s:  || j j }|d }i }| jj}z	| j jt W n
 tjy#   Y nw d |t< z8|d ri }t	| j
dr>| j
 s>tt|d}|rW| |||}	|	rV| j|	 d }n|dkrctdt| |d u r| | j|}
| | j|
|}|r| |||}	|	r| j|	 d }qc|d u sg||| j j< | jjr| jj|n||| j j< | j jD ]s}| j j|}|| ||< || d ur||| d ||< || d u r| |}
| ||
}|||< |jr|r| sd ||< | jd qdd	 |d
D ||< || d u s|js|| ||< |jr|j|| ||< qt|v r|t d u rt }td}||kr?| jd q| dkrN| jd qzt|| j di | W n0 tjy } z"| jd |j! t"d}|# dkrW Y d }~qW Y d }~nd }~ww ||t< t|v r|t d u s)nt|v rdt$j%v rt$j%d |t< |d u rt$j%&d| j j'  }|d u rtd| j j | |||}	|	rt|	||| j j< | j jD ]\}d|'  }|| pt$j%&|}| j j|}|s|j(r|| dkst$j%&|dkrqtd| ||d ||< |jr@t)|| t*r@dd	 || d
D ||< q| j j+,|j-di | |d dkr_| j.d W d S W d S  t/yw   | jd t01d Y d S  tjy } ztd |j!d }~w ty   | j.d Y d S w )Ndatabaser$   isatty)r9    %s cannot be blank.z"Error: This field cannot be blank.c                 S      g | ]}|  qS r   strip.0pkr   r   r   
<listcomp>       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]: yDJANGO_SUPERUSER_PASSWORDDJANGO_SUPERUSER_z!You must use --%s with --noinput.c                 S   r=   r   r>   r@   r   r   r   rC      rD   	verbosity   zSuperuser created successfully.z
Operation cancelled.; zSuperuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.r   )2r   r   r   verbose_namer   r   PASSWORD_FIELDr   FieldDoesNotExisthasattrr   r:   r   r   _validate_usernamestderrwriter   r   _get_input_messageget_input_datar.   modelr,   cleanr-   r?   splitmany_to_onegetpassr   ValidationErrorjoinmessagesinputlowerosenvironr5   upperblank
isinstancestr_default_manager
db_managercreate_superuserstdoutKeyboardInterruptr6   exit)r   r   r8   usernamer9   	user_dataverbose_field_namefake_user_datadefault_username	error_msgmessager2   r3   input_valuer   	password2errresponseenv_varvalueer   r   r   handle\   s.  











 

zCommand.handleNc              
   C   sp   t |}|r|dkr|}z	||d}W |S  tjy7 } z| jdd|j  d}W Y d}~|S d}~ww )zm
        Override this method if you want to customize data inputs or
        validation exceptions.
        r;   Nz	Error: %srL   )r^   rW   r   r[   rR   rS   r\   r]   )r   r3   rr   r'   	raw_valuevalry   r   r   r   rU      s   zCommand.get_input_datac                 C   sP   dt |j|rd| nd|jr$d|jjjj|jr| n|jjf f S df S )Nz%s%s%s: z (leave blank to use '%s')r;   z (%s.%s))	r   rM   r.   rV   r   object_namer-   m2m_target_field_namer2   )r   r3   r'   r   r   r   rT     s   

zCommand._get_input_messagec                    s(    j jrdS t fdd jjjD S )NTc                 3   s0    | ]}t |jd ko|jd  jjkV  qdS )rK   r   N)lenfieldsr   name)rA   unique_constraintr   r   r   	<genexpr>'  s    
z-Command.username_is_unique.<locals>.<genexpr>)r   uniqueanyr   r   total_unique_constraintsr   r   r   r   username_is_unique#  s
   zCommand.username_is_uniquec              
   C   s   | j rz| jj|| W d| S  | jjy   Y nw |s&dt| S z
| j|d W dS  t	j
yI } zd|jW  Y d}~S d}~ww )z=Validate username. If invalid, return a string error message.z Error: That %s is already taken.r<   NrL   )r   r   rf   rg   get_by_natural_keyDoesNotExistr   r   rW   r   r[   r\   r]   )r   rl   rn   r9   ry   r   r   r   rQ   -  s"   zCommand._validate_usernamer   )r   r   r   r"   requires_migrations_checksstealth_optionsr   r4   r7   rz   rU   rT   r
   r   rQ   __classcell__r   r   r    r   r      s    2 
%

	r   )__doc__rZ   r`   r6   django.contrib.authr   django.contrib.auth.managementr   'django.contrib.auth.password_validationr   django.corer   django.core.management.baser   r   	django.dbr   r	   django.utils.functionalr
   django.utils.textr   	Exceptionr   rN   r   r   r   r   r   <module>   s    