|
|
|
@ -1,4 +1,4 @@ |
|
|
|
from rest_framework.generics import ListAPIView, RetrieveAPIView |
|
|
|
from rest_framework.generics import ListAPIView, RetrieveAPIView, GenericAPIView |
|
|
|
|
|
|
|
from drf_yasg.utils import swagger_auto_schema |
|
|
|
from drf_yasg import openapi |
|
|
|
@ -9,9 +9,10 @@ from rest_framework.response import Response |
|
|
|
from apps.course.serializers import ( |
|
|
|
LessonSerializer |
|
|
|
) |
|
|
|
from apps.course.models import Course, Lesson |
|
|
|
from apps.course.models import Course, Lesson, LessonCompletion |
|
|
|
from apps.course.doc import * |
|
|
|
from utils.exceptions import AppAPIException |
|
|
|
from rest_framework.permissions import IsAuthenticated |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -80,3 +81,28 @@ class LessonDetailView(RetrieveAPIView): |
|
|
|
|
|
|
|
return Response(lesson_data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LessonCompletionCreateAPIView(GenericAPIView): |
|
|
|
permission_classes = [IsAuthenticated] |
|
|
|
|
|
|
|
def post(self, request): |
|
|
|
student = request.user # Assuming the user is the student |
|
|
|
lesson_id = request.data.get('lesson_id') |
|
|
|
|
|
|
|
if not lesson_id: |
|
|
|
return Response({'error': 'Lesson ID is required.'}, status=status.HTTP_400_BAD_REQUEST) |
|
|
|
try: |
|
|
|
lesson = Lesson.objects.get(id=lesson_id) |
|
|
|
except Lesson.DoesNotExist: |
|
|
|
return Response({'error': 'Lesson not found.'}, status=status.HTTP_404_NOT_FOUND) |
|
|
|
|
|
|
|
# Check if the lesson is already completed by the student |
|
|
|
if LessonCompletion.objects.filter(student=student, lesson=lesson).exists(): |
|
|
|
return Response({'message': 'Lesson already completed.'}, status=status.HTTP_200_OK) |
|
|
|
|
|
|
|
# Create a new completion record |
|
|
|
completion = LessonCompletion(student=student, lesson=lesson) |
|
|
|
completion.save() |
|
|
|
|
|
|
|
return Response({'message': 'Lesson completed successfully.'}, status=status.HTTP_201_CREATED) |