o
    þÞh67  ã                   @   s:  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 d dlmZ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/m0Z0 d dl1m2Z2 d dl3m4Z4 eƒ Z5G dd„ dƒZ6e$edd�G dd„ de6e4ƒƒZ7G dd„ de6e2ƒZ8d3d d!„Z9defd"d#„Z:G d$d%„ d%ƒZ;e$edd�G d&d'„ d'e;e4ƒƒZ<d(Z=e$edd�G d)d*„ d*e;e2ƒƒZ>e$edd�G d+d,„ d,e;e4ƒƒZ?e$edd�G d-d.„ d.e;e2ƒƒZ@G d/d0„ d0e;e4ƒZAG d1d2„ d2e;e2ƒZBdS )4é    )ÚurlsplitÚ
urlunsplit)Úsettings)ÚREDIRECT_FIELD_NAMEÚget_user_model)Úlogin)Úlogout)Úupdate_session_auth_hash)Úlogin_not_requiredÚ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dZeZeƒ Zdd„ Zdd„ Z	dd„ Z
dd	„ ZdS )
ÚRedirectURLMixinNc                 C   s   |   ¡ p|  ¡ S ©N)Úget_redirect_urlÚget_default_redirect_url©Úself© r'   úY/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/contrib/auth/views.pyÚget_success_url(   s   z RedirectURLMixin.get_success_urlc                 C   sB   | j j | j| j j | j¡¡}t||  ¡ | j  ¡ d�}|r|S dS )z6Return the user-originating redirect URL if it's safe.)ÚurlÚallowed_hostsÚrequire_httpsÚ )ÚrequestÚPOSTÚgetÚredirect_field_nameÚGETr   Úget_success_url_allowed_hostsÚ	is_secure)r&   Úredirect_toÚurl_is_safer'   r'   r(   r#   +   s   ÿýz!RedirectURLMixin.get_redirect_urlc                 C   s   | j  ¡ h| j£S r"   )r.   Úget_hostÚsuccess_url_allowed_hostsr%   r'   r'   r(   r3   7   s   z.RedirectURLMixin.get_success_url_allowed_hostsc                 C   s   | j rt| j ƒS tdƒ‚)ú Return the default redirect URL.z+No URL to redirect to. Provide a next_page.)Ú	next_pager   r   r%   r'   r'   r(   r$   :   s   
z)RedirectURLMixin.get_default_redirect_url)Ú__name__Ú
__module__Ú__qualname__r:   r   r1   Úsetr8   r)   r#   r3   r$   r'   r'   r'   r(   r!   #   s    r!   Údispatch)Únamec                       s~   e Zd ZdZeZdZdZdZdZ	e
eƒ ƒe
eƒe
eƒ‡ f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_authenticatedr)   ÚpathÚ
ValueErrorr   Úsuperr?   ©r&   r.   ÚargsÚkwargsr5   ©Ú	__class__r'   r(   r?   M   s   ÿzLoginView.dispatchc                 C   s   | j rt| j ƒS ttjƒS ©r9   )r:   r   r   ÚLOGIN_REDIRECT_URLr%   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_classb   s   zLoginView.get_form_classc                    ó   t ƒ  ¡ }| j|d< |S )Nr.   )rG   Úget_form_kwargsr.   ©r&   rJ   rK   r'   r(   rS   e   ó   

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_validj   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'   )	rG   Úget_context_datar   r.   Úupdater1   r#   r@   Úextra_context©r&   rJ   ÚcontextÚcurrent_siterK   r'   r(   r]   o   s   

ýüÿzLoginView.get_context_data)r;   r<   r=   Ú__doc__r   rP   rO   Útemplate_namerB   r_   r   r   r   r   r?   r$   rQ   rS   rZ   r]   Ú__classcell__r'   r'   rK   r(   rA   A   s     rA   c                       s`   e Zd ZdZddgZdZdZeeƒee	ƒ‡ fdd„ƒƒZ
‡ f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.
    ÚpostÚoptionszregistration/logged_out.htmlNc                    s   t ƒ j|g|¢R i |¤ŽS r"   ©rG   r?   )r&   r.   rI   rJ   rK   r'   r(   r?   †   s   zLogoutView.dispatchc                    s>   t |ƒ |  ¡ }|| ¡ krt|ƒS tƒ j|g|¢R i |¤ŽS )zLogout may be done via POST.)Úauth_logoutr)   Úget_full_pathr   rG   r0   rH   rK   r'   r(   rg   ‹   s
   zLogoutView.postc                 C   s(   | j rt| j ƒS tjrttjƒS | jjS rM   )r:   r   r   ÚLOGOUT_REDIRECT_URLr.   rE   r%   r'   r'   r(   r$   ”   s
   

z#LogoutView.get_default_redirect_urlc                    sD   t ƒ jdi |¤Ž}t| jƒ}| ||jtdƒd dœ| jpi ¥¡ |S )Nz
Logged out)r[   r\   ÚtitleÚsubtitler'   )rG   r]   r   r.   r^   r@   Ú_r_   r`   rK   r'   r(   r]   �   s   
üûÿ	zLogoutView.get_context_data)r;   r<   r=   rc   Úhttp_method_namesrd   r_   r   r   r   r?   rg   r$   r]   re   r'   r'   rK   r(   rf   }   s    		rf   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.
    )r:   )r   r   Ú	LOGIN_URLrf   Úas_view)r.   Ú	login_urlr'   r'   r(   Úlogout_then_login¬   s   rt   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   rq   Úlistr   r   Ú	urlencoder   r   )Únextrs   r1   Úresolved_urlÚlogin_url_partsÚquerystringr'   r'   r(   Úredirect_to_login´   s   r   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚPasswordContextMixinNc                    s2   t ƒ jdi |¤Ž}| | jd dœ| jpi ¥¡ |S )N)rm   rn   r'   )rG   r]   r^   rm   r_   ©r&   rJ   ra   rK   r'   r(   r]   Î   s
   ÿz%PasswordContextMixin.get_context_data)r;   r<   r=   r_   r]   re   r'   r'   rK   r(   r€   Ë   s    r€   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"   ri   ©r&   rI   rJ   rK   r'   r(   r?   ã   ó   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.   r4   rˆ   r‰   rŠ   r‹   rŒ   r�   ÚsaverG   rZ   )r&   rY   ÚoptsrK   r'   r(   rZ   ç   s   ø
zPasswordResetView.form_valid)r;   r<   r=   rŠ   r�   r   rP   r‰   rŒ   r‹   r   Úsuccess_urlrd   ro   rm   r   rˆ   r   r   r?   rZ   re   r'   r'   rK   r(   r‚   Ö   s    r‚   Ú_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=   rd   ro   rm   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-passwordÚpassword_reset_completez(registration/password_reset_confirm.htmlzEnter new passwordc                    sÆ   d|vsd|vrt dƒ‚d| _|  |d ¡| _| jd ur\|d }|| jkrA| jj t¡}| j	 
| j|¡r@d| _tƒ j|i |¤ŽS n| 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   Ú	validlinkrW   rC   Úreset_url_tokenr.   Úsessionr0   ÚINTERNAL_RESET_SESSION_TOKENrˆ   Úcheck_tokenrG   r?   rE   Úreplacer   Úrender_to_responser]   )r&   rI   rJ   r–   Úsession_tokenÚredirect_urlrK   r'   r(   r?   
  s*   ÿ

ýÿz!PasswordResetConfirmView.dispatchc                 C   sT   zt |ƒ ¡ }tjj |¡}tjj|d�}W |S  tt	t
tjtfy)   d }Y |S w )N)Úpk)r   ÚdecodeÚ	UserModelÚ_metar    Ú	to_pythonÚ_default_managerr0   Ú	TypeErrorrF   ÚOverflowErrorÚDoesNotExistr   )r&   r•   Úuidr    rC   r'   r'   r(   rW   ,  s   	ø
ûøz!PasswordResetConfirmView.get_userc                    rR   ©NrC   )rG   rS   rC   rT   rK   r'   r(   rS   <  rU   z(PasswordResetConfirmView.get_form_kwargsc                    s4   |  ¡ }| jjt= | jrt| j|| jƒ tƒ  |¡S r"   )	rŽ   r.   r™   rš   Úpost_reset_loginrV   Úpost_reset_login_backendrG   rZ   )r&   rY   rC   rK   r'   r(   rZ   A  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)rY   rm   r—   r'   )rG   r]   r—   r^   ro   r�   rK   r'   r(   r]   H  s   	ùýÿz)PasswordResetConfirmView.get_context_data)r;   r<   r=   r   rP   r«   r¬   r˜   r   r�   rd   ro   rm   r   rˆ   r   r   r   r?   rW   rS   rZ   r]   re   r'   r'   rK   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 )Nrs   r'   )rG   r]   r   r   rq   r�   rK   r'   r(   r]   \  s   z*PasswordResetCompleteView.get_context_data)r;   r<   r=   rd   ro   rm   r]   re   r'   r'   rK   r(   r­   W  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"   ri   r…   rK   r'   r(   r?   h  s   zPasswordChangeView.dispatchc                    s   t ƒ  ¡ }| jj|d< |S rª   )rG   rS   r.   rC   rT   rK   r'   r(   rS   n  s   
z"PasswordChangeView.get_form_kwargsc                    s"   |  ¡  t| j|jƒ tƒ  |¡S r"   )rŽ   r	   r.   rC   rG   rZ   rX   rK   r'   r(   rZ   s  s   zPasswordChangeView.form_valid)r;   r<   r=   r   rP   r   r�   rd   ro   rm   r   r   r   r   r?   rS   rZ   re   r'   r'   rK   r(   r®   b  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"   ri   r…   rK   r'   r(   r?     r†   zPasswordChangeDoneView.dispatch)
r;   r<   r=   rd   ro   rm   r   r   r?   re   r'   r'   rK   r(   r°   {  s
    r°   r"   )CÚurllib.parser   r   Údjango.confr   Údjango.contrib.authr   r   r   rV   r   rj   r	   Údjango.contrib.auth.decoratorsr
   r   Údjango.contrib.auth.formsr   r   r   r   Údjango.contrib.auth.tokensr   Údjango.contrib.sites.shortcutsr   Údjango.core.exceptionsr   r   Údjango.httpr   r   Údjango.shortcutsr   Údjango.urlsr   Údjango.utils.decoratorsr   Údjango.utils.httpr   r   Údjango.utils.translationr   ro   Údjango.views.decorators.cacher   Údjango.views.decorators.csrfr   Údjango.views.decorators.debugr   Údjango.views.generic.baser   Údjango.views.generic.editr    r¢   r!   rA   rf   rt   r   r€   r‚   rš   r’   r“   r­   r®   r°   r'   r'   r'   r(   Ú<module>   sR    
;
/



W
