B
    CVvgÁ,  ã               @   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 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)Úcapfirstc               @   s   e Zd ZdS )ÚNotRunningInTTYExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú[/tmp/pip-install-o3oxmrkh/Django/django/contrib/auth/management/commands/createsuperuser.pyr
      s   r
   Úpasswordc                   s`   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dd„ Z‡  ZS )ÚCommandzUsed to create a superuser.T)Ústdinc                s.   t ƒ j||Ž 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� xx| 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� qL|j d| d| d� qLW d 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"   s2    

zCommand.add_argumentsc                s   |  dtj¡| _tƒ j||ŽS )Nr   )ÚgetÚsysr   r   Úexecute)r   r   Úoptions)r   r   r   r/   M   s    zCommand.executec          
   O   s¨  || j j }|d }i }| jj}y| j j t¡ W n tjk
rH   Y n
X d |t< �yÎ|d �r i }t	| j
dƒr~| j
 ¡ s~t‚t|d�}|r°|  |||¡}	|	rÈ| j |	¡ d }n|dkrÈtdt|ƒ ƒ‚xT|d k�r|  | j|¡}
|  | j|
|¡}|rÊ|  |||¡}	|	rÊ| j |	¡ d }qÊqÊW ||| j j< | jj�rB| jj |¡n||| j j< xÐ| j jD ]Ä}| j j |¡}|| ||< x¢|| d k�r|  |¡}
|  ||
¡}|||< |j�rì|�rì| ¡ �sÔd ||< | j d¡ �qxdd	„ | d
¡D ƒ||< |j�sü|||< |j�rx|j |¡||< �qxW �qXW xÜt|k�rü|t d k�rüt ¡ }t d¡}||k�rf| j d¡ �q"| ¡ dk�r„| j d¡ �q"yt|| j f |Žƒ W nT tjk
�rî } z2| j d |j ¡¡ t!dƒ}| "¡ dk�rÞ�w"W d d }~X Y nX ||t< �q"W nðt|k�r$dt#j$k�r$t#j$d |t< |d k�rFt#j$ %d| j j &¡  ¡}|d k�rbtd| j j ƒ‚n|  |||¡}	|	�r~t|	ƒ‚||| j j< xd| j jD ]X}d| &¡  }|| �p¸t#j$ %|¡}|�sÌtd| ƒ‚| j j |¡}| '|d ¡||< �q”W | j j( )|¡j*f |Ž |d dk�r | j+ d¡ W n€ t,k
�rN   | j d¡ t- .d¡ Y nV tjk
�r‚ } ztd |j ¡ƒ‚W d d }~X Y n" tk
�r¢   | j+ d¡ Y nX d S )NÚdatabaser    Úisatty)r1   Ú z%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.Ú	verbosityé   zSuperuser created successfully.z
Operation cancelled.z; z‡Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.)/r   r   r   Úverbose_namer   r   ÚPASSWORD_FIELDr   ZFieldDoesNotExistÚhasattrr   r2   r
   r   Ú_validate_usernameÚstderrÚwriter   r	   Ú_get_input_messageÚget_input_datar(   Úmodelr&   r'   r4   ÚsplitZmany_to_oneÚgetpassr   ÚValidationErrorÚjoinÚmessagesÚinputÚlowerÚosÚenvironr-   ÚupperÚcleanÚ_default_managerÚ
db_managerZcreate_superuserÚstdoutÚKeyboardInterruptr.   Úexit)r   r   r0   Úusernamer1   Z	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   ÚhandleQ   s¾    

$





 zCommand.handleNc          
   C   sn   t |ƒ}|r|dkr|}y| |d¡}W n@ tjk
rh } z | j dd |j¡ ¡ d}W dd}~X Y nX |S )zm
        Override this method if you want to customize data inputs or
        validation exceptions.
        r3   Nz	Error: %sz; )rK   rP   r   rH   rA   rB   rI   rJ   )r   r+   rY   r#   Z	raw_valueÚvalr^   r   r   r   rD   Ì   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')r3   z (%s.%s))	r	   r=   r(   rE   r   Zobject_namer'   Zm2m_target_field_namer*   )r   r+   r#   r   r   r   rC   Ü   s    zCommand._get_input_messagec          
   C   s–   | j jrBy| jj |¡ |¡ W n | jjk
r8   Y n
X d| S |sRdt|ƒ S y| j  |d¡ W n, t	j
k
r� } zd |j¡S d}~X Y nX dS )z=Validate username. If invalid, return a string error message.z Error: That %s is already taken.z%s cannot be blank.Nz; )r   Úuniquer   rQ   rR   Zget_by_natural_keyZDoesNotExistr	   rP   r   rH   rI   rJ   )r   rV   rW   r1   r^   r   r   r   r@   æ   s    zCommand._validate_username)N)N)r   r   r   r   Zrequires_migrations_checksZstealth_optionsr   r,   r/   r_   rD   rC   r@   Ú__classcell__r   r   )r   r   r      s   +{


r   )Ú__doc__rG   rM   r.   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.textr	   Ú	Exceptionr
   r>   r   r   r   r   r   Ú<module>   s   