o
    di})                     @   s   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 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 d dlmZ G dd deZG dd deZG dd deZ G dd deZ!dS )    )settings)default_token_generator)ObjectDoesNotExist)force_bytes)urlsafe_base64_encodeurlsafe_base64_decodestatus)CreateAPIView)AllowAnyIsAuthenticated)Response)APIView)RefreshToken)User)admin_account_serializers)send_reset_password_mailc                   @   $   e Zd ZdZefZdgZdd ZdS )AdminLoginViewa  
    This is the AdminLoginView class responsible for handling user login through a POST request.

    It allows administrators to log in and generate an authentication token.

    Attributes:
        permission_classes (tuple): Permissions required to access this view (AllowAny for public access).
        http_method_names (list): The allowed HTTP methods, in this case, only POST is allowed.

    Methods:
        post(request): Handles user login and token generation logic.
    postc           
      O   s   t j|jd}|jdd |jd}|jd}z0tjj|d}||r@t	|}|j
|j|jt|jt|d}	t|	W S tdd	itjd
W S  ty[   tdditjd
 Y S w )a  
        Handles user login and token generation logic.

        Args:
            request (HttpRequest): The HTTP request containing user data.

        Returns:
            Response: The serialized user data with an authentication token or an error response.
        dataTraise_exceptionemailpassword)r   )idr   nametokenrefreshmessagezPYou have entered the wrong password. Please try again with the correct password.r   zEThe entered email address does not exist. Please sign up to continue.)r   ZAdminLoginSerializerr   is_validgetr   objectscheck_passwordr   for_userpkr   r   straccess_tokenr   r	   HTTP_400_BAD_REQUESTr   HTTP_404_NOT_FOUND)
selfrequestargskwargs
serializerr   r   userr   response_data r2   a/var/www/html/evchargy.com/django-station-ev/station_ev/apps/account/views/admin_account_views.pyr   #   s6   



zAdminLoginView.postN__name__
__module____qualname____doc__r   permission_classeshttp_method_namesr   r2   r2   r2   r3   r      
    r   c                   @   r   )AdminChangePasswordViewa  
    This is the AdminChangePasswordView class responsible for handling password change through a POST request.

    It allows administrators to change their password by providing their old and new passwords.

    Attributes:
        permission_classes (tuple): Permissions required to access this view (AllowAny for public access).
        http_method_names (list): The allowed HTTP methods, in this case, only POST is allowed.

    Methods:
        post(request): Handles password change logic.
    r   c           	      O   s   t j|jd}|jdd |jd}|jd}|jd}z*tjj|dd}||r@|| |	  t
dd	itjd
W S t
dditjd
W S  ty[   t
dditjd
 Y S w )a   
        Handles password change logic.

        Args:
            request (HttpRequest): The HTTP request containing user data.

        Returns:
            Response: A response indicating the success or failure of the password change operation.
        r   Tr   r   old_passwordnew_passwordr   is_superuserr    z'Password has been changed successfully.r   zMYour old password does not match. Please try again with the correct password.)The entered email address does not exist.)r   ZAdminChangePasswordSerializerr   r!   r"   r   r#   r$   set_passwordsaver   r	   HTTP_200_OKr)   r   r*   )	r+   r,   r-   r.   r/   r   r=   r>   r0   r2   r2   r3   r   j   s6   


zAdminChangePasswordView.postN)r5   r6   r7   r8   r   r9   r:   r   r2   r2   r2   r3   r<   Y   r;   r<   c                   @   r   )ForgotPasswordViewa  
    This is the ForgotPasswordView class responsible for handling password reset requests.

    It allows users to request a password reset by providing their email address. A password reset link
    with a time-limited token is then sent to the user's email address.

    Attributes:
        permission_classes (tuple): Specifies the permissions required to access this view, set to AllowAny for public access.
        http_method_names (list): The allowed HTTP methods, in this case, only POST is allowed.

    Methods:
        post(request, *args, **kwargs): Handles the password reset request, including generating and sending reset links.
    r   c           	      O   s   |j d}z
tjj|dd}W n ty"   tdditjd Y S w tt	|j
}t|}tj | d| }t|| tdditjdS )	a  
        Handles the password reset request, including generating and sending reset links.

        Args:
            request (HttpRequest): The HTTP request containing the user's email address.
            *args: Variable-length argument list.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            Response: A response indicating the success of the password reset request.
        r   Tr?   r    rA   r   /zCYou will receive an email with instructions to reset your password.)r   r"   r   r#   r   r   r	   r*   r   r   r&   r   
make_tokenr   FORGOT_PASSWORD_LINKr   rD   )	r+   r,   r-   r.   r   r0   uidr   Z	reset_urlr2   r2   r3   r      s$   


zForgotPasswordView.postNr4   r2   r2   r2   r3   rE      s
    rE   c                   @   s   e Zd ZdZefZdd ZdS )ResetPasswordViewa  
    This is the ResetPasswordView class responsible for handling password reset requests.

    It allows users to reset their passwords by providing a valid reset token along with a new password.

    Attributes:
        permission_classes (tuple): Specifies the permissions required to access this view, set to AllowAny for public access.

    Methods:
        post(request, uidb64, token): Handles the password reset logic.
    c           
   
   C   s  |j d}|j d}|du rtdditjdS |du r&tdditjdS t|}|d}z
tjj||d	}W n t	yK   tdd
itj
d Y S w z#t||rf|| |  tdditjdW S tdditjdW S  ty }	 ztdditjdW  Y d}	~	S d}	~	ww )az  
        Handles the password reset logic.

        Args:
            request (HttpRequest): The HTTP request containing the new password.
            uidb64 (str): A base64-encoded user ID for identification.
            token (str): A token for password reset

        Returns:
            Response: A response indicating the success or failure of the password reset.
        r>   r   Nr    zNew password requiredr   zEmail is requiredzutf-8)r&   r   zUser does not existz"Password has been reset successfulzInvalid or expired token)r   r"   r   r	   r)   r   decoder   r#   r   r*   r   check_tokenrB   rC   rD   	Exception)
r+   r,   Zuidb64r   r>   r   Zdecoded_bytesrI   r0   er2   r2   r3   r      sR   


zResetPasswordView.postN)r5   r6   r7   r8   r   r9   r   r2   r2   r2   r3   rJ      s    rJ   N)"django.confr   django.contrib.auth.tokensr   django.core.exceptionsr   django.utils.encodingr   django.utils.httpr   r   rest_frameworkr	   Zrest_framework.genericsr
   Zrest_framework.permissionsr   r   rest_framework.responser   rest_framework.viewsr   Zrest_framework_simplejwt.tokensr   Zapps.account.modelsr   apps.account.serializersr   #apps.account.services.mail_servicesr   r   r<   rE   rJ   r2   r2   r2   r3   <module>   s$    GC<