from rest_framework.generics import ListAPIView, RetrieveAPIView from drf_yasg.utils import swagger_auto_schema from drf_yasg import openapi from rest_framework.exceptions import NotFound from apps.course.serializers import ( CourseListSerializer, CourseCategorySerializer, CourseDetailSerializer, AttachmentSerializer, GlossarySerializer ) from apps.course.models import Course, CourseCategory, Attachment, Glossary class CourseCategoryAPIView(ListAPIView): queryset = CourseCategory.objects.all() serializer_class = CourseCategorySerializer class CourseListAPIView(ListAPIView): queryset = Course.objects.all().exclude(status=Course.StatusChoices.INACTIVE) serializer_class = CourseListSerializer # filterset_fields = ['category__slug',] @swagger_auto_schema(manual_parameters=[ openapi.Parameter( 'category_slug', openapi.IN_QUERY, description="Category of the Course", type=openapi.TYPE_STRING, enum=[category.slug for category in CourseCategory.objects.all()] ), ]) def get(self, request, *args, **kwargs): return super().get(request, *args, **kwargs) def get_queryset(self): queryset = super().get_queryset() request = self.request filters = request.query_params if category := filters.get('category_slug'): queryset = queryset.filter(category__slug=category) return queryset class CourseDetailAPIView(RetrieveAPIView): queryset = Course.objects.all() serializer_class = CourseDetailSerializer lookup_field = "slug" class AttachmentListAPIView(ListAPIView): serializer_class = AttachmentSerializer @swagger_auto_schema( manual_parameters=[ openapi.Parameter( 'slug', openapi.IN_PATH, description="Slug of the Course", type=openapi.TYPE_STRING, required=True ) ], operation_description="Retrieve a list of attachments for a given course by its slug." ) def get(self, request, *args, **kwargs): return super().get(request, *args, **kwargs) def get_queryset(self): course_slug = self.kwargs.get('slug') try: course = Course.objects.get(slug=course_slug) except Course.DoesNotExist: raise NotFound("Course not found") return Attachment.objects.filter(course=course) class GlossaryListAPIView(ListAPIView): serializer_class = GlossarySerializer def get_queryset(self): course_slug = self.kwargs.get('slug') try: course = Course.objects.get(slug=course_slug) except Course.DoesNotExist: raise NotFound("Course not found") return Glossary.objects.filter(course=course)