
    h*                        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	 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 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 m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z,  e edddd            Z- G d de      Z. G d de      Z/ G d de      Z0 G d de      Z1 G d d e      Z2 G d! d"e      Z3y#)$    )settings)get_user_model)loginlogout)ObjectDoesNotExist)timezone)method_decorator)gettext_lazy)sensitive_post_parametersstatus)GenericAPIViewRetrieveUpdateAPIView)AllowAnyIsAuthenticated)Response)APIView   )	JWTSerializerJWTSerializerWithExpirationLoginSerializerPasswordChangeSerializerPasswordResetConfirmSerializerPasswordResetSerializerTokenSerializerUserDetailsSerializercreate_token)
TokenModel)
jwt_encodepasswordold_passwordnew_password1new_password2c                   h     e Zd ZdZefZeZeZ	dZ
dZdZdZe fd       Zd Zd Zd Zd Zd	 Z xZS )
	LoginViewa4  
    Check the credentials and return the REST Token
    if the credentials are valid and authenticated.
    Calls Django Auth login method to register User ID
    in Django session framework

    Accept the following POST parameters: username, password
    Return the REST Framework Token Object's key.
    dj_rest_authNc                 "    t        |   |i |S Nsuperdispatchselfargskwargs	__class__s      U/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/dj_rest_auth/views.pyr,   zLoginView.dispatch4       w000    c                 D    t        | j                  | j                         y r)   )django_loginrequestuserr.   s    r2   process_loginzLoginView.process_login8   s    T\\499-r4   c                 v    t        t        dd      r!t        t        dd      rt        }|S t        }|S t        }|S )NREST_USE_JWTFJWT_AUTH_RETURN_EXPIRATION)getattrr   r   r   r   )r.   response_serializers     r2   get_response_serializerz!LoginView.get_response_serializer;   sG    8^U3x!=uE&A# #"	 '4# #" #2""r4   c                 L   | j                   j                  d   | _        t        t        dd      r#t        | j                        \  | _        | _        n0t        | j                  | j                  | j                         | _
        t        t        dd      r| j                          y y )Nr8   r<   FREST_SESSION_LOGINT)
serializervalidated_datar8   r>   r   r    access_tokenrefresh_tokenr   token_modeltokenr:   r9   s    r2   r   zLoginView.loginG   s    OO226:	8^U34>tyy4I1Dt1%  $))DJ
 8148  9r4   c                    | j                         }t        t        dd      rddlm} t        j                         |j                  z   }t        j                         |j                  z   }t        t        dd      }| j                  | j                  | j                  d}|r
||d<   ||d<    ||| j                         	      }n" || j                  | j                         	      }t        |j                  t         j"                  
      }t        t        dd      r$ddlm}	  |	|| j                  | j                         |S )Nr<   Fr   )api_settingsr=   )r8   rE   rF   access_token_expirationrefresh_token_expiration)instancecontextr   r   )set_jwt_cookies)r@   r>   r   !rest_framework_simplejwt.settingsrJ   r	   nowACCESS_TOKEN_LIFETIMEREFRESH_TOKEN_LIFETIMEr8   rE   rF   get_serializer_contextrH   r   datar   HTTP_200_OKjwt_authrO   )
r.   serializer_classjwt_settingsrK   rL   return_expiration_timesrU   rC   responserO   s
             r2   get_responsezLoginView.get_responseU   s   7798^U3 (0||~8Z8Z'Z#(09\9\(\$&-h8TV[&\# 		 $ 1 1!%!3!3D '2I./3K/0)335J
 *335J
 JOOF4F4FG8^U31Hd&7&79K9KLr4   c                     || _         | j                  | j                   j                        | _        | j                  j	                  d       | j                          | j                         S )NrU   Traise_exception)r7   get_serializerrU   rC   is_validr   r\   r.   r7   r/   r0   s       r2   postzLoginView.postz   sU    --4<<3D3D-E   6

  ""r4   )__name__
__module____qualname____doc__r   permission_classesr   rX   r   rG   throttle_scoper8   rE   rH   sensitive_post_parameters_mr,   r:   r@   r   r\   rd   __classcell__r1   s   @r2   r&   r&   !   s]     #&K#NDLE 1 !1.
#!#J#r4   r&   c                   ,    e Zd ZdZefZdZd Zd Zd Z	y)
LogoutViewz
    Calls Django logout method and delete the Token object
    assigned to the current User object.

    Accepts/Returns nothing.
    r'   c                     t        t        dd      r| j                  |      }n | j                  |g|i |} | j                  ||g|i |S )NACCOUNT_LOGOUT_ON_GETF)r>   r   r   http_method_not_allowedfinalize_response)r.   r7   r/   r0   r[   s        r2   getzLogoutView.get   sX    84e<{{7+H3t33GMdMfMH%t%%gxI$I&IIr4   c                 $    | j                  |      S r)   r   rc   s       r2   rd   zLogoutView.post   s    {{7##r4   c                 8   	 |j                   j                  j                          t        t        dd      rt        |       t        dt        d      it        j                        }t        t        dd      rdd	lm} dd
lm} ddlm} t        t        dd       } ||       dt        j$                  v r(	  ||j&                  d         }|j)                          |S |s)t        d      }	d|	i|_        t        j                  |_        |S # t        t        f$ r Y w xY w# t*        $ r+ dt        d      i|_        t        j,                  |_        Y |S |t        t0        f$ r}t3        |d      ryd|j4                  v sd|j4                  v r5dt        |j4                  d         i|_        t        j,                  |_        nOdt        d      i|_        t        j6                  |_        n'dt        d      i|_        t        j6                  |_        Y d }~|S d }~ww xY w)NrB   TdetailzSuccessfully logged out.r   r<   Fr   )
TokenError)RefreshTokenr   )unset_jwt_cookiesJWT_AUTH_COOKIEz(rest_framework_simplejwt.token_blacklistrefreshz/Refresh token was not included in request data.r/   zToken is blacklistedzToken is invalid or expiredzAn error has occurred.zNeither cookies or blacklist are enabled, so the token has not been deleted server side. Please make sure the token is deleted client side.)r8   
auth_tokendeleteAttributeErrorr   r>   r   django_logoutr   _r   rV   #rest_framework_simplejwt.exceptionsrx   rest_framework_simplejwt.tokensry   rW   rz   INSTALLED_APPSrU   	blacklistKeyErrorHTTP_401_UNAUTHORIZEDstatus_code	TypeErrorhasattrr/   HTTP_500_INTERNAL_SERVER_ERROR)
r.   r7   r[   rx   ry   rz   cookie_namerH   errormessages
             r2   r   zLogoutView.logout   s   	LL##**, 8148'"q345%%

 8^U3 GD3!(,=tDKh'9X=T=TTU(i)@AEOO%0  !k "*7 3'-'9'9$g  23 		8   G%-q1b/c$dHM*0*F*FH(* ) #NI> Uuf-1UZZ?C`didndnCn-5qA7G,HHM393O3OH0-5q9Q7R,SHM393X3XH0 *215M3N(O/5/T/T, )Us0   $D 7%D  DD 0HH"B,HHN)
re   rf   rg   rh   r   ri   rj   rt   rd   r    r4   r2   ro   ro      s&     ##NJ$6r4   ro   c                   &    e Zd ZdZeZefZd Zd Z	y)UserDetailsViewa  
    Reads and updates UserModel fields
    Accepts GET, PUT, PATCH methods.

    Default accepted fields: username, first_name, last_name
    Default display fields: pk, username, email, first_name, last_name
    Read-only fields: pk, email

    Returns UserModel fields.
    c                 .    | j                   j                  S r)   )r7   r8   r9   s    r2   
get_objectzUserDetailsView.get_object   s    ||   r4   c                 F    t               j                  j                         S )zh
        Adding this method since it is sometimes called when using
        django-rest-swagger
        )r   objectsnoner9   s    r2   get_querysetzUserDetailsView.get_queryset   s    
 '',,..r4   N)
re   rf   rg   rh   r   rX   r   ri   r   r   r   r4   r2   r   r      s"    	 -)+!/r4   r   c                   $    e Zd ZdZeZefZdZd Z	y)PasswordResetViewz
    Calls Django Auth PasswordResetForm save method.

    Accepts the following POST parameters: email
    Returns the success/fail message.
    r'   c                     | j                  |j                        }|j                  d       |j                          t	        dt        d      it        j                        S )Nr^   Tr_   rw   z$Password reset e-mail has been sent.r   )ra   rU   rb   saver   r   r   rV   r.   r7   r/   r0   rC   s        r2   rd   zPasswordResetView.post   sX    ((gll(;
D1q?@A%%
 	
r4   N)
re   rf   rg   rh   r   rX   r   ri   rj   rd   r   r4   r2   r   r      s!     /"#N

r4   r   c                   @     e Zd ZdZeZefZdZe	 fd       Z
d Z xZS )PasswordResetConfirmViewz
    Password reset e-mail link is confirmed, therefore
    this resets the user's password.

    Accepts the following POST parameters: token, uid,
        new_password1, new_password2
    Returns the success/fail message.
    r'   c                 "    t        |   |i |S r)   r*   r-   s      r2   r,   z!PasswordResetConfirmView.dispatch  r3   r4   c                     | j                  |j                        }|j                  d       |j                          t	        dt        d      i      S )Nr^   Tr_   rw   z.Password has been reset with the new password.ra   rU   rb   r   r   r   r   s        r2   rd   zPasswordResetConfirmView.post  sO    ((gll(;
D1qIJK
 	
r4   )re   rf   rg   rh   r   rX   r   ri   rj   rk   r,   rd   rl   rm   s   @r2   r   r     s4     6"#N 1 !1
r4   r   c                   @     e Zd ZdZeZefZdZe	 fd       Z
d Z xZS )PasswordChangeViewz
    Calls Django Auth SetPasswordForm save method.

    Accepts the following POST parameters: new_password1, new_password2
    Returns the success/fail message.
    r'   c                 "    t        |   |i |S r)   r*   r-   s      r2   r,   zPasswordChangeView.dispatch'  r3   r4   c                     | j                  |j                        }|j                  d       |j                          t	        dt        d      i      S )Nr^   Tr_   rw   zNew password has been saved.r   r   s        r2   rd   zPasswordChangeView.post+  sJ    ((gll(;
D11%C#DEFFr4   )re   rf   rg   rh   r   rX   r   ri   rj   rk   r,   rd   rl   rm   s   @r2   r   r     s6     0)+#N 1 !1Gr4   r   N)4django.confr   django.contrib.authr   r   r6   r   r   django.core.exceptionsr   django.utilsr	   django.utils.decoratorsr
   django.utils.translationr   r   django.views.decorators.debugr   rest_frameworkr   rest_framework.genericsr   r   rest_framework.permissionsr   r   rest_framework.responser   rest_framework.viewsr   app_settingsr   r   r   r   r   r   r   r   r   modelsr   utilsr    rk   r&   ro   r   r   r   r   r   r4   r2   <module>r      s      . 5 7 5 ! 4 6 C ! I @ , (     /NO_ _# _#DK K\/+ /2
 
0
~ 
4G Gr4   