3
W­E^Ÿ!  ã               @   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j-eƒG dd„ dej.ƒƒZ/ej-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*|jd|j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-build-78s3knkw/Django/django/contrib/auth/admin.pyr#      s    
z#GroupAdmin.formfield_for_manytomany)r   )r   )r   )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d0Zd1Zd2Zd3‡ fdd„	Zd4‡ fdd„	Z‡ fdd„Z‡ fdd„Zeed5d d!„ƒƒZd6‡ fd"d#„	Zed7d$d%„ƒZd8‡ fd&d'„	Z‡  ZS )9Ú	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Ú
last_loginÚdate_joinedÚwideÚ	password1Ú	password2)Úclassesr2   c                s   |s
| j S tƒ j||ƒS )N)Úadd_fieldsetsr"   Úget_fieldsets)r$   r   Úobj)r'   r(   r)   rD   C   s    zUserAdmin.get_fieldsetsc                s2   i }|dkr| j |d< |j|ƒ tƒ j||f|ŽS )z7
        Use special form during user creation
        NÚform)Úadd_formÚupdater"   Úget_form)r$   r   rE   r%   Údefaults)r'   r(   r)   rI   H   s
    

zUserAdmin.get_formc                s$   t d| jj| jƒdd�gtƒ jƒ  S )Nz<id>/password/Zauth_user_password_change)r   )r   Ú
admin_siteZ
admin_viewÚuser_change_passwordr"   Úget_urls)r$   )r'   r(   r)   rM   R   s
    zUserAdmin.get_urlsc                s   |j dƒ otƒ j||ƒS )Nr4   )Ú
startswithr"   Úlookup_allowed)r$   ÚlookupÚvalue)r'   r(   r)   rO   [   s    zUserAdmin.lookup_allowedÚ c             C   s.   t jtj| jƒd�� | j|||ƒ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   | j |ƒs&| j|ƒr"tjr"tdƒ‚t‚|d kr2i }| jjj| jj	ƒ}f |j
dœ}|j|ƒ tƒ j|||ƒ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.)Zauto_populated_fieldsZusername_help_text)Úhas_change_permissionZhas_add_permissionr   ÚDEBUGr   r   r!   Ú_metaÚ	get_fieldZUSERNAME_FIELDÚ	help_textrH   r"   rV   )r$   r   rT   rU   Zusername_fieldrJ   )r'   r(   r)   rS   e   s    


zUserAdmin._add_viewc             C   sx  | j |t|ƒƒ}| j||ƒs t‚|d krHttdƒ| jjjt	|ƒdœ ƒ‚|j
dkrØ| j||jƒ}|jƒ râ|jƒ  | j||d ƒ}| j|||ƒ tdƒ}tj||ƒ t||jƒ ttd| jj|jj|jjf |jfd�ƒS n
| j|ƒ}d dt|jƒifg}tj j!||i ƒ}	tdƒt	|j"ƒ ƒ |	||t#|jk�p2t#|j$kd	d
d
d	d
| jj|d
d	dœ| j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Ú	adminFormrT   rF   Zis_popupÚaddZchangeZhas_delete_permissionrW   Zhas_absolute_urlÚoptsÚoriginalZsave_asZ	show_savez$admin/auth/user/change_password.html))Z
get_objectr   rW   r   r   Ú_r!   rY   Zverbose_namer   ÚmethodÚchange_password_formr]   Zis_validÚsaveZconstruct_change_messageZ
log_changer   r   Úsuccessr   Úuserr   r   rK   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   ÚidrT   ri   rF   Zchange_messageÚmsgÚ	fieldsetsr`   Úcontextr(   r(   r)   rL   €   s`    



zUserAdmin.user_change_passwordc                s:   d|j kr*t|j kr*|j jƒ |_ d|j d< tƒ j|||ƒ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)r]   r   Úcopyr"   Úresponse_add)r$   r   rE   Zpost_url_continue)r'   r(   r)   ru   ¿   s    
zUserAdmin.response_add)r3   r4   )r5   r6   r7   )r8   r9   r:   r;   r<   )r=   r>   )r?   )r3   r@   rA   )r3   r7   r5   r6   r9   )r9   r:   r8   r;   )r3   r5   r6   r7   )r3   )r;   r<   )N)N)rR   N)rR   N)rR   )N)r*   r+   r,   Zadd_form_templatern   rd   rq   rC   r	   rF   r
   rG   r   rf   Zlist_displayZlist_filterr-   r.   r/   rD   rI   rM   rO   Úsensitive_post_parameters_mÚcsrf_protect_mrV   rS   rL   ru   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   rd   Zdjango.views.decorators.csrfr   Zdjango.views.decorators.debugr   rw   rv   ÚregisterZ
ModelAdminr   r1   r(   r(   r(   r)   Ú<module>   s,   

