a
    ÛEb÷6  ã                   @   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 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ƒZ7d1d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/d0„ d0e:e1ƒZAdS )2é    )Ú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'   úY/home/ja/django-apps/lartica_env/lib/python3.9/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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                    sN   | j r4| jjjr4|  ¡ }|| 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(   r6   7   s    ÿzLoginView.dispatchc                 C   s   |   ¡ p|  ¡ S r!   )Úget_redirect_urlÚget_default_redirect_urlr%   r'   r'   r(   r2   E   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<   H   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=   T   s    z"LoginView.get_default_redirect_urlc                 C   s   | j p
| jS r!   )Úauthentication_formÚ
form_classr%   r'   r'   r(   Úget_form_classX   s    zLoginView.get_form_classc                    s   t ƒ  ¡ }| j|d< |S )Nr"   )r5   Úget_form_kwargsr"   ©r&   r8   r:   r'   r(   rK   [   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                    sH   t ƒ jf i |¤Ž}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   e   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                    s6   t |ƒ |  ¡ }|rt|ƒS tƒ j|g|¢R i |¤ŽS r!   )Úauth_logoutÚget_next_pager   r5   r6   )r&   r"   r7   r8   rG   r:   r'   r(   r6   }   s
    zLogoutView.dispatchc                 O   s   | j |g|¢R i |¤Ž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 urt| j ƒ}ntjr(ttjƒ}n| j }| j| jjv sJ| j| jjv 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                    sB   t ƒ jf i |¤Ž}t| jƒ}| ||jtdƒdœ| jp8i ¥¡ |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]   s   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                    s0   t ƒ jf i |¤Ž}| 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|i |¤ŽS r!   ©r5   r6   ©r&   r7   r8   r:   r'   r(   r6   æ   s    zPasswordResetView.dispatchc              	      sF   | j  ¡ | j| j| j| j| j | j| jdœ}|jf i |¤Ž 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|vsd|vrt dƒ‚d| _|  |d ¡| _| jd ur¸|d }|| jkr‚| jj t¡}| j	 
| j|¡r¸d| _tƒ j|i |¤Ž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   sF   zt |ƒ ¡ }tjj|d�}W n" ttttjt	fy@   d }Y n0 |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   A  s
    
z#PasswordResetConfirmView.form_validc                    s<   t ƒ jf i |¤Ž}| 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   H  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 i |¤Ž}ttjƒ|d< |S )Nrd   )r5   rT   r   r   rc   rn   r:   r'   r(   rT   [  s    z*PasswordResetCompleteView.get_context_data)r*   r+   r,   r[   rb   ra   rT   r\   r'   r'   r:   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 )
ÚPasswordChangeViewZpassword_change_donez&registration/password_change_form.htmlzPassword changec                    s   t ƒ j|i |¤ŽS r!   rp   rq   r:   r'   r(   r6   g  s    zPasswordChangeView.dispatchc                    s   t ƒ  ¡ }| jj|d< |S rŠ   )r5   rK   r"   r0   rL   r:   r'   r(   rK   m  s    
z"PasswordChangeView.get_form_kwargsc                    s"   |  ¡  t| j|jƒ tƒ  |¡S r!   )rx   r	   r"   r0   r5   rQ   rO   r:   r'   r(   rQ   r  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Ž   a  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|i |¤ŽS r!   rp   rq   r:   r'   r(   r6   ~  s    zPasswordChangeDoneView.dispatch)
r*   r+   r,   r[   rb   ra   r   r
   r6   r\   r'   r'   r:   r(   r�   z  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>   sF   I?
	V
