B
    CVvgš4  ã               @   sÞ  d dl mZmZ d dlmZ d dlmZmZmZ	m
ZmZ d dlmZ d dlmZmZ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 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, d dl-m.Z. d dl/m0Z0 d dl1m2Z2 eƒ Z3G dd„ dƒZ4G dd„ de4e2ƒZ5G dd„ de4e0ƒZ6d.dd„Z7defdd„Z8G dd „ d ƒZ9G d!d"„ d"e9e2ƒZ:d#Z;G d$d%„ d%e9e0ƒZ<G d&d'„ d'e9e2ƒZ=G d(d)„ d)e9e0ƒZ>G d*d+„ d+e9e2ƒZ?G d,d-„ d-e9e0ƒZ@dS )/é    )ÚurlparseÚ
urlunparse)Úsettings)ÚREDIRECT_FIELD_NAMEÚget_user_modelÚloginÚlogoutÚupdate_session_auth_hash)Úlogin_required)ÚAuthenticationFormÚPasswordChangeFormÚPasswordResetFormÚSetPasswordForm)Údefault_token_generator)Úget_current_site)ÚValidationError)ÚHttpResponseRedirectÚ	QueryDict)Úresolve_url)Úreverse_lazy)Úmethod_decorator)Úurl_has_allowed_host_and_schemeÚurlsafe_base64_decode)Úgettext_lazy)Únever_cache)Úcsrf_protect)Úsensitive_post_parameters)ÚTemplateView)ÚFormViewc               @   s   e Zd Zeƒ Zdd„ ZdS )ÚSuccessURLAllowedHostsMixinc             C   s   | j  ¡ h| j™S )N)ÚrequestÚget_hostÚsuccess_url_allowed_hosts)Úself© r$   ú=/tmp/pip-install-o3oxmrkh/Django/django/contrib/auth/views.pyÚget_success_url_allowed_hosts$   s    z9SuccessURLAllowedHostsMixin.get_success_url_allowed_hostsN)Ú__name__Ú
__module__Ú__qualname__Úsetr"   r&   r$   r$   r$   r%   r   !   s   r   c                   sŠ   e Zd ZdZeZdZeZdZ	dZ
dZeeƒ ƒeeƒeeƒ‡ fdd„ƒƒƒZdd„ Zd	d
„ Zdd„ Z‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )Ú	LoginViewz=
    Display the login form and handle the login action.
    Nzregistration/login.htmlFc                sH   | j r4| jjjr4|  ¡ }|| jjkr,tdƒ‚t|ƒS tƒ j	|f|ž|ŽS )NzsRedirection loop for authenticated user detected. Check that your LOGIN_REDIRECT_URL doesn't point to a login page.)
Úredirect_authenticated_userr    ÚuserÚis_authenticatedÚget_success_urlÚpathÚ
ValueErrorr   ÚsuperÚdispatch)r#   r    ÚargsÚkwargsÚredirect_to)Ú	__class__r$   r%   r3   3   s    zLoginView.dispatchc             C   s   |   ¡ }|pttjƒS )N)Úget_redirect_urlr   r   ZLOGIN_REDIRECT_URL)r#   Úurlr$   r$   r%   r/   A   s    zLoginView.get_success_urlc             C   sD   | j j | j| j j | jd¡¡}t||  ¡ | j  ¡ d�}|r@|S dS )z6Return the user-originating redirect URL if it's safe.Ú )r9   Úallowed_hostsÚrequire_https)r    ÚPOSTÚgetÚredirect_field_nameÚGETr   r&   Ú	is_secure)r#   r6   Úurl_is_safer$   r$   r%   r8   E   s    zLoginView.get_redirect_urlc             C   s   | j p
| jS )N)Úauthentication_formÚ
form_class)r#   r$   r$   r%   Úget_form_classR   s    zLoginView.get_form_classc                s   t ƒ  ¡ }| j|d< |S )Nr    )r2   Úget_form_kwargsr    )r#   r5   )r7   r$   r%   rF   U   s    

zLoginView.get_form_kwargsc             C   s   t | j| ¡ ƒ t|  ¡ ƒS )z)Security check complete. Log the user in.)Ú
auth_loginr    Úget_userr   r/   )r#   Úformr$   r$   r%   Ú
form_validZ   s    zLoginView.form_validc                sD   t ƒ jf |Ž}t| jƒ}| | j|  ¡ d|d|ji| jp:i –¡ |S )NÚsiteÚ	site_name)	r2   Úget_context_datar   r    Úupdater?   r8   ÚnameÚextra_context)r#   r5   ÚcontextÚcurrent_site)r7   r$   r%   rM   _   s    

zLoginView.get_context_data)r'   r(   r)   Ú__doc__r   rD   rC   r   r?   Útemplate_namer,   rP   r   r   r   r   r3   r/   r8   rE   rF   rJ   rM   Ú__classcell__r$   r$   )r7   r%   r+   (   s    r+   c                   sT   e Zd ZdZdZeZdZdZe	e
ƒ‡ fdd„ƒZdd„ Zdd	„ Z‡ fd
d„Z‡  ZS )Ú
LogoutViewzH
    Log out the user and display the 'You are logged out' message.
    Nzregistration/logged_out.htmlc                s0   t |ƒ |  ¡ }|rt|ƒS tƒ j|f|ž|ŽS )N)Úauth_logoutÚget_next_pager   r2   r3   )r#   r    r4   r5   Ú	next_page)r7   r$   r%   r3   t   s
    zLogoutView.dispatchc             O   s   | j |f|ž|ŽS )zLogout may be done via POST.)r>   )r#   r    r4   r5   r$   r$   r%   Úpost}   s    zLogoutView.postc             C   s�   | j d k	rt| j ƒ}ntjr(ttjƒ}n| j }| j| jjksJ| j| jjkrŒ| jj | j| jj | j¡¡}t	||  
¡ | j ¡ d�}|sŒ| jj}|S )N)r9   r;   r<   )rY   r   r   ZLOGOUT_REDIRECT_URLr?   r    r=   r@   r>   r   r&   rA   r0   )r#   rY   rB   r$   r$   r%   rX   �   s"    
zLogoutView.get_next_pagec                s>   t ƒ jf |Ž}t| jƒ}| ||jtdƒdœ| jp4i –¡ |S )Nz
Logged out)rK   rL   Útitle)r2   rM   r   r    rN   rO   Ú_rP   )r#   r5   rQ   rR   )r7   r$   r%   rM   š   s    

zLogoutView.get_context_data)r'   r(   r)   rS   rY   r   r?   rT   rP   r   r   r3   rZ   rX   rM   rU   r$   r$   )r7   r%   rV   k   s   	rV   Nc             C   s   t |p
tjƒ}tj|d�| ƒS )zR
    Log out the user if they are logged in. Then redirect to the login page.
    )rY   )r   r   Ú	LOGIN_URLrV   Zas_view)r    Ú	login_urlr$   r$   r%   Úlogout_then_login¦   s    r_   c             C   sR   t |p
tjƒ}tt|ƒƒ}|rFt|d dd�}| ||< |jdd�|d< tt|ƒƒS )zM
    Redirect the user to the login page, passing the given 'next' page.
    é   T)Zmutableú/)Úsafe)	r   r   r]   Úlistr   r   Ú	urlencoder   r   )Únextr^   r?   Zresolved_urlZlogin_url_partsZquerystringr$   r$   r%   Úredirect_to_login®   s    rf   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚPasswordContextMixinNc                s,   t ƒ jf |Ž}| d| ji| jp"i –¡ |S )Nr[   )r2   rM   rN   r[   rP   )r#   r5   rQ   )r7   r$   r%   rM   Ç   s
    z%PasswordContextMixin.get_context_data)r'   r(   r)   rP   rM   rU   r$   r$   )r7   r%   rg   Ä   s   rg   c                   s`   e Zd ZdZdZeZdZdZdZ	e
dƒZdZedƒZeZeeƒ‡ fdd„ƒZ‡ fd	d
„Z‡  ZS )ÚPasswordResetViewz&registration/password_reset_email.htmlNz'registration/password_reset_subject.txtZpassword_reset_donez%registration/password_reset_form.htmlzPassword resetc                s   t ƒ j||ŽS )N)r2   r3   )r#   r4   r5   )r7   r$   r%   r3   Ü   s    zPasswordResetView.dispatchc          	      sB   | j  ¡ | j| j| j| j| j | j| jdœ}|jf |Ž t	ƒ  
|¡S )N)Z	use_httpsÚtoken_generatorÚ
from_emailÚemail_template_nameÚsubject_template_namer    Úhtml_email_template_nameÚextra_email_context)r    rA   ri   rj   rk   rl   rm   rn   Úsaver2   rJ   )r#   rI   Úopts)r7   r$   r%   rJ   à   s    
zPasswordResetView.form_valid)r'   r(   r)   rk   rn   r   rD   rj   rm   rl   r   Úsuccess_urlrT   r\   r[   r   ri   r   r   r3   rJ   rU   r$   r$   )r7   r%   rh   Ð   s   rh   Z_password_reset_tokenc               @   s   e Zd ZdZedƒZdS )ÚPasswordResetDoneViewz%registration/password_reset_done.htmlzPassword reset sentN)r'   r(   r)   rT   r\   r[   r$   r$   r$   r%   rr   ò   s   rr   c                   s‚   e Zd ZeZdZdZdZedƒZ	dZ
edƒZeZeeƒ ƒeeƒ‡ fdd„ƒƒZd	d
„ Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚPasswordResetConfirmViewFNzset-passwordZpassword_reset_completez(registration/password_reset_confirm.htmlzEnter new passwordc                s¾   d|krd|kst ‚d| _|  |d ¡| _| jd k	r°|d }|| jkrz| jj t¡}| j	 
| j|¡r°d| _tƒ j||ŽS n6| j	 
| j|¡r°|| jjt< | jj || j¡}t|ƒS |  |  ¡ ¡S )NÚuidb64ÚtokenFT)ÚAssertionErrorÚ	validlinkrH   r-   Úreset_url_tokenr    Úsessionr>   ÚINTERNAL_RESET_SESSION_TOKENri   Zcheck_tokenr2   r3   r0   Úreplacer   Zrender_to_responserM   )r#   r4   r5   ru   Zsession_tokenZredirect_url)r7   r$   r%   r3     s    

z!PasswordResetConfirmView.dispatchc             C   sH   yt |ƒ ¡ }tjj|d�}W n$ ttttjt	fk
rB   d }Y nX |S )N)Úpk)
r   ÚdecodeÚ	UserModelZ_default_managerr>   Ú	TypeErrorr1   ÚOverflowErrorZDoesNotExistr   )r#   rt   Úuidr-   r$   r$   r%   rH     s    
z!PasswordResetConfirmView.get_userc                s   t ƒ  ¡ }| j|d< |S )Nr-   )r2   rF   r-   )r#   r5   )r7   r$   r%   rF   '  s    

z(PasswordResetConfirmView.get_form_kwargsc                s4   |  ¡ }| jjt= | jr(t| j|| jƒ tƒ  |¡S )N)	ro   r    ry   rz   Úpost_reset_loginrG   Úpost_reset_login_backendr2   rJ   )r#   rI   r-   )r7   r$   r%   rJ   ,  s
    
z#PasswordResetConfirmView.form_validc                s8   t ƒ jf |Ž}| jrd|d< n| d tdƒddœ¡ |S )NTrw   zPassword reset unsuccessfulF)rI   r[   rw   )r2   rM   rw   rN   r\   )r#   r5   rQ   )r7   r$   r%   rM   3  s    

z)PasswordResetConfirmView.get_context_data)r'   r(   r)   r   rD   r‚   rƒ   rx   r   rq   rT   r\   r[   r   ri   r   r   r   r3   rH   rF   rJ   rM   rU   r$   r$   )r7   r%   rs   ÷   s   	rs   c                   s(   e Zd ZdZedƒZ‡ fdd„Z‡  ZS )ÚPasswordResetCompleteViewz)registration/password_reset_complete.htmlzPassword reset completec                s    t ƒ jf |Ž}ttjƒ|d< |S )Nr^   )r2   rM   r   r   r]   )r#   r5   rQ   )r7   r$   r%   rM   D  s    z*PasswordResetCompleteView.get_context_data)r'   r(   r)   rT   r\   r[   rM   rU   r$   r$   )r7   r%   r„   @  s   r„   c                   sf   e Zd ZeZedƒZdZedƒZ	e
eƒ ƒe
eƒe
eƒ‡ fdd„ƒƒƒZ‡ fdd„Z‡ fdd	„Z‡  ZS )
ÚPasswordChangeViewZpassword_change_donez&registration/password_change_form.htmlzPassword changec                s   t ƒ j||ŽS )N)r2   r3   )r#   r4   r5   )r7   r$   r%   r3   P  s    zPasswordChangeView.dispatchc                s   t ƒ  ¡ }| jj|d< |S )Nr-   )r2   rF   r    r-   )r#   r5   )r7   r$   r%   rF   V  s    
z"PasswordChangeView.get_form_kwargsc                s"   |  ¡  t| j|jƒ tƒ  |¡S )N)ro   r	   r    r-   r2   rJ   )r#   rI   )r7   r$   r%   rJ   [  s    zPasswordChangeView.form_valid)r'   r(   r)   r   rD   r   rq   rT   r\   r[   r   r   r   r
   r3   rF   rJ   rU   r$   r$   )r7   r%   r…   J  s   r…   c                   s0   e Zd ZdZedƒZeeƒ‡ fdd„ƒZ‡  Z	S )ÚPasswordChangeDoneViewz&registration/password_change_done.htmlzPassword change successfulc                s   t ƒ j||ŽS )N)r2   r3   )r#   r4   r5   )r7   r$   r%   r3   g  s    zPasswordChangeDoneView.dispatch)
r'   r(   r)   rT   r\   r[   r   r
   r3   rU   r$   r$   )r7   r%   r†   c  s   r†   )N)AÚurllib.parser   r   Zdjango.confr   Zdjango.contrib.authr   r   r   rG   r   rW   r	   Zdjango.contrib.auth.decoratorsr
   Zdjango.contrib.auth.formsr   r   r   r   Zdjango.contrib.auth.tokensr   Zdjango.contrib.sites.shortcutsr   Zdjango.core.exceptionsr   Zdjango.httpr   r   Zdjango.shortcutsr   Zdjango.urlsr   Zdjango.utils.decoratorsr   Zdjango.utils.httpr   r   Zdjango.utils.translationr   r\   Zdjango.views.decorators.cacher   Zdjango.views.decorators.csrfr   Zdjango.views.decorators.debugr   Zdjango.views.generic.baser   Zdjango.views.generic.editr   r~   r   r+   rV   r_   rf   rg   rh   rz   rr   rs   r„   r…   r†   r$   r$   r$   r%   Ú<module>   s@   C;
I
