Browse Source

feat lesson completion

master
alireza 1 year ago
parent
commit
0677323c3d
  1. 3
      apps/course/urls.py
  2. 30
      apps/course/views/lesson.py

3
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('<slug:slug>/', views.CourseDetailAPIView.as_view(), name='course-detail'),
path('<slug:slug>/attachments/', views.AttachmentListAPIView.as_view(), name='course-attachment-list'),
path('<slug:slug>/glossaries/', views.GlossaryListAPIView.as_view(), name='course-glossary-list'),
@ -16,6 +18,7 @@ urlpatterns = [
path('lesson/<int:id>/', views.LessonDetailView.as_view(), name='lesson-detail'),
path('<slug:slug>/participants/', views.CourseParticipantsView.as_view(), name='course-participant-list'),
# path('<slug:slug>/participant/join/', views.ParticipantCreateView.as_view(), name='course-participant-join'),

30
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)
Loading…
Cancel
Save