o
    uvXjb4  ã                   @   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;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ƒZAdS )0é    )Ú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)Úis_safe_urlÚ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&   úV/var/www/html/myproject/venv/lib/python3.10/site-packages/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                    sN   | j r| jjjr|  ¡ }|| jjkrtdƒ‚t|ƒS tƒ j	|g|¢R i |¤Ž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'   r5   1   s   ÿzLoginView.dispatchc                 C   s   |   ¡ }|p
ttjƒS r    )Úget_redirect_urlr   r   ÚLOGIN_REDIRECT_URL)r%   Úurlr&   r&   r'   r1   ?   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.Ú ©r=   Úallowed_hostsÚrequire_https)r!   ÚPOSTÚgetÚredirect_field_nameÚGETr   r(   Ú	is_secure)r%   r8   Úurl_is_safer&   r&   r'   r;   C   s   þýzLoginView.get_redirect_urlc                 C   s   | j p| jS r    )Úauthentication_formÚ
form_classr$   r&   r&   r'   Úget_form_classP   s   zLoginView.get_form_classc                    ó   t ƒ  ¡ }| j|d< |S )Nr!   )r4   Úget_form_kwargsr!   ©r%   r7   r9   r&   r'   rL   S   ó   

zLoginView.get_form_kwargsc                 C   s   t | j| ¡ ƒ t|  ¡ ƒS )z)Security check complete. Log the user in.)Ú
auth_loginr!   Úget_userr   r1   ©r%   Úformr&   r&   r'   Ú
form_validX   s   zLoginView.form_validc                    sH   t ƒ jdi |¤Ž}t| jƒ}| | j|  ¡ d|d|ji| jpi ¥¡ |S )NÚsiteÚ	site_namer&   )	r4   Úget_context_datar   r!   ÚupdaterD   r;   ÚnameÚextra_context©r%   r7   ÚcontextÚcurrent_siter9   r&   r'   rV   ]   s   

ýüzLoginView.get_context_data)r)   r*   r+   Ú__doc__r   rI   rH   r   rD   Útemplate_namer.   rY   r   r   r   r   r5   r1   r;   rJ   rL   rS   rV   Ú__classcell__r&   r&   r9   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                    s6   t |ƒ |  ¡ }|rt|ƒS tƒ j|g|¢R i |¤ŽS r    )Úauth_logoutÚget_next_pager   r4   r5   )r%   r!   r6   r7   Ú	next_pager9   r&   r'   r5   r   s
   zLogoutView.dispatchc                 O   s   | j |g|¢R i |¤ŽS )zLogout may be done via POST.)rC   )r%   r!   r6   r7   r&   r&   r'   Úpost{   s   zLogoutView.postc                 C   s�   | j d urt| j ƒ}ntjrttjƒ}n| j }| j| jjv s%| j| jjv rF| jj | j| jj | j¡¡}t	||  
¡ | j ¡ d�}|sF| jj}|S )Nr?   )rc   r   r   ÚLOGOUT_REDIRECT_URLrD   r!   rB   rE   rC   r   r(   rF   r2   )r%   rc   rG   r&   r&   r'   rb      s&   
þýzLogoutView.get_next_pagec                    sB   t ƒ jdi |¤Ž}t| jƒ}| ||jtdƒdœ| jpi ¥¡ |S )Nz
Logged out)rT   rU   Útitler&   )r4   rV   r   r!   rW   rX   Ú_rY   rZ   r9   r&   r'   rV   ˜   s   
ýüzLogoutView.get_context_data)r)   r*   r+   r]   rc   r   rD   r^   rY   r   r   r5   rd   rb   rV   r_   r&   r&   r9   r'   r`   i   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.
    )rc   )r   r   Ú	LOGIN_URLr`   Úas_view)r!   Ú	login_urlr&   r&   r'   Úlogout_then_login¤   s   rk   c                 C   sR   t |ptjƒ}tt|ƒƒ}|r#t|d dd�}| ||< |jdd�|d< tt|ƒƒS )zM
    Redirect the user to the login page, passing the given 'next' page.
    é   T)Úmutableú/)Úsafe)	r   r   rh   Úlistr   r   Ú	urlencoder   r   )Únextrj   rD   Úresolved_urlÚlogin_url_partsÚquerystringr&   r&   r'   Úredirect_to_login¬   s   rv   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚPasswordContextMixinNc                    s0   t ƒ jdi |¤Ž}| d| ji| jpi ¥¡ |S )Nrf   r&   )r4   rV   rW   rf   rY   ©r%   r7   r[   r9   r&   r'   rV   Å   s   ÿþz%PasswordContextMixin.get_context_data)r)   r*   r+   rY   rV   r_   r&   r&   r9   r'   rw   Â   s    rw   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.txtÚpassword_reset_donez%registration/password_reset_form.htmlzPassword resetc                    ó   t ƒ j|i |¤ŽS r    ©r4   r5   ©r%   r6   r7   r9   r&   r'   r5   Ú   ó   zPasswordResetView.dispatchc              	      sF   | j  ¡ | j| j| j| j| j | j| jdœ}|jdi |¤Ž t	ƒ  
|¡S )N)Ú	use_httpsÚtoken_generatorÚ
from_emailÚemail_template_nameÚsubject_template_namer!   Úhtml_email_template_nameÚextra_email_contextr&   )r!   rF   r€   r�   r‚   rƒ   r„   r…   Úsaver4   rS   )r%   rR   Úoptsr9   r&   r'   rS   Þ   s   ø
zPasswordResetView.form_valid)r)   r*   r+   r‚   r…   r   rI   r�   r„   rƒ   r   Úsuccess_urlr^   rg   rf   r   r€   r   r   r5   rS   r_   r&   r&   r9   r'   ry   Î   s    ry   zset-passwordÚ_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^   rg   rf   r&   r&   r&   r'   rŠ   ñ   s    rŠ   c                       s~   e Zd Ze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 )ÚPasswordResetConfirmViewFNÚpassword_reset_completez(registration/password_reset_confirm.htmlzEnter new passwordc                    s¾   d|v rd|v s
J ‚d| _ |  |d ¡| _| jd urX|d }|tkr>| jj t¡}| j 	| j|¡r=d| _ t
ƒ j|i |¤ŽS n| j 	| j|¡rX|| jjt< | jj |t¡}t|ƒS |  |  ¡ ¡S )NÚuidb64ÚtokenFT)Ú	validlinkrP   r/   ÚINTERNAL_RESET_URL_TOKENr!   ÚsessionrC   ÚINTERNAL_RESET_SESSION_TOKENr€   Úcheck_tokenr4   r5   r2   Úreplacer   Úrender_to_responserV   )r%   r6   r7   rŽ   Úsession_tokenÚredirect_urlr9   r&   r'   r5   ÿ   s    
ýz!PasswordResetConfirmView.dispatchc                 C   sF   zt |ƒ ¡ }tjj|d�}W |S  ttttjt	fy"   d }Y |S w )N)Úpk)
r   ÚdecodeÚ	UserModelÚ_default_managerrC   Ú	TypeErrorr3   ÚOverflowErrorÚDoesNotExistr   )r%   r�   Úuidr/   r&   r&   r'   rP     s   þþz!PasswordResetConfirmView.get_userc                    rK   ©Nr/   )r4   rL   r/   rM   r9   r&   r'   rL   %  rN   z(PasswordResetConfirmView.get_form_kwargsc                    s4   |  ¡ }| jjt= | jrt| j|| jƒ tƒ  |¡S r    )	r†   r!   r‘   r’   Úpost_reset_loginrO   Úpost_reset_login_backendr4   rS   )r%   rR   r/   r9   r&   r'   rS   *  s
   
z#PasswordResetConfirmView.form_validc                    s>   t ƒ jdi |¤Ž}| jrd|d< |S | d tdƒddœ¡ |S )NTr�   zPassword reset unsuccessfulF)rR   rf   r�   r&   )r4   rV   r�   rW   rg   rx   r9   r&   r'   rV   1  s   ûýz)PasswordResetConfirmView.get_context_data)r)   r*   r+   r   rI   r¡   r¢   r   rˆ   r^   rg   rf   r   r€   r   r   r   r5   rP   rL   rS   rV   r_   r&   r&   r9   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di |¤Ž}ttjƒ|d< |S )Nrj   r&   )r4   rV   r   r   rh   rx   r9   r&   r'   rV   B  s   z*PasswordResetCompleteView.get_context_data)r)   r*   r+   r^   rg   rf   rV   r_   r&   r&   r9   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 )
ÚPasswordChangeViewÚpassword_change_donez&registration/password_change_form.htmlzPassword changec                    r{   r    r|   r}   r9   r&   r'   r5   N  s   zPasswordChangeView.dispatchc                    s   t ƒ  ¡ }| jj|d< |S r    )r4   rL   r!   r/   rM   r9   r&   r'   rL   T  s   
z"PasswordChangeView.get_form_kwargsc                    s"   |  ¡  t| j|jƒ tƒ  |¡S r    )r†   r	   r!   r/   r4   rS   rQ   r9   r&   r'   rS   Y  s   zPasswordChangeView.form_valid)r)   r*   r+   r   rI   r   rˆ   r^   rg   rf   r   r   r   r
   r5   rL   rS   r_   r&   r&   r9   r'   r¤   H  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                    r{   r    r|   r}   r9   r&   r'   r5   e  r~   zPasswordChangeDoneView.dispatch)
r)   r*   r+   r^   rg   rf   r   r
   r5   r_   r&   r&   r9   r'   r¦   a  s
    r¦   r    )BÚurllib.parser   r   Údjango.confr   Údjango.contrib.authr   r   r   rO   r   ra   r	   Údjango.contrib.auth.decoratorsr
   Údjango.contrib.auth.formsr   r   r   r   Údjango.contrib.auth.tokensr   Údjango.contrib.sites.shortcutsr   Údjango.core.exceptionsr   Údjango.httpr   r   Údjango.shortcutsr   Údjango.urlsr   Údjango.utils.decoratorsr   Údjango.utils.httpr   r   Údjango.utils.translationr   rg   Údjango.views.decorators.cacher   Údjango.views.decorators.csrfr   Údjango.views.decorators.debugr   Údjango.views.generic.baser   Údjango.views.generic.editr   rš   r   r-   r`   rk   rv   rw   ry   r�   r’   rŠ   r‹   r£   r¤   r¦   r&   r&   r&   r'   Ú<module>   sD    C
;H
