From 0677323c3d5cbd113f0b6b6233d85647ea158e2b Mon Sep 17 00:00:00 2001 From: alireza Date: Sun, 12 Jan 2025 17:09:03 +0330 Subject: [PATCH] feat lesson completion --- apps/course/urls.py | 3 +++ apps/course/views/lesson.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/apps/course/urls.py b/apps/course/urls.py index e08572d..ff96734 100644 --- a/apps/course/urls.py +++ b/apps/course/urls.py @@ -9,6 +9,8 @@ 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('/', views.CourseDetailAPIView.as_view(), name='course-detail'), path('/attachments/', views.AttachmentListAPIView.as_view(), name='course-attachment-list'), path('/glossaries/', views.GlossaryListAPIView.as_view(), name='course-glossary-list'), @@ -16,6 +18,7 @@ urlpatterns = [ path('lesson//', views.LessonDetailView.as_view(), name='lesson-detail'), path('/participants/', views.CourseParticipantsView.as_view(), name='course-participant-list'), + # path('/participant/join/', views.ParticipantCreateView.as_view(), name='course-participant-join'), diff --git a/apps/course/views/lesson.py b/apps/course/views/lesson.py index 3719c4e..9941f93 100644 --- a/apps/course/views/lesson.py +++ b/apps/course/views/lesson.py @@ -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) \ No newline at end of file