o
    6]h                     @   s   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 ddlmZmZmZ dd	lmZmZmZmZmZ G d
d de
ZG dd dejZG dd dejZG dd dejZedgdd Zedgdd Zedgdd ZdS )    )get_object_or_404)genericsstatus)api_view)Response)PageNumberPagination)Q   )NotificationSiteStatisticNotificationCategory)NotificationListSerializerNotificationDetailSerializerSiteStatisticSerializer#NotificationStatsResponseSerializerNotificationCategorySerializerc                   @   s   e Zd ZdZdZdZdS )NotificationPagination   	page_size2   N)__name__
__module____qualname__r   page_size_query_parammax_page_size r   r   (/var/www/apihucms/notifications/views.pyr      s    r   c                   @   s    e Zd ZdZeZeZdd ZdS )NotificationListViewz7
    List all active notifications with pagination
    c                 C   s   t jjdd}| jjdd }|r|j|d}| jjdd }|r'|j|d}| jjdd }|r=| dkr=|jdd	}| jjd
d }|rY|t|dt|dB t|dB }|d	dS )NT	is_activecategory)category__name__iexactpriority)r"   featuredtrue)is_featuredsearch)title__icontains)description__icontains)content__icontainsnotification_tags__tag)
r
   objectsfilterrequestquery_paramsgetlowerr   select_relatedprefetch_related)selfquerysetr    r"   r#   r&   r   r   r   get_queryset   s(   z!NotificationListView.get_querysetN)	r   r   r   __doc__r   serializer_classr   pagination_classr5   r   r   r   r   r      s
    r   c                   @   s.   e Zd ZdZejjddZeZ	dZ
dd ZdS )NotificationDetailViewz2
    Retrieve a specific notification by slug
    Tr   slugc                 C   s(   | j d}ttjdd|ddS )Nr:   r    r*   T)r:   r   )kwargsr/   r   r
   r+   r1   r2   )r3   r:   r   r   r   
get_objectD   s   z!NotificationDetailView.get_objectN)r   r   r   r6   r
   r+   r,   r4   r   r7   lookup_fieldr<   r   r   r   r   r9   <   s    r9   c                   @   s"   e Zd ZdZejjddZeZ	dS )SiteStatisticListViewz)
    List all active site statistics
    Tr   N)
r   r   r   r6   r   r+   r,   r4   r   r7   r   r   r   r   r>   M   s    r>   GETc           	      C   s   t  }| |_d|_| }t }||| }t|dd}tjj	dd}t
|dd}|rA|jjj| | |jjj|jj|jdni }t|j|j|dS )zP
    Combined endpoint to get notifications and statistics for the homepage
    NTmanyr   )countnextprevioustotal_pagescurrent_pager   )notifications
statistics
pagination)r   r-   format_kwargr5   r   paginate_querysetr   r   r+   r,   r   page	paginatorrB   get_next_linkget_previous_link	num_pagesnumberr   r   data)	r-   notification_viewnotifications_querysetrM   paginated_notificationsnotification_serializerstatistics_querysetstatistics_serializerpagination_infor   r   r   notifications_and_statsU   s2   
	rZ   c                 C   s    t j }t|dd}t|jS )z)
    Get all notification categories
    Tr@   )r   r+   allr   r   rR   )r-   
categories
serializerr   r   r   notification_categoriesy   s   

r^   c                 C   s.   t jjddddd }t|dd}t|jS )zC
    Get only featured notifications (for homepage highlights)
    T)r   r%   Nr   r@   )r
   r+   r,   r   r   rR   )r-   rG   r]   r   r   r   featured_notifications   s   
r_   N) django.shortcutsr   rest_frameworkr   r   rest_framework.decoratorsr   rest_framework.responser   rest_framework.paginationr   django.db.modelsr   modelsr
   r   r   serializersr   r   r   r   r   r   ListAPIViewr   RetrieveAPIViewr9   r>   rZ   r^   r_   r   r   r   r   <module>   s$    	%
#
	