You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.6 KiB
49 lines
1.6 KiB
from rest_framework import generics, status
|
|
from rest_framework.response import Response
|
|
from .models import VideoCategory, Video
|
|
from .serializers import VideoCategoryListSerializer, VideoListSerializer, VideoDetailSerializer
|
|
|
|
|
|
class VideoCategoryListAPIView(generics.ListAPIView):
|
|
"""
|
|
API view to list all video categories with their video counts
|
|
"""
|
|
serializer_class = VideoCategoryListSerializer
|
|
|
|
def get_queryset(self):
|
|
return VideoCategory.objects.filter(status=True).order_by('order')
|
|
|
|
|
|
class VideoListAPIView(generics.ListAPIView):
|
|
"""
|
|
API view to list all videos, with optional category filtering
|
|
"""
|
|
serializer_class = VideoListSerializer
|
|
|
|
def get_queryset(self):
|
|
queryset = Video.objects.filter(status=True).order_by('-created_at')
|
|
|
|
# Filter by category if provided
|
|
category_slug = self.request.query_params.get('category', None)
|
|
if category_slug:
|
|
queryset = queryset.filter(category__slug=category_slug)
|
|
|
|
return queryset
|
|
|
|
|
|
class VideoDetailAPIView(generics.RetrieveAPIView):
|
|
"""
|
|
API view to get video details, including related videos from the same collection
|
|
"""
|
|
serializer_class = VideoDetailSerializer
|
|
lookup_field = 'slug'
|
|
|
|
def get_queryset(self):
|
|
return Video.objects.filter(status=True)
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
instance = self.get_object()
|
|
# Increment view count
|
|
instance.increment_view_count()
|
|
serializer = self.get_serializer(instance)
|
|
return Response(serializer.data)
|