|
|
|
@ -105,44 +105,55 @@ class LessonDetailView(RetrieveAPIView): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LessonCompletionCreateAPIView(GenericAPIView): |
|
|
|
class LessonCompletionToggleAPIView(GenericAPIView): |
|
|
|
permission_classes = [IsAuthenticated] |
|
|
|
|
|
|
|
|
|
|
|
@swagger_auto_schema( |
|
|
|
operation_description="Mark a lesson as completed", |
|
|
|
operation_description="Toggle lesson completion status (Check/Uncheck)", |
|
|
|
tags=["Imam-Javad - Course"], |
|
|
|
request_body=openapi.Schema( |
|
|
|
type=openapi.TYPE_OBJECT, |
|
|
|
required=['lesson_id'], |
|
|
|
properties={ |
|
|
|
'lesson_id': openapi.Schema(type=openapi.TYPE_INTEGER, description='ID of the lesson to be marked as completed'), |
|
|
|
'lesson_id': openapi.Schema(type=openapi.TYPE_INTEGER, description='ID of the lesson to toggle'), |
|
|
|
}, |
|
|
|
), |
|
|
|
responses={ |
|
|
|
201: 'Lesson completed successfully.', |
|
|
|
200: 'Lesson already completed.', |
|
|
|
201: 'Lesson marked as COMPLETED.', |
|
|
|
200: 'Lesson marked as INCOMPLETE (Unchecked).', |
|
|
|
400: 'Lesson ID is required.', |
|
|
|
404: 'Lesson not found.', |
|
|
|
} |
|
|
|
) |
|
|
|
def post(self, request): |
|
|
|
student = request.user # Assuming the user is the student |
|
|
|
student = request.user |
|
|
|
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: |
|
|
|
course_lesson = CourseLesson.objects.get(id=lesson_id) |
|
|
|
except CourseLesson.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, course_lesson=course_lesson).exists(): |
|
|
|
return Response({'message': 'Lesson already completed.'}, status=status.HTTP_200_OK) |
|
|
|
|
|
|
|
# Create a new completion record |
|
|
|
completion = LessonCompletion(student=student, course_lesson=course_lesson) |
|
|
|
completion.save() |
|
|
|
|
|
|
|
return Response({'message': 'Lesson completed successfully.'}, status=status.HTTP_201_CREATED) |
|
|
|
# TOGGLE LOGIC |
|
|
|
# Try to find an existing completion record |
|
|
|
completion = LessonCompletion.objects.filter(student=student, course_lesson=course_lesson).first() |
|
|
|
|
|
|
|
if completion: |
|
|
|
# Scenario: The user clicked by mistake or wants to un-check |
|
|
|
# Action: Delete the record |
|
|
|
completion.delete() |
|
|
|
return Response( |
|
|
|
{'message': 'Lesson marked as incomplete.', 'is_completed': False}, |
|
|
|
status=status.HTTP_200_OK |
|
|
|
) |
|
|
|
else: |
|
|
|
# Scenario: The lesson is not finished yet |
|
|
|
# Action: Create the record |
|
|
|
LessonCompletion.objects.create(student=student, course_lesson=course_lesson) |
|
|
|
return Response( |
|
|
|
{'message': 'Lesson completed successfully.', 'is_completed': True}, |
|
|
|
status=status.HTTP_201_CREATED |
|
|
|
) |