
    ;7i                        d dl mZ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 dd	lmZmZmZmZmZ  G d
 dej2                        Z G d dej2                        Z G d dej8                        Z G d dej<                        Z G d dej8                        Z  G d dej8                        Z!y)    )viewsetsgenericsfiltersstatus)action)Response)AllowAny)timezone)DjangoFilterBackend   )CategoryNewsImageGallery)CategorySerializerNewsListSerializerNewsDetailSerializerNewsCreateUpdateSerializerImageGallerySerializerc                   p    e Zd ZdZej
                  j                         ZeZ	dZ
 eddg      dd       Zy)	CategoryViewSetzc
    Read-only ViewSet for Category model (GET only)
    Provides list and retrieve operations
    slugTgetdetailmethodsNc                     | j                         }|j                  j                  d      j                  d      }t	        |dd|i      }t        |j                        S )z*Get all news items for a specific categoryT	is_active-publish_daterequestmanycontext)
get_object
news_itemsfilterorder_byr   r   data)selfr    r   categoryr%   
serializers         /var/www/apihucms/news/views.pynewszCategoryViewSet.news   sW     ??$((//$/?HHY
'
	SZG[\

((    )N)__name__
__module____qualname____doc__r   objectsallquerysetr   serializer_classlookup_fieldr   r-    r.   r,   r   r      sC     ##%H)L4%)) *)r.   r   c                       e Zd ZdZej
                  j                  d      ZdZe	e
j                  e
j                  gZg dZg dZg dZdgZd	 Z ed
dg      d        Z ed
dg      d        Zy)NewsViewSetz|
    Read-only ViewSet for News model (GET only)
    Provides list and retrieve operations and read-only custom actions
    Tr   r   )r*   r   publish_date)titleshort_descriptiondescription)r;   
created_atr<   r   c                 `    | j                   dk(  rt        S | j                   dv rt        S t        S )z-Return appropriate serializer based on actionlist)createupdatepartial_update)r   r   r   r   )r)   s    r,   get_serializer_classz NewsViewSet.get_serializer_class0   s-    ;;& %%[[BB--##r.   Fr   r   c                     | j                   j                  d      dd }t        |dd|i      }t        |j                        S )z(Get featured/latest news items (limit 5)r   N   Tr    r!   )r5   r'   r   r   r(   )r)   r    featured_newsr+   s       r,   featuredzNewsViewSet.featured8   sB     ..?C'D9V]J^_

((r.   c           
      @   t         j                  j                         }g }|D ]o  }| j                  j	                  |      j                  d      dd }|j                  t        |      j                  t        |dd|i      j                  d       q t        |      S )	zGet news grouped by category)r*   r   NrG   Tr    r!   )r*   r-   )r   r3   r4   r5   r&   r'   appendr   r(   r   r   )r)   r    
categoriesresultr*   r%   s         r,   by_categoryzNewsViewSet.by_category?   s     %%))+
" 	H--x-@II/Z[]\]^JMM.x8==*:D9V]J^_dd 	 r.   N)r/   r0   r1   r2   r   r3   r&   r5   r7   r   r   SearchFilterOrderingFilterfilter_backendsfilterset_fieldssearch_fieldsordering_fieldsorderingrE   r   rI   rN   r8   r.   r,   r:   r:   #   s     ||""T"2HL*G,@,@'BXBXYO@AM=O H$ 55'*) +) 55'*
  +
 r.   r:   c                       e Zd ZdZej
                  j                  d      ZeZ	e
gZeej                  ej                  gZddgZddgZddgZd	gZy
)NewsListViewzF
    GET API for listing all active news
    Endpoint: /api/news/
    Tr   r*   r;   r<   r=   r?   r   N)r/   r0   r1   r2   r   r3   r&   r5   r   r6   r	   permission_classesr   r   rO   rP   rQ   rR   rS   rT   rU   r8   r.   r,   rW   rW   M   sn     ||""T"2H)"*G,@,@'BXBXYO"N312M%|4O Hr.   rW   c                   V    e Zd ZdZej
                  j                  d      ZeZ	e
gZdZy)NewsDetailViewzI
    GET API for news detail by slug
    Endpoint: /api/news/<slug>/
    Tr   r   N)r/   r0   r1   r2   r   r3   r&   r5   r   r6   r	   rX   r7   r8   r.   r,   rZ   rZ   \   s3     ||""T"2H+"Lr.   rZ   c                   N    e Zd ZdZej
                  j                         ZeZ	e
gZy)CategoryListViewzP
    GET API for listing all categories
    Endpoint: /api/news/categories/
    N)r/   r0   r1   r2   r   r3   r4   r5   r   r6   r	   rX   r8   r.   r,   r\   r\   g   s+     ##%H)"r.   r\   c                        e Zd ZdZeZegZd Zy)ImageGalleryListViewz
    Read-only image gallery list for a news item.
    Endpoint: /api/news/<news_id>/gallery/
    Only GET is allowed (public).
    c                 x    | j                   j                  d      }t        j                  j	                  |      S )Nnews_id)r`   )kwargsr   r   r3   r&   )r)   r`   s     r,   get_querysetz!ImageGalleryListView.get_querysetz   s.    ++//),##**7*;;r.   N)	r/   r0   r1   r2   r   r6   r	   rX   rb   r8   r.   r,   r^   r^   q   s    
 ."<r.   r^   N)"rest_frameworkr   r   r   r   rest_framework.decoratorsr   rest_framework.responser   rest_framework.permissionsr	   django.utilsr
   django_filters.rest_frameworkr   modelsr   r   r   serializersr   r   r   r   r   ReadOnlyModelViewSetr   r:   ListAPIViewrW   RetrieveAPIViewrZ   r\   r^   r8   r.   r,   <module>rn      s    > > , , / ! = 0 0 )h33 )$' (// ' T!8'' !X-- $x++ $<8// <r.   