B
    CVvgŸ!  ã               @   s:  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
 d dlmZmZmZ d dlmZ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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* e e(ƒZ+e e*ƒ ƒZ,e -e¡G dd„ dej.ƒƒZ/e -e¡G dd„ dej.ƒƒZ0dS )é    )Úsettings)ÚadminÚmessages)ÚIS_POPUP_VAR)Úunquote)Úupdate_session_auth_hash)ÚAdminPasswordChangeFormÚUserChangeFormÚUserCreationForm)ÚGroupÚUser)ÚPermissionDenied)ÚrouterÚtransaction)ÚHttp404ÚHttpResponseRedirect)ÚTemplateResponse)ÚpathÚreverse)Úmethod_decorator)Úescape)ÚgettextÚgettext_lazy)Úcsrf_protect)Úsensitive_post_parametersc                   s*   e Zd ZdZdZdZd‡ fdd„	Z‡  ZS )Ú
GroupAdmin)Úname)ÚpermissionsNc                sB   |j dkr*| d|jjj¡}| d¡|d< tƒ j|fd|i|—ŽS )Nr   ZquerysetÚcontent_typeÚrequest)r   ÚgetZremote_fieldÚmodelZobjectsZselect_relatedÚsuperÚformfield_for_manytomany)ÚselfZdb_fieldr   ÚkwargsÚqs)Ú	__class__© ú=/tmp/pip-install-o3oxmrkh/Django/django/contrib/auth/admin.pyr#      s    
z#GroupAdmin.formfield_for_manytomany)N)Ú__name__Ú
__module__Ú__qualname__Úsearch_fieldsÚorderingÚfilter_horizontalr#   Ú__classcell__r(   r(   )r'   r)   r      s   r   c                   sð   e Zd ZdZdZdddifedƒddifedƒddifed	ƒdd
iffZddddœffZeZ	e
ZeZdZdZdZdZdZd$‡ fdd„	Zd%‡ fdd„	Z‡ fdd„Z‡ fdd„Zeed&dd„ƒƒZd'‡ fdd„	Zed(d d!„ƒZd)‡ fd"d#„	Z‡  ZS )*Ú	UserAdminzadmin/auth/user/add_form.htmlNÚfields)ÚusernameÚpasswordzPersonal info)Ú
first_nameÚ	last_nameÚemailZPermissions)Ú	is_activeÚis_staffÚis_superuserÚgroupsÚuser_permissionszImportant dates)Z
last_loginZdate_joined)Zwide)r3   Z	password1Z	password2)Úclassesr2   )r3   r7   r5   r6   r9   )r9   r:   r8   r;   )r3   r5   r6   r7   )r3   )r;   r<   c                s   |s
| j S tƒ  ||¡S )N)Úadd_fieldsetsr"   Úget_fieldsets)r$   r   Úobj)r'   r(   r)   r?   C   s    zUserAdmin.get_fieldsetsc                s2   i }|dkr| j |d< | |¡ tƒ j||f|ŽS )z7
        Use special form during user creation
        NÚform)Úadd_formÚupdater"   Úget_form)r$   r   r@   r%   Údefaults)r'   r(   r)   rD   H   s
    

zUserAdmin.get_formc                s$   t d| j | j¡dd�gtƒ  ¡  S )Nz<id>/password/Zauth_user_password_change)r   )r   Ú
admin_siteZ
admin_viewÚuser_change_passwordr"   Úget_urls)r$   )r'   r(   r)   rH   R   s
    zUserAdmin.get_urlsc                s   |  d¡ otƒ  ||¡S )Nr4   )Ú
startswithr"   Úlookup_allowed)r$   ÚlookupÚvalue)r'   r(   r)   rJ   [   s    zUserAdmin.lookup_allowedÚ c          	   C   s.   t jt | j¡d�� |  |||¡S Q R X d S )N)Zusing)r   Zatomicr   Zdb_for_writer!   Ú	_add_view)r$   r   Úform_urlÚextra_contextr(   r(   r)   Úadd_view_   s    zUserAdmin.add_viewc                sj   |   |¡s&|  |¡r"tjr"tdƒ‚t‚|d kr2i }| jj | jj	¡}d|j
dœ}| |¡ tƒ  |||¡S )Nz¯Your user does not have the "Change user" permission. In order to add users, Django requires that your user account have both the "Add user" and "Change user" permissions set.r(   )Zauto_populated_fieldsZusername_help_text)Úhas_change_permissionZhas_add_permissionr   ÚDEBUGr   r   r!   Ú_metaÚ	get_fieldZUSERNAME_FIELDÚ	help_textrC   r"   rQ   )r$   r   rO   rP   Zusername_fieldrE   )r'   r(   r)   rN   e   s    


zUserAdmin._add_viewc             C   sx  |   |t|ƒ¡}|  ||¡s t‚|d krHttdƒ| jjjt	|ƒdœ ƒ‚|j
dkrØ|  ||j¡}| ¡ râ| ¡  |  ||d ¡}|  |||¡ tdƒ}t ||¡ t||jƒ ttd| jj|jj|jjf |jfd�ƒS n
|  |¡}d dt|jƒifg}tj  !||i ¡}	tdƒt	| "¡ ƒ |	||t#|jk�p2t#|j$kd	d
d
d	d
| jj|d
d	dœ| j %|¡–}
| jj|_&t'|| j(�prd|
ƒS )Nz8%(name)s object with primary key %(key)r does not exist.)r   ÚkeyÚPOSTzPassword changed successfully.z%s:%s_%s_change)Úargsr2   zChange password: %sTF)ÚtitleÚ	adminFormrO   rA   Zis_popupÚaddZchangeZhas_delete_permissionrR   Zhas_absolute_urlÚoptsÚoriginalZsave_asZ	show_savez$admin/auth/user/change_password.html))Z
get_objectr   rR   r   r   Ú_r!   rT   Zverbose_namer   ÚmethodÚchange_password_formrX   Zis_validÚsaveZconstruct_change_messageZ
log_changer   r   Úsuccessr   Úuserr   r   rF   r   Z	app_labelZ
model_nameÚpkÚlistZbase_fieldsr   ÚhelpersZ	AdminFormZget_usernamer   ÚGETZeach_contextZcurrent_appr   Úchange_user_password_template)r$   r   ÚidrO   rd   rA   Zchange_messageÚmsgÚ	fieldsetsr[   Úcontextr(   r(   r)   rG   €   s`    



zUserAdmin.user_change_passwordc                s:   d|j kr*t|j kr*|j  ¡ |_ d|j d< tƒ  |||¡S )zÒ
        Determine the HttpResponse for the add_view stage. It mostly defers to
        its superclass implementation but is customized because the User model
        has a slightly different workflow.
        Z_addanotheré   Z	_continue)rX   r   Úcopyr"   Úresponse_add)r$   r   r@   Zpost_url_continue)r'   r(   r)   rp   ¿   s    
zUserAdmin.response_add)N)N)rM   N)rM   N)rM   )N)r*   r+   r,   Zadd_form_templateri   r_   rl   r>   r	   rA   r
   rB   r   ra   Zlist_displayZlist_filterr-   r.   r/   r?   rD   rH   rJ   Úsensitive_post_parameters_mÚcsrf_protect_mrQ   rN   rG   rp   r0   r(   r(   )r'   r)   r1   (   s:   

	>r1   N)1Zdjango.confr   Zdjango.contribr   r   Zdjango.contrib.admin.optionsr   Zdjango.contrib.admin.utilsr   Zdjango.contrib.authr   Zdjango.contrib.auth.formsr   r	   r
   Zdjango.contrib.auth.modelsr   r   Zdjango.core.exceptionsr   Z	django.dbr   r   Zdjango.httpr   r   Zdjango.template.responser   Zdjango.urlsr   r   Zdjango.utils.decoratorsr   Zdjango.utils.htmlr   Zdjango.utils.translationr   r   r_   Zdjango.views.decorators.csrfr   Zdjango.views.decorators.debugr   rr   rq   ÚregisterZ
ModelAdminr   r1   r(   r(   r(   r)   Ú<module>   s,   

