o
    di                     @   sp   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 d dlmZ G d	d
 d
eZdS )    Nstatus)IsAuthenticated)Response)models)VehicleInsuranceListFilter)vehicle_insurance_serializers)BaseModelViewSetc                   @   sL   e Zd ZdZefZejZe	j
j ZeZddgZdd Zdd Zdd	 Zd
S )VehicleInsuranceModelViewSeta  
    A viewset for handling CRUD operations on VehicleInsurance instances.

    This viewset supports the standard CRUD operations - Create and Retrieve -
    for the VehicleInsurance model. It filters instances based on the authenticated user.

    Attributes:
        permission_classes (tuple): A tuple of permission classes.
        serializer_class: The serializer class for the VehicleInsurance model.
        queryset: The queryset containing all VehicleInsurance instances.
        filterset_class: The filterset class for supporting custom filtering.
        http_method_names (list): List of HTTP methods allowed on this viewset.
    getpostc                 C   s   | j j| jjdS )z
        Get the queryset of VehicleInsurance instances for the authenticated user.

        Returns:
            queryset: The filtered queryset of VehicleInsurance instances.
        user)querysetfilterrequestr   )self r   e/var/www/html/evchargy.com/django-station-ev/station_ev/apps/vehicle/views/vehicle_insurance_views.pyget_queryset"   s   z)VehicleInsuranceModelViewSet.get_querysetc           
      O   s   |j }i }| D ]\}}t|trt|dkr|d ||< q	|||< q	d|v r1t|d |d< d|v r8|d= t|}t|}t|jdkrQ|j	d|d< | j
|d}	|	 rk|	j| jjd t|	j tjdS t|	jtjdS )N   r   insurance_typespolicy)datar   r   )r   items
isinstancelistlenjsonloadsdumpsFILESr   get_serializeris_validsaver   r   r   r   HTTP_201_CREATEDerrorsHTTP_400_BAD_REQUEST)
r   r   argskwargsZrequest_data	data_dictkeyvalue	json_data
serializerr   r   r   create+   s2   


z#VehicleInsuranceModelViewSet.createc                 C   s   |j | jjd dS )z
        Perform custom actions when creating a new VehicleInsurance instance.

        Args:
            serializer: The serializer instance handling the create operation.
        r   N)r$   r   r   )r   r.   r   r   r   perform_createS   s   z+VehicleInsuranceModelViewSet.perform_createN)__name__
__module____qualname____doc__r   permission_classesr   ZVehicleInsuranceSerializersserializer_classr   VehicleInsuranceobjectsallr   r   filterset_classhttp_method_namesr   r/   r0   r   r   r   r   r
      s    	(r
   )r   rest_frameworkr   Zrest_framework.permissionsr   rest_framework.responser   Zapps.vehicler   $apps.vehicle.filters.vehicle_filtersr   Zapps.vehicle.serializersr   
base.viewsr	   r
   r   r   r   r   <module>   s    