U
    5øiÓ5  ã                   @   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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l0m1Z1 d dl2m3Z3 eƒ Z4G dd„ dƒZ5G dd„ de5e3ƒZ6G dd„ de5e1ƒZ7d.dd„Z8defdd„Z9G dd „ d ƒZ:G d!d"„ d"e:e3ƒZ;d#Z<G d$d%„ d%e:e1ƒZ=G d&d'„ d'e:e3ƒZ>G d(d)„ d)e:e1ƒZ?G d*d+„ d+e:e3ƒZ@G d,d-„ d-e:e1ƒZAd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)ÚImproperlyConfiguredÚ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-unpacked-wheel-gcyiijx6/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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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(   r6   4   s    ÿzLoginView.dispatchc                 C   s   |   ¡ p|  ¡ S r!   )Úget_redirect_urlÚget_default_redirect_urlr%   r'   r'   r(   r2   B   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.Ú ©ÚurlZallowed_hostsZrequire_https)r"   ÚPOSTÚgetÚredirect_field_nameÚGETr   r)   Ú	is_secure)r&   r9   Úurl_is_safer'   r'   r(   r<   E   s    þýzLoginView.get_redirect_urlc                 C   s   t | jptjƒS )z Return the default redirect URL.)r   Ú	next_pager   ZLOGIN_REDIRECT_URLr%   r'   r'   r(   r=   R   s    z"LoginView.get_default_redirect_urlc                 C   s   | j p
| jS r!   )Úauthentication_formÚ
form_classr%   r'   r'   r(   Úget_form_classV   s    zLoginView.get_form_classc                    s   t ƒ  ¡ }| j|d< |S )Nr"   )r5   Úget_form_kwargsr"   ©r&   r8   r:   r'   r(   rK   Y   s    

zLoginView.get_form_kwargsc                 C   s   t | j| ¡ ƒ t|  ¡ ƒS )z)Security check complete. Log the user in.)Ú
auth_loginr"   Úget_userr   r2   ©r&   Úformr'   r'   r(   Ú
form_valid^   s    zLoginView.form_validc                    sD   t ƒ jf |Ž}t| jƒ}| | j|  ¡ d|d|ji| jp:i –¡ |S )NÚsiteÚ	site_name)	r5   Úget_context_datar   r"   ÚupdaterC   r<   ÚnameÚextra_context©r&   r8   ÚcontextZcurrent_siter:   r'   r(   rT   c   s    
   ýüzLoginView.get_context_data)r*   r+   r,   Ú__doc__r   rI   rH   rG   r   rC   Útemplate_namer/   rW   r   r   r   r   r6   r2   r<   r=   rJ   rK   rQ   rT   Ú__classcell__r'   r'   r:   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 r!   )Úauth_logoutÚget_next_pager   r5   r6   )r&   r"   r7   r8   rG   r:   r'   r(   r6   x   s
    zLogoutView.dispatchc                 O   s   | j |f|ž|ŽS )zLogout may be done via POST.)rB   )r&   r"   r7   r8   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 )Nr?   )rG   r   r   ZLOGOUT_REDIRECT_URLrC   r"   rA   rD   rB   r   r)   rE   r3   )r&   rG   rF   r'   r'   r(   r_   …   s(    
ÿþýzLogoutView.get_next_pagec                    s>   t ƒ jf |Ž}t| jƒ}| ||jtdƒdœ| jp4i –¡ |S )Nz
Logged out)rR   rS   Útitle)r5   rT   r   r"   rU   rV   Ú_rW   rX   r:   r'   r(   rT   ž   s    
ýüzLogoutView.get_context_data)r*   r+   r,   rZ   rG   r   rC   r[   rW   r   r   r6   r`   r_   rT   r\   r'   r'   r:   r(   r]   o   s   r]   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.
    )rG   )r   r   Ú	LOGIN_URLr]   Zas_view)r"   Ú	login_urlr'   r'   r(   Úlogout_then_loginª   s    re   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   rc   Úlistr   r   Ú	urlencoder   r   )Únextrd   rC   Zresolved_urlZlogin_url_partsZquerystringr'   r'   r(   Úredirect_to_login²   s    rl   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 )Nra   )r5   rT   rU   ra   rW   ©r&   r8   rY   r:   r'   r(   rT   Ë   s     ÿþz%PasswordContextMixin.get_context_data)r*   r+   r,   rW   rT   r\   r'   r'   r:   r(   rm   È   s   rm   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 r!   ©r5   r6   ©r&   r7   r8   r:   r'   r(   r6   à   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"   rE   rr   rs   rt   ru   rv   rw   Úsaver5   rQ   )r&   rP   Úoptsr:   r'   r(   rQ   ä   s    ø
zPasswordResetView.form_valid)r*   r+   r,   rt   rw   r   rI   rs   rv   ru   r   Úsuccess_urlr[   rb   ra   r   rr   r   r   r6   rQ   r\   r'   r'   r:   r(   ro   Ô   s   ro   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,   r[   rb   ra   r'   r'   r'   r(   r{   ö   s   r{   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sd|krt dƒ‚d| _|  |d ¡| _| jd k	r´|d }|| jkr~| 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Útokenz:The URL path must contain 'uidb64' and 'token' parameters.FT)r   Ú	validlinkrN   r0   Úreset_url_tokenr"   ÚsessionrB   ÚINTERNAL_RESET_SESSION_TOKENrr   Zcheck_tokenr5   r6   r3   Úreplacer   Zrender_to_responserT   )r&   r7   r8   r~   Zsession_tokenZredirect_urlr:   r'   r(   r6     s$    ÿ

z!PasswordResetConfirmView.dispatchc                 C   sH   z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_managerrB   Ú	TypeErrorr4   ÚOverflowErrorZDoesNotExistr   )r&   r}   Úuidr0   r'   r'   r(   rN   %  s    
z!PasswordResetConfirmView.get_userc                    s   t ƒ  ¡ }| j|d< |S ©Nr0   )r5   rK   r0   rL   r:   r'   r(   rK   .  s    

z(PasswordResetConfirmView.get_form_kwargsc                    s4   |  ¡ }| jjt= | jr(t| j|| jƒ tƒ  |¡S r!   )	rx   r"   r�   r‚   Úpost_reset_loginrM   Úpost_reset_login_backendr5   rQ   )r&   rP   r0   r:   r'   r(   rQ   3  s
    
z#PasswordResetConfirmView.form_validc                    s8   t ƒ jf |Ž}| jrd|d< n| d tdƒddœ¡ |S )NTr   zPassword reset unsuccessfulF)rP   ra   r   )r5   rT   r   rU   rb   rn   r:   r'   r(   rT   :  s    
ýz)PasswordResetConfirmView.get_context_data)r*   r+   r,   r   rI   r‹   rŒ   r€   r   rz   r[   rb   ra   r   rr   r   r   r   r6   rN   rK   rQ   rT   r\   r'   r'   r:   r(   r|   û   s   	r|   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 )Nrd   )r5   rT   r   r   rc   rn   r:   r'   r(   rT   K  s    z*PasswordResetCompleteView.get_context_data)r*   r+   r,   r[   rb   ra   rT   r\   r'   r'   r:   r(   r�   G  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 r!   rp   rq   r:   r'   r(   r6   W  s    zPasswordChangeView.dispatchc                    s   t ƒ  ¡ }| jj|d< |S rŠ   )r5   rK   r"   r0   rL   r:   r'   r(   rK   ]  s    
z"PasswordChangeView.get_form_kwargsc                    s"   |  ¡  t| j|jƒ tƒ  |¡S r!   )rx   r	   r"   r0   r5   rQ   rO   r:   r'   r(   rQ   b  s    zPasswordChangeView.form_valid)r*   r+   r,   r   rI   r   rz   r[   rb   ra   r   r   r   r
   r6   rK   rQ   r\   r'   r'   r:   r(   rŽ   Q  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 r!   rp   rq   r:   r'   r(   r6   n  s    zPasswordChangeDoneView.dispatch)
r*   r+   r,   r[   rb   ra   r   r
   r6   r\   r'   r'   r:   r(   r�   j  s   r�   )N)BÚurllib.parser   r   Zdjango.confr   Zdjango.contrib.authr   r   r   rM   r   r^   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   r   Zdjango.httpr   r   Zdjango.shortcutsr   Zdjango.urlsr   Zdjango.utils.decoratorsr   Zdjango.utils.httpr   r   Zdjango.utils.translationr   rb   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.   r]   re   rl   rm   ro   r‚   r{   r|   r�   rŽ   r�   r'   r'   r'   r(   Ú<module>   s@   G;
L
