diff --git a/apps/article/admin.py b/apps/article/admin.py index fbe60a7..5450b81 100755 --- a/apps/article/admin.py +++ b/apps/article/admin.py @@ -183,10 +183,6 @@ class ArticleCategoryAdmin(ModelAdmin): if form.base_fields.get('slug'): form.base_fields['slug'].required = False return form - - @display(description=_("Category"), header=True) - def display_header(self, obj): - return list(obj.title) class ArticleAdmin(ModelAdmin): diff --git a/apps/course/urls.py b/apps/course/urls.py index 6c5fa54..8028538 100644 --- a/apps/course/urls.py +++ b/apps/course/urls.py @@ -9,7 +9,7 @@ urlpatterns = [ path('categories/', views.CourseCategoryAPIView.as_view(), name='course-categories'), path('', views.CourseListAPIView.as_view(), name='course-list'), path('my-courses/', views.MyCourseListAPIView.as_view(), name='course-my-courses-list'), - path('lesson/completion/', views.LessonCompletionCreateAPIView.as_view(), name='lesson-completion'), + path('lesson/completion/', views.LessonCompletionToggleAPIView.as_view(), name='lesson-completion'), path('professors/', views.ProfessorListAPIView.as_view(), name='course-professor-list'), re_path(r'professors/(?P[\w-]+)/courses/$', views.ProfessorCourseListAPIView.as_view(), name='course-professor-course-list'), re_path(r'professors/(?P[\w-]+)/$', views.ProfessorDetailAPIView.as_view(), name='course-professor-detail'), diff --git a/apps/course/views/lesson.py b/apps/course/views/lesson.py index 111cbe5..81b6396 100644 --- a/apps/course/views/lesson.py +++ b/apps/course/views/lesson.py @@ -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) \ No newline at end of file + # 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 + ) \ No newline at end of file