
    ]P9i~                        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 d dlmZ dd	lmZmZmZmZ dd
lmZmZmZmZmZmZ  G d de      Z G d dej:                        Z G d dej>                        Z  G d dej:                        Z! edg      d        Z" edg      d        Z# edg      d        Z$ G d dejJ                        Z&y)    )get_object_or_404)genericsstatusviewsets)api_view)Response)PageNumberPagination)AllowAny)Q   )NotificationSiteStatisticNotificationCategoryNotificationTag)NotificationListSerializerNotificationDetailSerializerSiteStatisticSerializer#NotificationStatsResponseSerializerNotificationCategorySerializerNotificationTagSerializerc                       e Zd ZdZdZdZy)NotificationPagination   	page_size2   N)__name__
__module____qualname__r   page_size_query_parammax_page_size     (/var/www/apihucms/notifications/views.pyr   r      s    I'Mr"   r   c                       e Zd ZdZeZeZd Zy)NotificationListViewz7
    List all active notifications with pagination
    c                    t         j                  j                  d      }| j                  j                  j                  dd       }|r|j                  |      }| j                  j                  j                  dd       }|r|j                  |      }| j                  j                  j                  dd       }|r%|j                         dk(  r|j                  d	      }| j                  j                  j                  d
d       }|r5|j                  t        |      t        |      z  t        |      z        }|j                  d      j                  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/   s         r#   get_querysetz!NotificationListView.get_queryset    s,   ''...> <<,,00TBhGH <<,,00TB9H <<,,00TB(F248H **..x>6*01V,-H &&z2CCD\]]r"   N)	r   r   r   __doc__r   serializer_classr   pagination_classr>   r!   r"   r#   r%   r%      s     2-^r"   r%   c                   V    e Zd ZdZej
                  j                  d      ZeZ	dZ
d Zy)NotificationDetailViewz2
    Retrieve a specific notification by slug
    Tr'   slugc                     | j                   j                  d      }t        t        j                  j                  d      j                  d      |d      S )NrD   r)   r3   T)rD   r(   )kwargsr8   r   r   r4   r:   r;   )r<   rD   s     r#   
get_objectz!NotificationDetailView.get_objectF   sG    {{v&   //
;LLMef
 	
r"   N)r   r   r   r?   r   r4   r5   r=   r   r@   lookup_fieldrG   r!   r"   r#   rC   rC   >   s2     ##**T*:H3L
r"   rC   c                   L    e Zd ZdZej
                  j                  d      ZeZ	y)SiteStatisticListViewz)
    List all active site statistics
    Tr'   N)
r   r   r   r?   r   r4   r5   r=   r   r@   r!   r"   r#   rJ   rJ   O   s(     $$++d+;H.r"   rJ   GETc                 J   t               }| |_        d|_        |j                         }t	               }|j                  ||       }t        |d      }t        j                  j                  d      }t        |d      }|r|j                  j                  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%   r6   format_kwargr>   r   paginate_querysetr   r   r4   r5   r   page	paginatorrO   get_next_linkget_previous_link	num_pagesnumberr   r   data)	r6   notification_viewnotifications_querysetrZ   paginated_notificationsnotification_serializerstatistics_querysetstatistics_serializerpagination_infos	            r#   notifications_and_statsrg   W   s    -. '%)".;;=&(I'99:PRYZ89PW[\ (//666F34GdS 
! ))//'')//1 ~~//99!--(( ')  055+00%  r"   c                     t         j                  j                         }t        |d      }t	        |j
                        S )z)
    Get all notification categories
    TrM   )r   r4   allr   r   r_   )r6   
categories
serializers      r#   notification_categoriesrl   {   s3    
 &--113J/
FJJOO$$r"   c                     t         j                  j                  dd      dd }t        |d      }t	        |j
                        S )zC
    Get only featured notifications (for homepage highlights)
    T)r(   r.   Nr   rM   )r   r4   r5   r   r   r_   )r6   rT   rk   s      r#   featured_notificationsrn      sC    
 !((//$D/QRTSTUM+MEJJOO$$r"   c                   T    e Zd ZdZej
                  j                         ZeZ	e
gZd Zy)NotificationTagViewSetaK  
    Read-only ViewSet for NotificationTag model (GET only)
    Provides list and retrieve operations
    
    Available endpoints:
    - GET /notification-tags/ - List all tags
    - GET /notification-tags/{id}/ - Retrieve a specific tag
    
    Query parameters:
    - ?search=tag_name - Filter tags by name (partial match)
    c                     t         j                  j                         }| j                  j                  j                  dd      }|r|j                  |      }|j                  d      S )zS
        Optionally filter tags by a search query
        ?search=tag_name
        r/   N)name__icontainsname)r   r4   ri   r6   r7   r8   r5   order_by)r<   r=   r/   s      r#   r>   z#NotificationTagViewSet.get_queryset   sX    
 #**..0**..x>v>H  ((r"   N)r   r   r   r?   r   r4   ri   r=   r   r@   r
   permission_classesr>   r!   r"   r#   rp   rp      s0    
 &&**,H0"	)r"   rp   N)'django.shortcutsr   rest_frameworkr   r   r   rest_framework.decoratorsr   rest_framework.responser   rest_framework.paginationr	   rest_framework.permissionsr
   django.db.modelsr   modelsr   r   r   r   serializersr   r   r   r   r   r   r   ListAPIViewr%   RetrieveAPIViewrC   rJ   rg   rl   rn   ReadOnlyModelViewSetrp   r!   r"   r#   <module>r      s    . 5 5 . , : /  V V 1 "^8// "^J
X55 
"/H00 / 
5'   F 
5'% % 
5'% %)X:: )r"   