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mZ d dlmZ d dlmZ G d	d
 d
eZG dd deZdS )    )ObjectDoesNotExiststatus)MultiPartParser)Response)user_vehicle_filters)UserVehicleUserVehicleAssets)user_vehicle_serializers)BaseModelViewSetc                   @   sN   e Zd ZdZejZej	 Z
ejZg dZdd Zdd Zdd Zd	d
 ZdS )UserVehicleModelViewSeta  
    This view manages user vehicle records, allowing user profile retrieval and vehicle management.

    Attributes:
        permission_classes (tuple): Permissions required to access this view (IsAuthenticated for authenticated users).
        parser_classes (tuple): Parsers used for request data (MultiPartParser for file uploads).
        serializer_class (user_vehicle_serializers.UserVehicleSerializer): The serializer class for user vehicle data.
        queryset (QuerySet): The set of user vehicle records that this viewset operates on.
        http_method_names (list): The allowed HTTP methods (POST, PUT, GET, DELETE are allowed).

    Methods:
        list(request): Retrieves a list of user vehicles (Not allowed, returns an error response).
        retrieve(request): Retrieves a specific user's vehicle details with permission checks.
        destroy(request): Deletes a specific user's vehicle with permission checks.
    )postputgetdeletec                 C   s   | j j| jjdS )a  
        Filter user vehicle records based on the requesting user.

        Returns:
        QuerySet: User vehicle records belonging to the requesting user.

        This method filters the queryset to retrieve user vehicle records specific to the requesting user.
        user)querysetfilterrequestr   )self r   `/var/www/html/evchargy.com/django-station-ev/station_ev/apps/account/views/user_vehicle_views.pyget_queryset"   s   	z$UserVehicleModelViewSet.get_querysetc                 C   s   |j | jjd dS )a%  
        Perform user vehicle record creation, associating it with the requesting user.

        Args:
        serializer (UserVehicleSerializer): The serializer for creating a new user vehicle.

        This method associates the created user vehicle record with the requesting user.
        r   N)saver   r   )r   
serializerr   r   r   perform_create-   s   
z&UserVehicleModelViewSet.perform_createc                 O   sB   |   }|j|jkrtdditjdS | j|d|id}t|jS )a  
        Retrieve a specific user's vehicle details with permission checks.

        Args:
        request (HttpRequest): The HTTP request for retrieving a user's vehicle.

        Returns:
        Response: The user's vehicle details or an error response if permission is denied.

        This method retrieves the details of a specific user's vehicle, with permission checks to ensure
        that the requesting user has access to the requested vehicle.
        errorz4You do not have permission to see other user vehicler   r   )context)
get_objectr   r   r   HTTP_403_FORBIDDENserializer_classdata)r   r   argskwargsinstancer   r   r   r   retrieve9   s   
z UserVehicleModelViewSet.retrievec                 O   s@   |   }|j|jkrtddiddS | | tdditjdS )a  
        Delete a specific user's vehicle with permission checks.

        Args:
        request (HttpRequest): The HTTP request for deleting a user's vehicle.

        Returns:
        Response: A success message or an error response if permission is denied.

        This method deletes a specific user's vehicle, with permission checks to ensure that the
        requesting user has the authority to delete the vehicle.
        messagez#You can't delete other user vehicle  r   zVehicle deleted successfully)r   r   r   perform_destroyr   HTTP_204_NO_CONTENTr   r   r#   r$   r%   r   r   r   destroyQ   s   
zUserVehicleModelViewSet.destroyN)__name__
__module____qualname____doc__r
   ZUserVehicleSerializerr!   r   objectsallr   r   ZUserVehicleListFilterfilterset_classhttp_method_namesr   r   r&   r,   r   r   r   r   r      s    
r   c                   @   s>   e Zd ZdZejZefZe	j
 ZddgZdd Zdd ZdS )	UserVehicleAssetsModelViewSeta_  
    A viewset for handling CRUD operations on UserVehicleAssets instances.

    This viewset supports the creation and deletion of UserVehicleAssets instances.
    It includes validation to ensure that users can only upload and delete their own
    vehicle media.

    Attributes:
        serializer_class: The serializer class for the UserVehicleAssets model.
        parser_classes (tuple): A tuple of parser classes, including MultiPartParser.
        queryset: The queryset containing all UserVehicleAssets instances.
        http_method_names (list): List of HTTP methods allowed on this viewset.
    r   r   c           
   	   O   s  | j d}ztjj|d}|j| jjkrtdditjdW S W n t	y2   tdditj
d Y S w g }t|jD ]\}}|d|jd| d	i q:|j t| d
krbtdditjdS |smtdditjdS | j|dd}	|	 r|	j|d tdditjdS t|	jtjdS )a.  
        Create UserVehicleAssets instances.

        Handles HTTP POST method to create UserVehicleAssets instances.
        Validates permissions to ensure users can only upload their own vehicle media.

        Returns:
            Response: The HTTP response indicating success or failure.
        user_vehicle_id)idr'   z?You do not have permission to upload other users' rating media.r   zUser vehicle dose not existassetzasset[]   zYou can upload up-to 5 imageszAssets are requiredT)r"   many)user_vehiclez$Vehicle media uploaded successfully.)r$   r   r   r1   r   r   r   r   HTTP_400_BAD_REQUESTr   HTTP_404_NOT_FOUND	enumerateFILESappenduser_vehicle_assetscountlenget_serializeris_validr   HTTP_201_CREATEDerrors)
r   r   r#   r$   r6   r<   Zassetsindexfiler   r   r   r   create   sV   

z$UserVehicleAssetsModelViewSet.createc                 O   sB   |   }|j|jjkrtddiddS | | tdditjdS )a0  
        Delete UserVehicleAssets instances.

        Handles HTTP DELETE method to delete UserVehicleAssets instances.
        Validates permissions to ensure users can only delete their own vehicle media.

        Returns:
            Response: The HTTP response indicating success or failure.
        r'   z)You can't delete other user vehicle mediar(   r   z"Vehicle media deleted successfully)r   r   r<   r   r)   r   r*   r+   r   r   r   r,      s   

z%UserVehicleAssetsModelViewSet.destroyN)r-   r.   r/   r0   r
   ZUserVehicleAssetSerializerr!   r   parser_classesr	   r1   r2   r   r4   rK   r,   r   r   r   r   r5   o   s    
>r5   N)django.core.exceptionsr   rest_frameworkr   Zrest_framework.parsersr   rest_framework.responser   Zapps.account.filtersr   Zapps.account.modelsr   r	   apps.account.serializersr
   
base.viewsr   r   r5   r   r   r   r   <module>   s    c