|
|
|
@ -4,6 +4,7 @@ from drf_yasg.utils import swagger_auto_schema |
|
|
|
from drf_yasg import openapi |
|
|
|
from django.shortcuts import get_object_or_404 |
|
|
|
from rest_framework import status |
|
|
|
from rest_framework.response import Response |
|
|
|
|
|
|
|
from apps.course.serializers import ( |
|
|
|
LessonSerializer |
|
|
|
@ -32,3 +33,40 @@ class LessonListView(ListAPIView): |
|
|
|
raise AppAPIException({"message": "course not found"}, status_code=status.HTTP_404_NOT_FOUND) |
|
|
|
|
|
|
|
return self.queryset.filter(course=course).order_by('priority','id') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LessonDetailView(RetrieveAPIView): |
|
|
|
serializer_class = LessonSerializer |
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs): |
|
|
|
lesson_id = self.kwargs.get('id') |
|
|
|
lesson = get_object_or_404(Lesson, id=lesson_id, is_active=True) |
|
|
|
|
|
|
|
# Get the next and previous lessons based on priority and id |
|
|
|
next_lesson = Lesson.objects.filter( |
|
|
|
course=lesson.course, |
|
|
|
is_active=True, |
|
|
|
priority__gte=lesson.priority, |
|
|
|
id__gt=lesson.id |
|
|
|
).order_by('priority', 'id').first() |
|
|
|
|
|
|
|
previous_lesson = Lesson.objects.filter( |
|
|
|
course=lesson.course, |
|
|
|
is_active=True, |
|
|
|
priority__lte=lesson.priority, |
|
|
|
id__lt=lesson.id |
|
|
|
).order_by('-priority', '-id').first() |
|
|
|
|
|
|
|
# Serialize the current lesson |
|
|
|
lesson_data = self.get_serializer(lesson).data |
|
|
|
|
|
|
|
# Add next and previous lesson ids |
|
|
|
lesson_data['next_lesson_id'] = next_lesson.id if next_lesson else None |
|
|
|
lesson_data['previous_lesson_id'] = previous_lesson.id if previous_lesson else None |
|
|
|
|
|
|
|
lesson_data['can_go_next'] = next_lesson is not None |
|
|
|
|
|
|
|
return Response(lesson_data) |
|
|
|
|