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
 d dlmZ G dd deZd	S )
    )timezone)serializers)models)insurance_serializers)VehicleInsurance)DynamicFieldsModelSerializerc                       sx   e Zd ZdZejddddgdZejddddgddZe	j
dd	ZG d
d dZ fddZ fddZdd Z  ZS )VehicleInsuranceSerializersaN  
    Serializer class for serializing and deserializing VehicleInsurance instances.

    This serializer class includes fields for 'id', 'user_vehicle', 'insurance_company',
    'insurance_type', 'user', 'start_date', 'end_date', and 'policy'. It also
    dynamically filters the 'insurance_type' field based on the selected 'insurance_company'.

    Attributes:
        Meta.model: The model class to which this serializer applies.
        Meta.fields: The fields to include in the serialized representation.
        Meta.read_only_fields: The fields that should be treated as read-only during updates.

        insurance_company_data: Serializer field for 'insurance_company' with read-only fields 'id' and 'name'.
        insurance_type_data: Serializer field for 'insurance_type' with read-only fields 'id' and 'name'.
        is_insurance_expired: Serializer method field indicating whether the insurance has expired.

    Methods:
        __init__: Initialize the VehicleInsuranceSerializers instance. Dynamically filter the 'insurance_type'
            field queryset based on the selected 'insurance_company'.

        to_representation: Override the default to_representation method to add 'file_size' to the serialized representation.

        get_is_insurance_expired: Method to determine if the insurance has expired based on the 'end_date'.
    insurance_companyTidname)source	read_onlyfieldsinsurance_types)r   r   r   many)r   c                   @   s4   e Zd ZeZdZddgZddiddiddidZdS )z VehicleInsuranceSerializers.Meta)r
   user_vehicler	   r   user
start_dateend_dateinsurance_company_datainsurance_types_datais_insurance_expiredpolicyr
   r   
write_onlyT)r   r	   r   N)__name__
__module____qualname__r   modelr   read_only_fieldsextra_kwargs r    r    q/var/www/html/evchargy.com/django-station-ev/station_ev/apps/vehicle/serializers/vehicle_insurance_serializers.pyMeta,   s    
r"   c                    sr   t t| j|i | d|v r3d|d v r5|d d }d|jv r7|jd }tjjj|d| jd _	dS dS dS dS )a%  
        Initialize the VehicleInsuranceSerializers instance.

        Dynamically filter the 'insurance_type' field queryset based on the selected 'insurance_company'.

        Args:
            *args: Variable length argument list.
            **kwargs: Arbitrary keyword arguments.
        contextrequestr	   )r	   r   N)
superr   __init__datamaster_modelsInsuranceTypeMasterobjectsfilterr   queryset)selfargskwargsr$   Zselected_company	__class__r    r!   r&   B   s   


z$VehicleInsuranceSerializers.__init__c                    sJ   t  |}|jj}|dkr|d dd}n|d dd}||d< |S )a  
        Override the default to_representation method to add 'file_size' to the serialized representation.

        Args:
            instance: The instance being serialized.

        Returns:
            dict: The serialized representation of the instance.
        i   z.2fz MBi   z KB	file_size)r%   to_representationr   size)r-   instancerepresentationZfile_size_bytesr2   r0   r    r!   r3   Y   s   
z-VehicleInsuranceSerializers.to_representationc                 C   s   t   }|j|k S )z
        Method to determine if the insurance has expired based on the 'end_date'.

        Args:
            obj: The VehicleInsurance instance.

        Returns:
            bool: True if the insurance has expired, False otherwise.
        )r   nowdater   )r-   objZcurrent_dater    r    r!   get_is_insurance_expiredr   s   

z4VehicleInsuranceSerializers.get_is_insurance_expired)r   r   r   __doc__r   !InsuranceCompanyMasterSerializersr   InsuranceTypeMasterSerializersr   r   SerializerMethodFieldr   r"   r&   r3   r:   __classcell__r    r    r0   r!   r   
   s    
r   N)django.utilsr   rest_frameworkr   Zapps.masterr   r(   Zapps.master.serializersr   Zapps.vehicle.modelsr   base.serializersr   r   r    r    r    r!   <module>   s    