o
    di2                     @   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	m
Z
 d dlmZ d dlm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status)CreateAPIView)MultiPartParser)IsAuthenticatedIsAdminUser)Response)csv_master_serializersc                   @   ,   e Zd ZdZefZejZe	e
fZdd ZdS )AmenitiesCSVUploadViewa  
    This is the AmenitiesCSVUploadView.

    It handles the CSV file upload for amenities data.

    Attributes:
        parser_classes (tuple): The parsers used for request data (MultiPartParser for handling file uploads).
        serializer_class (Serializer): The serializer class for the uploaded data.
        permission_classes (tuple): Permissions required to access this view (IsAuthenticated for authenticated users).

    Methods:
        post(request, *args, **kwargs): Handles the processing of uploaded CSV data.
    c           	         s   |j d}|stdditjdS |jds tdditjdS t|}t	|j
 ddg}t fd	d
|D rfdd | D }tj|dd}t| | r^|  tdditjdS t|jtjdS tddd| ditjdS )a*  
        Handles the processing of uploaded CSV data and creation of amenities.

        Args:
            request (HttpRequest): The HTTP request containing the uploaded CSV file.

        Returns:
            Response: A response indicating the success or failure of the CSV data upload.
        filemessageFile not providedr   .csvInvalid file formatnametype_idc                 3       | ]}| v V  qd S N .0colcolumnsr   ]/var/www/html/evchargy.com/django-station-ev/station_ev/apps/master/views/csv_master_views.py	<genexpr>>       z.AmenitiesCSVUploadView.post.<locals>.<genexpr>c                 S      g | ]\}}t |qS r   dict)r   _rowr   r   r   
<listcomp>?       z/AmenitiesCSVUploadView.post.<locals>.<listcomp>TdatamanyzAmenities uploaded successfullyFile must have ,  columns)FILESgetr   r   HTTP_400_BAD_REQUESTr   endswithpdread_csvlistr   alliterrowsr	   AmenitiesUploadCSVSerializersprintis_validsaveHTTP_200_OKerrorsjoin	selfrequestargskwargsr   dfrequired_columns	json_data
serializerr   r   r   post   s:   




zAmenitiesCSVUploadView.postN)__name__
__module____qualname____doc__r   parser_classesr	   r4   serializer_classr   r   permission_classesrD   r   r   r   r   r      s    r   c                   @   r
   )ConnectorCSVUploadViewa  
    This is the ConnectorCSVUploadView.

    It handles the CSV file upload for connectors data.

    Attributes:
        parser_classes (tuple): The parsers used for request data (MultiPartParser for handling file uploads).
        serializer_class (Serializer): The serializer class for the uploaded data.
        permission_classes (tuple): Permissions required to access this view (IsAuthenticated for authenticated users).

    Methods:
        post(request, *args, **kwargs): Handles the processing of uploaded CSV data.
    c           	         s   |j d}|stdditjdS |jds tdditjdS t|}t	|j
 g d}t fdd	|D rbd
d | D }tj|dd}| rZ|  tdditjdS t|jtjdS tddd| ditjdS )a  
        Handles the processing of uploaded CSV data.

        Args:
            request (HttpRequest): The HTTP request containing the uploaded CSV file.
            *args: Variable-length argument list.
            **kwargs: Variable-length keyword argument list.

        Returns:
            Response: The response message indicating whether the connectors were uploaded successfully.
        r   r   r   r   r   r   )r   r   
short_namec                 3   r   r   r   r   r   r   r   r      r   z.ConnectorCSVUploadView.post.<locals>.<genexpr>c                 S   r   r   r   r   r!   vr   r   r   r#      r$   z/ConnectorCSVUploadView.post.<locals>.<listcomp>Tr%   z Connectors uploaded successfullyr(   r)   r*   )r+   r,   r   r   r-   r   r.   r/   r0   r1   r   r2   r3   r	   ConnectorCSVUploadSerializersr6   r7   r8   r9   r:   r;   r   r   r   rD   k   s8   



zConnectorCSVUploadView.postNrE   rF   rG   rH   r   rI   r	   rP   rJ   r   r   rK   rD   r   r   r   r   rL   W   s    rL   c                   @   r
   )LocationCSVUploadViewa  
    This is the LocationCSVUploadView class responsible for handling CSV file uploads containing location data.

    It allows users to upload location data from a CSV file, which is then processed and saved to the database.

    Attributes:
        parser_classes (tuple): The parsers used for request data, including MultiPartParser for handling file uploads.
        serializer_class (Serializer): The serializer class for deserializing the uploaded data.
        permission_classes (tuple): Specifies the permissions required to access this view,
        set to IsAuthenticated for authenticated users.

    Methods:
        post(request, *args, **kwargs): Handles the processing of uploaded CSV data, validating,
        and saving it to the database.
    c           	         s   |j d}|stdditjdS |jds tdditjdS t|}t	|j
 ddg}t fd	d
|D rbdd | D }tj|dd}| rZ|  tdditjdS t|jtjdS tddd| ditjdS )ai  
        Handles the processing of uploaded CSV data.

        Args:
            request (HttpRequest): The HTTP request containing the uploaded CSV file.
            *args: Variable-length argument list.
            **kwargs: Variable-length keyword argument list.

        Returns:
            Response: A response message indicating whether the connectors
            were uploaded successfully or any errors encountered.

        This method processes the uploaded CSV file containing location data. It validates the data,
        checks for required columns, and saves the location data to the database.
        r   r   r   r   r   r   r   r   c                 3   r   r   r   r   r   r   r   r      r   z-LocationCSVUploadView.post.<locals>.<genexpr>c                 S   r   r   r   rN   r   r   r   r#      r$   z.LocationCSVUploadView.post.<locals>.<listcomp>Tr%   zLocations uploaded successfullyr(   r)   r*   )r+   r,   r   r   r-   r   r.   r/   r0   r1   r   r2   r3   r	   ZLocationsCSVUploadSerializersr6   r7   r8   r9   r:   r;   r   r   r   rD      s8   



zLocationCSVUploadView.postNrQ   r   r   r   r   rR      s    rR   c                   @   r
   )NetworkOperatorCSVUploadViewal  
    This is the NetworkOperatorCSVUploadView class responsible for handling CSV file uploads containing network operator data.

    It allows users to upload network operator data from a CSV file, which is then processed and saved to the database.

    Attributes:
        parser_classes (tuple): The parsers used for request data, including MultiPartParser for handling file uploads.
        serializer_class (Serializer): The serializer class for deserializing the uploaded data.
        permission_classes (tuple): Specifies the permissions required to access this view,
        set to IsAuthenticated for authenticated users.

    Methods:
        post(request, *args, **kwargs): Handles the processing of uploaded CSV data, validating,
        and saving it to the database.

    Meta:
        permissions (tuple): Permissions required to access this view (IsAuthenticated for authenticated users).
        serializer_class (NetworkOperatorUploadCSVSerializers): The serializer used for input/output data.
        parser_classes (tuple): Parsers used for request data, including MultiPartParser for handling file uploads.
    c           	         s   |j d}|stdditjdS |jds tdditjdS t|}t	|j
 ddg}t fd	d
|D rbdd | D }| j|dd}| rZ|  tdditjdS t|jtjdS tddd| ditjdS )a  
        Handles the processing of uploaded CSV data.

        Args:
            request (HttpRequest): The HTTP request containing the uploaded CSV file.
            *args: Variable-length argument list.
            **kwargs: Variable-length keyword argument list.

        Returns:
            Response: The response message indicating whether the network operators were uploaded successfully.

        This method processes a CSV file containing network operator data, validates the data, and saves it to the database.
        It checks if the required file format and columns are present in the CSV file and handles the uploading process.
        r   r   r   r   r   r   r   r   c                 3   r   r   r   r   r   r   r   r   :  r   z4NetworkOperatorCSVUploadView.post.<locals>.<genexpr>c                 S   r   r   r   rN   r   r   r   r#   ;  r$   z5NetworkOperatorCSVUploadView.post.<locals>.<listcomp>Tr%   z&Network Operator uploaded successfullyr(   r)   r*   )r+   r,   r   r   r-   r   r.   r/   r0   r1   r   r2   r3   get_serializerr6   r7   r8   r9   r:   r;   r   r   r   rD     s4   



z!NetworkOperatorCSVUploadView.postN)rE   rF   rG   rH   r   rI   r	   Z#NetworkOperatorUploadCSVSerializersrJ   r   r   rK   rD   r   r   r   r   rS      s    rS   )pandasr/   rest_frameworkr   Zrest_framework.genericsr   Zrest_framework.parsersr   Zrest_framework.permissionsr   r   rest_framework.responser   Zapps.master.serializersr	   r   rL   rR   rS   r   r   r   r   <module>   s    LNT