o
    di                     @   sl   d dl 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 G dd deZG d	d
 d
eZdS )    N)serializers)models)ConnectorMasterSerializers)VehicleMakeMasterSerializersVehicleModelMasterSerializers)DynamicFieldsModelSerializerc                   @   s   e Zd ZdZG dd dZdS )UserVehicleAssetSerializera  
    Serializer class for serializing and deserializing UserVehicleAssets instances.

    This serializer class includes fields for 'id', 'user_vehicle', and 'asset'.
    'id' and 'user_vehicle' are marked as read-only.

    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.
    c                   @   s   e Zd ZejZdZddgZdS )zUserVehicleAssetSerializer.Meta)iduser_vehicleassetr	   r
   N)__name__
__module____qualname__r   UserVehicleAssetsmodelfieldsread_only_fields r   r   l/var/www/html/evchargy.com/django-station-ev/station_ev/apps/account/serializers/user_vehicle_serializers.pyMeta   s    r   N)r   r   r   __doc__r   r   r   r   r   r      s    r   c                       s   e Zd ZdZeddddgdZeddddgdZeddddgdZ	e
jdd	ZG d
d dZdd Zdd Z fddZ  ZS )UserVehicleSerializerac  
    This serializer is used for validating and deserializing user vehicle data and user profile updates.

    Fields:
        vehicle_make (str): The make of the user's vehicle.
        vehicle_model (str): The model of the user's vehicle.
        connector (str): The connector type of the user's vehicle.
        wheeler_type (str): The type of vehicle (e.g., two-wheeler, four-wheeler, etc.).
        manufacturing_year (int): The manufacturing year of the vehicle.
        vehicle_number (str): The vehicle registration number.
        asset (File): An optional asset (e.g., an image) associated with the vehicle.

    These fields are required and should not be blank or null.

    Inherits from DynamicFieldsModelSerializer to provide flexibility in specifying fields.

    Meta:
        model (UserVehicle): The model used for serialization and deserialization.
        fields (str): The fields to include in the serialization.
        read_only_fields (str): The fields that are read-only.

    Methods:
        update(instance, validated_data): Updates the user vehicle record, validating and saving changes.
    vehicle_makeTr	   name)source	read_onlyr   vehicle_model	connector)r   c                   @   s2   e Zd ZejZdZdZddiddiddidZdS )zUserVehicleSerializer.Meta)
r	   r   r   r   manufacturing_yearvehicle_numbervehicle_make_datavehicle_model_dataconnector_datamedia)r	   r"   
write_onlyT)r   r   r   N)	r   r   r   r   UserVehicler   r   r   extra_kwargsr   r   r   r   r   K   s    
r   c                 C   sB   t j j}d}||  kr|ksn td| d| d|S )z.
        Validate manufacturing year.
        i  z%Manufacturing year should be between z and .)datetimedatetodayyearr   ValidationError)selfvaluecurrent_yearZmin_yearr   r   r   validate_manufacturing_yearc   s   z1UserVehicleSerializer.validate_manufacturing_yearc                 C   sH   |j  }g }|D ]}|jr!|j| jd |jjd}|| q	|S )z
        Retrieve and format associated media assets.

        Args:
            obj: The station rating record.

        Returns:
            List of dictionaries with media asset details.
        request)r	   r   )user_vehicle_assetsallr   r	   contextbuild_absolute_uriurlappend)r-   objZmediasZ
media_datar#   Z
media_itemr   r   r   	get_mediaq   s   



zUserVehicleSerializer.get_mediac                    s8   | j d}|j|jkrtdtt| ||}|S )a  
        Update the user vehicle record, validating and saving changes.

        Args:
        instance (UserVehicle): The existing user vehicle record.
        validated_data (dict): Validated data containing updated vehicle details.

        Returns:
        UserVehicle: The updated user vehicle record.

        This method updates the user vehicle record, with validation checks to ensure that the user has
        permission to update the record.
        r1   z-You have no permission to update this vehicle)r4   getuserr   r,   superr   update)r-   instancevalidated_datar1   	__class__r   r   r=      s   zUserVehicleSerializer.update)r   r   r   r   r   r    r   r!   r   r"   r   SerializerMethodFieldr#   r   r0   r9   r=   __classcell__r   r   r@   r   r   %   s     


r   )r(   rest_frameworkr   Zapps.accountr   Z*apps.master.serializers.master_serializersr   Z,apps.vehicle.serializers.vehicle_serializersr   r   base.serializersr   r   r   r   r   r   r   <module>   s    