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	m
Z
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 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 G dd deZ!G dd deZ"dS )    )ObjectDoesNotExiststatus)action)RetrieveAPIView)AllowAnyIsAuthenticatedIsAdminUser)Response)models)StationCheckInListFilter)StationRatingListFilterStationRatingMediaListFilter)"station_checkIn_rating_serializers)AdminOrPostDeleteOnlyPermission)BaseModelViewSet)settingsc                   @   s:   e Zd ZdZejZejj	
 ZddgZdd Zdd ZdS )	CreateStationRatingModelViewSeta  
    This viewset allows authenticated users to create station rating records.

    Attributes:
        permission_classes (tuple): Permissions required to access this view (IsAuthenticated for authenticated users).
        serializer_class (StationRatingSerializer): The serializer used for input and output data.
        queryset (QuerySet): The base queryset for station rating records.
        http_method_names (list): The allowed HTTP methods, in this case, only POST and GET are allowed.

    Methods:
        get_queryset(self): Retrieves a queryset of station rating records for the requesting user.
        perform_create(serializer): Associates a newly created station rating record with the requesting user.
    postgetc                 C   s   | j j| jjdS )z
        Retrieves a queryset of station rating records for the requesting user.

        Returns:
            QuerySet: A filtered queryset containing station rating records associated with the user.
        user)querysetfilterrequestr   )self r   j/var/www/html/evchargy.com/django-station-ev/station_ev/apps/station/views/station_checkIn_rating_views.pyget_queryset)   s   z,CreateStationRatingModelViewSet.get_querysetc           
   	   O   s   t |jdkrtdditjdS | j|jd}|jdd |j| j	j
d g }t|jD ]\}}||jd	| d
|jd| d
d q,tj|dd|id}|jdd tjjj|jd d}	|j|	tjd | j|	d|id}t|jtjdS )aQ  
        Create a new StationRating instance and associated StationRatingAssets instances.

        Args:
            request (Request): The HTTP request object.
            *args: Variable-length argument list.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            Response: The HTTP response object.
           messageYou can upload up-to 5 imagesr   )dataT)raise_exceptionr   asset[]asset_type[asset
asset_typer   r"   manycontextid)pkrating_stationis_verified)r,   )lenFILESr
   r   HTTP_400_BAD_REQUESTget_serializerr"   is_validsaver   r   	enumerateappendr   r   RatingMediaSerializerr   StationRatingobjectsr   ALLOW_AUTO_VERIFY_MEDIAHTTP_201_CREATED)
r   r   argskwargs
serializerassetsindexfileassets_serializerstation_ratingr   r   r   create2   s2   
z&CreateStationRatingModelViewSet.createN)__name__
__module____qualname____doc__r   StationRatingSerializerserializer_classr   r;   r<   allr   http_method_namesr   rG   r   r   r   r   r      s    	r   c                   @   sF   e Zd ZdZeefZejZ	e
jj ZeZg dZdd Zdd ZdS )CreateRatingMediaModelViewSeta  
    This viewset allows authenticated users to upload media assets for a specific station rating.

    Attributes:
        permission_classes (tuple): Permissions required to access this view (IsAuthenticated for authenticated users).
        serializer_class (RatingMediaSerializer): The serializer used for handling media asset uploads.
        parser_classes (tuple): Parsers used for request data (MultiPartParser for file uploads).
        queryset (QuerySet): The base queryset for station rating media records.
        http_method_names (list): The allowed HTTP methods, in this case, only POST is allowed.

    Methods:
        create(request, *args, **kwargs): Handles the uploading of station media assets and
        associates them with a specific station rating.
    )r   r   patchdeletec              	   O   sR  | j d }ztj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 ]\}}||jd| d|jd	| dd
 q:|sbtdditj	dS |jjstjjj|j|d}	|	 t| dkrtdditj	dS | j|dd|id}
|
 r|
j|tjd t|
jtjdS t|
jtj	dS )a  
        Handles the uploading of station media assets and associates them with a specific station rating.

        Args:
            request (HttpRequest): The HTTP request object containing media assets and associated data.
            *args: Variable-length argument list.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            Response: A response indicating the success or failure of the media asset upload operation.

        This method processes the uploaded media assets, associates them with a specified station rating,
         and handles the creation
        of station media records. It verifies the provided check-in ID, asset data, and the associated user.
        	rating_id)r-   r    z?You do not have permission to upload other users' rating media.r   zInvalid rating IDr$   r%   r&   r'   zAssets are required)rating_station__userr0   r   r!   Tr   r*   r/   )r@   r   r;   r<   r   r   r   r
   r   r4   r   r8   r3   r9   r"   is_superuserStationRatingMediar   countr2   r5   r6   r7   r   r=   r>   errors)r   r   r?   r@   rS   Zcheckin_objrB   rC   rD   station_mediarA   r   r   r   rG   x   sh   


z$CreateRatingMediaModelViewSet.createc                 O   st   | j d}ztjjj||j| j dd}W n tjjy+   tdditj	d Y S w | 
| tdditjdS )a\  
        Remove a media from the user's media.

        Args:
            request (HttpRequest): The HTTP request object.
            *args: Variable-length argument list.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            Response: A response indicating the success or failure of the media removal operation.
        rS   r.   )r0   rT   r-   r    zMedia not foundr   zMedia deleted successful)r@   r   r   rV   r<   r   DoesNotExistr
   r   HTTP_404_NOT_FOUNDperform_destroyHTTP_204_NO_CONTENT)r   r   r?   r@   rS   Zrating_station_mediar   r   r   destroy   s$   



z%CreateRatingMediaModelViewSet.destroyN)rH   rI   rJ   rK   r   r   permission_classesr   r:   rM   r   rV   r<   rN   r   r   filterset_classrO   rG   r^   r   r   r   r   rP   b   s    NrP   c                   @   s:   e Zd ZdZefZejZe	j
j ZeZdgZdd ZdS )StationRatingModelViewSetas  
    This viewset allows public access to retrieve station rating records associated with a specific station.

    Attributes:
        permission_classes (tuple): Permissions required to access this view (AllowAny for public access).
        serializer_class (StationRatingSerializer): The serializer used for station rating records.
        queryset (QuerySet): The base queryset for station rating records.
        http_method_names (list): The allowed HTTP methods, in this case, only GET is allowed.

    Methods:
        get_queryset(): Retrieves station rating records for a specific station based on the station ID.
    r   c                 C   s   | j d }| jj|dS )az  
        Retrieves station rating records for a specific station based on the station ID.

        Returns:
            QuerySet: A filtered queryset containing station rating records associated with the specified station.

        This method filters the base queryset to retrieve station rating records
        associated with the station specified by the station ID.
        
station_id)station__id)r@   r   r   )r   rb   r   r   r   r      s   

z&StationRatingModelViewSet.get_querysetN)rH   rI   rJ   rK   r   r_   r   rL   rM   r   r;   r<   rN   r   r   r`   rO   r   r   r   r   r   ra      s    ra   c                   @   s(   e Zd ZdZefZejZe	j
j ZdS ) StationCheckInCalculationAPIViewz
    A view for retrieving Station Check-In calculations.
    This view allows retrieval of StationMaster data with specific permissions.
    N)rH   rI   rJ   rK   r   r_   r   Z#StationCheckInCalculationSerializerrM   r   StationMasterr<   rN   r   r   r   r   r   rd     s    rd   c                   @   s6   e Zd ZdZejZejj	
 ZeZddgZdd ZdS )StationCheckInModelViewSetz
    A viewset for handling Station Check-In operations.
    This viewset supports creating StationCheckIn objects via HTTP POST.
    r   r   c                 C   s   |j | jjd dS )a5  
        Perform the creation of a StationCheckIn object.
        This method is called when a new object is created.
        It associates the object with the current user.

        Args:
            serializer (Serializer): The serializer for creating the object.

        Returns:
            None
        r   N)r7   r   r   )r   rA   r   r   r   perform_create$  s   z)StationCheckInModelViewSet.perform_createN)rH   rI   rJ   rK   r   ZStationCheckInSerializerrM   r   StationCheckInr<   rN   r   r   r`   rO   rg   r   r   r   r   rf     s    rf   c                   @   sf   e Zd ZdZeefZejZ	e
jj ZeZg dZeddgdddd Zeddgd	dd
d ZdS )#UnverifiedStationRatingMediaViewSetab  
    This viewset provides endpoints to manage unverified station rating media.

    Attributes:
        permission_classes (tuple): Specifies the permissions required to access this view.
        serializer_class: The serializer class used for handling station rating media data.
        queryset (QuerySet): The queryset for accessing unverified station rating media records.
        filterset_class: The filterset class used for filtering station rating media records.
        http_method_names (list): Specifies the supported HTTP methods for this view,
        including 'get', 'delete', and 'patch'.
    )r   rR   rQ   FrR   )detailmethodsurl_pathc              
   O   s|   |j dg }ztjjj|d \}}td| ditj	dW S  t
y= } ztdt|itjdW  Y d}~S d}~ww )ag  
        Delete selected unverified rating media.

        Args:
            request (HttpRequest): The HTTP request object.
            *args: Variable-length argument list.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            Response: A response indicating the success or failure of the rating media deletion operation.
        	media_idsid__inr    z media deleted successfullyr   errorN)r"   r   r   rV   r<   r   rR   r
   r   r]   	ExceptionstrHTTP_500_INTERNAL_SERVER_ERROR)r   r   r?   r@   rm   deleted_count_er   r   r   delete_selected_mediaF  "   z9UnverifiedStationRatingMediaViewSet.delete_selected_mediarQ   verifiedc              
   O   s|   |j dg }ztjjj|djdd}td| ditj	dW S  t
y= } ztdt|itjdW  Y d	}~S d	}~ww )
ak  
        Verify selected unverified rating media.

        Args:
            request (HttpRequest): The HTTP request object.
            *args: Variable-length argument list.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            Response: A response indicating the success or failure of the rating media verification operation.
        rm   rn   T)r1   r    z media verified successfullyr   rp   N)r"   r   r   rV   r<   r   updater
   r   HTTP_200_OKrq   rr   rs   )r   r   r?   r@   rm   updated_countrv   r   r   r   verify_selected_mediab  rx   z9UnverifiedStationRatingMediaViewSet.verify_selected_mediaN)rH   rI   rJ   rK   r   r	   r_   r   r:   rM   r   rV   r<   rN   r   r   r`   rO   r   rw   r}   r   r   r   r   ri   3  s    
ri   N)#django.core.exceptionsr   rest_frameworkr   rest_framework.decoratorsr   Zrest_framework.genericsr   Zrest_framework.permissionsr   r   r	   rest_framework.responser
   Zapps.stationr   Z,apps.station.filters.station_checkin_filtersr   $apps.station.filters.station_filtersr   r   Zapps.station.serializersr   base.permissionsr   
base.viewsr   
station_evr   r   rP   ra   rd   rf   ri   r   r   r   r   <module>   s(    L 	"