from rest_framework import generics from rest_framework.exceptions import NotFound from drf_yasg.utils import swagger_auto_schema from drf_yasg import openapi from rest_framework.permissions import IsAuthenticated from apps.account.models import StudentUser from apps.course.models import Participant, Course from apps.course.serializers import ParticipantSerializer from apps.account.serializers import UserProfileSerializer from apps.course.doc import * from utils.exceptions import AppAPIException class CourseParticipantsView(generics.ListAPIView): serializer_class = UserProfileSerializer @swagger_auto_schema( operation_description=doc_course_participants(), ) def get_queryset(self): course_slug = self.kwargs.get('slug') try: course = Course.objects.get(slug=course_slug) except Course.DoesNotExist: raise AppAPIException({'message': "Course not found"}) # Handle course not found return StudentUser.objects.filter(participated_courses__course=course) # class ParticipantCreateView(generics.CreateAPIView): # queryset = StudentUser.objects.all() # serializer_class = ParticipantSerializer # permission_classes = [IsAuthenticated] # def create(self, request, *args, **kwargs): # user = request.user # course_slug = self.kwargs.get('slug') # Get the slug from the URL # try: # course = Course.objects.get(slug=slug) # Retrieve the Course object # except Course.DoesNotExist: # raise AppAPIException({'message': "Course not found"}) # Handle course not found # if request.data.get('email') != request.user: # raise AppAPIException({'message': "The email must be for the requesting user"}) # if user.user_type != User.UserType.STUDENT: # user.change_user_type(User.UserType.STUDENT) # participant, created = Participant.objects.get_or_create( # student=user, # course=course # ) # serializer = self.get_serializer(participant) # return Response(serializer.data, status=status.HTTP_201_CREATED)