from django.db.models import Value from django.shortcuts import get_object_or_404 from rest_framework.generics import RetrieveAPIView from rest_framework.permissions import IsAuthenticated from rest_framework.authentication import TokenAuthentication from drf_yasg.utils import swagger_auto_schema from drf_yasg import openapi from apps.course.access import user_has_course_access from apps.quiz.models import Quiz from apps.quiz.serializers.quiz import QuizSerializer from apps.quiz.doc import * from utils.exceptions import AppAPIException class QuizDetailAPIView(RetrieveAPIView): serializer_class = QuizSerializer permission_classes = [IsAuthenticated] authentication_classes = [TokenAuthentication] @swagger_auto_schema( operation_description=doc_quiz_detail(), tags=["Imam-Javad - Quiz"], ) def get(self, request, *args, **kwargs): return super().get(request, *args, **kwargs) def get_object(self): quiz = get_object_or_404( Quiz.objects.filter( id=self.kwargs['quiz_id'], ).annotate( lesson__has_quiz=Value(True) ).select_related('lesson', 'course') ) if quiz.course and not user_has_course_access(self.request.user, quiz.course): raise AppAPIException( {'message': 'You do not have access to this quiz.'}, status_code=403, ) return quiz