o
    di!                     @   s   d dl 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j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G dd deZdS )    N)settings)BaseUserManager)ObjectDoesNotExist)models)timezone)send_opt_verification_mailc                       sH   e Zd ZdZdd ZdddZdddZ fd	d
Zedd Z	  Z
S )UserManagera4  
    Custom manager for the User model.

    Methods:
        create_user(email, password, **extra_fields): Creates and saves a regular user.
        create_superuser(email, password, **extra_fields): Creates and saves a superuser.
        lowercase_email(email): Converts an email address to lowercase.
    c                 K   s:   |  |}| jdd|i|}|| |j| jd |S )a  
        Creates and saves a User with the given email and password.

        Args:
        email (str): The user's email address.
        password (str): The user's password.
        **extra_fields: Additional fields for the user.

        Returns:
        User: The created User instance.

        This method is used to create and save a regular user with the given email and password.
        email)usingN )lowercase_emailmodelset_passwordsave_db)selfr	   passwordextra_fieldsuserr   r   ]/var/www/html/evchargy.com/django-station-ev/station_ev/apps/account/managers/user_manager.py_create_user   s
   

zUserManager._create_userNc                 K   s,   | dd | dd | j||fi |S )a  
        Creates and saves a regular user.

        Args:
        email (str): The user's email address.
        password (str): The user's password.
        **extra_fields: Additional fields for the user.

        Returns:
        User: The created regular User instance.

        This method is used to create and save a regular user with the given email, password, and optional
        extra fields. By default, regular users are not staff members or superusers.
        is_staffFis_superuser)
setdefaultr   r   r	   r   r   r   r   r   create_user+   s   zUserManager.create_userc                 K   sb   | dd | dd |ddurtd|ddur"td| |}| j||fi |S )a  
        Creates and saves a superuser.

        Args:
        email (str): The superuser's email address.
        password (str): The superuser's password.
        **extra_fields: Additional fields for the superuser.

        Returns:
        User: The created superuser User instance.

        This method is used to create and save a superuser with the given email, password, and optional
        extra fields. Superusers have staff and superuser privileges.
        r   Tr   z"Superuser must have is_staff=True.z&Superuser must have is_superuser=True.)r   get
ValueErrorr   r   r   r   r   r   create_superuser?   s   
zUserManager.create_superuserc                       t   jddS )a
  
        Override the default queryset to filter out removed users.

        Returns:
        QuerySet: The filtered queryset of non-removed users.

        This method overrides the default queryset to filter out users who have been removed (soft deleted).
        FZ
is_removedsuperget_querysetfilterr   	__class__r   r   r#   Z   s   	zUserManager.get_querysetc                 C   s0   | pd} z	|    } W | S  ty   Y | S w )a;  
        Convert an email address to lowercase.

        Args:
        email (str): The email address to convert.

        Returns:
        str: The lowercase email address.

        This method is used to convert an email address to lowercase to
        ensure case-insensitive matching when users log in.
         )striplowerr   )r	   r   r   r   r   e   s   zUserManager.lowercase_email)NN)__name__
__module____qualname____doc__r   r   r   r#   staticmethodr   __classcell__r   r   r&   r   r      s    	

r   c                   @   s   e Zd ZdZdd ZdS )UserCodeManagerz
    Custom manager for the UserCode model.

    Methods:
        create_or_update_code(user): Create or update a UserCode instance for the given user.
    c                 C   sx   t  }z| j|d}W n ty   | j|dd}Y nw |r:tjr0dtj	ddd}||_
nd|_
||_|  |S )a  
        Create or update a UserCode instance for the given user.

        Args:
        user (User): The user for whom the verification code is created or updated.

        Returns:
        UserCode: The UserCode instance associated with the user.

        This method is used to create or update a UserCode instance for a user. It generates
        a verification code and updates the timestamp.
        )r   i@ )r   coder(   
1234567890   )k)r   nowr   r   creater   IS_PRODUCTIONjoinrandomchoicesr2   	timestampr   )r   r   Zdate_updated	user_codeZ
code_valuer   r   r   create_or_update_code   s    
z%UserCodeManager.create_or_update_codeN)r+   r,   r-   r.   r>   r   r   r   r   r1   {   s    r1   c                           e Zd ZdZ fddZ  ZS )UserVehicleManagerz
    Custom manager for the UserVehicle model.

    Methods:
        get_queryset(): Override the default queryset to filter out removed users.
    c                    r   )a`  
        Override the default queryset to filter out removed users.

        Returns:
        QuerySet: The filtered queryset of user vehicle records.

        This method is used to customize the default queryset for the UserVehicle model.
        It filters out user vehicle records associated with users marked as removed (is_removed=True).
        Fr    r!   r%   r&   r   r   r#      s   
zUserVehicleManager.get_querysetr+   r,   r-   r.   r#   r0   r   r   r&   r   r@      s    r@   c                       r?   )UserVehicleAssetManagera  
    Custom manager for the UserVehicleAsset model.

    This manager provides additional methods for querying UserVehicleAsset instances.

    Methods:
        get_queryset: Returns a queryset of UserVehicleAsset instances with 'is_removed' set to False.
    c                    r   )z
        Get a queryset of UserVehicleAsset instances with 'is_removed' set to False.

        Returns:
            QuerySet: The queryset of UserVehicleAsset instances.
        Fr    r!   r%   r&   r   r   r#         z$UserVehicleAssetManager.get_querysetrA   r   r   r&   r   rB          	rB   c                       r?   )ContactUsManagerz
    Custom manager for the ContactUs model.

    This manager provides additional methods for querying ContactUs instances.

    Methods:
        get_queryset: Returns a queryset of ContactUs instances with 'is_removed' set to False.
    c                    r   )z
        Get a queryset of ContactUs instances with 'is_removed' set to False.

        Returns:
            QuerySet: The queryset of ContactUs instances.
        Fr    r!   r%   r&   r   r   r#      rC   zContactUsManager.get_querysetrA   r   r   r&   r   rE      rD   rE   c                       r?   )ContactUsAssetsManagera  
    Custom manager for the ContactUsAssets model.

    This manager provides additional methods for querying ContactUsAssets instances.

    Methods:
        get_queryset: Returns a queryset of ContactUsAssets instances with 'is_removed' set to False.
    c                    r   )z
        Get a queryset of ContactUsAssets instances with 'is_removed' set to False.

        Returns:
            QuerySet: The queryset of ContactUsAssets instances.
        Fr    r!   r%   r&   r   r   r#      rC   z#ContactUsAssetsManager.get_querysetrA   r   r   r&   r   rF      rD   rF   c                       r?   )NewsLetterSubscriberManagera  
    Custom manager for the NewsLetterSubscriber model.

    This manager provides additional methods for querying NewsLetterSubscriber instances.

    Methods:
        get_queryset: Returns a queryset of NewsLetterSubscriber instances with 'is_removed' set to False.
    c                    r   )z
        Get a queryset of NewsLetterSubscriber instances with 'is_removed' set to False.

        Returns:
            QuerySet: The queryset of NewsLetterSubscriber instances.
        Fr    r!   r%   r&   r   r   r#   
  rC   z(NewsLetterSubscriberManager.get_querysetrA   r   r   r&   r   rG      rD   rG   )r:   django.confr   django.contrib.auth.modelsr   django.core.exceptionsr   	django.dbr   django.utilsr   Z#apps.account.services.mail_servicesr   r   Managerr1   r@   rB   rE   rF   rG   r   r   r   r   <module>   s    o4