o
    di                     @   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 d dl	m
Z
 d dlmZ d dlmZ d	d
 ZG dd deZG dd dZdS )    N)ValidationError)UploadedFile)BaseValidator)HttpResponseBadRequest)status)json)FileFieldConstantsc                 C   s   d}t t|| S )Nzzapi/v1/(vehicle/insurance|master/upload/(amenities|connectors|locations|network)/csv|station/upload/csv|vehicle/insurance))boolresearch)stringpattern r   K/var/www/html/evchargy.com/django-station-ev/station_ev/base/middlewares.pycheck_path_is_allowed   s   r   c                   @   s2   e Zd ZdZddej dZdefddZ	dS )	FileValidatora<  
    Validates the file size and extension of an uploaded file.

    Args:
        message (str, optional): The error message to display if the file is invalid.
                                 Defaults to a message indicating the allowed file formats
                                 and the maximum file size.
    zGInvalid file type or exceeds maximum file size. (Allowed file formats: z, z, maximum file size: 15 MB)valuec                 C   sB   |j tjkrt| j|jdd  }|tjvrt| jdS )z
        Validates the given `value` (an UploadedFile instance).

        Args:
            value (UploadedFile): The file to validate.

        Raises:
            ValidationError: If the file is invalid.
        .N)	sizer   	FILE_SIZEr   messagenamesplitlowerIMAGE_FORMATS)selfr   extr   r   r   __call__!   s   


zFileValidator.__call__N)
__name__
__module____qualname____doc__joinr   r   r   r   r   r   r   r   r   r      s    

r   c                   @   s    e Zd ZdZdd Zdd ZdS )FileValidationMiddlewarez
    Middleware that validates uploaded files.

    Args:
        get_response (callable): The callable to be called after processing the request.
    c                 C   s
   || _ dS )z
        Initializes the middleware with the given `get_response` callable.

        Args:
            get_response (callable): The callable to be called after processing the request.
        N)get_response)r   r%   r   r   r   __init__<   s   
z!FileValidationMiddleware.__init__c                 C   s   t |jsJ|jdv rJ|j D ]:\}}t|trIz	ttj	| W q t
yH } ztjd|jd d}tt|ddW  Y d}~  S d}~ww q| |S )z
        Processes the incoming request, validating uploaded files if necessary.

        Args:
            request (HttpRequest): The HTTP request to process.

        Returns:
            HttpResponse: The response to return to the client.
        )POSTPUTPATCHBAD_REQUESTr   )coder   r   zapplication/json)content_typeN)r   pathmethodFILESitems
isinstancer   r   r   r   r   r   HTTP_400_BAD_REQUESTmessagesr   r   dumpsr%   )r   request
field_namer   eresponser   r   r   r   E   s$   





z!FileValidationMiddleware.__call__N)r   r    r!   r"   r&   r   r   r   r   r   r$   4   s    	r$   )r
   django.core.exceptionsr   django.core.files.uploadedfiler   Zdjango.core.validatorsr   django.httpr   rest_frameworkr   Zrest_framework.utilsr   base.constantsr   r   r   r$   r   r   r   r   <module>   s    "