You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

82 lines
2.8 KiB

from rest_framework.generics import ListAPIView, RetrieveAPIView
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
)
from apps.course.models import Course, Lesson
from apps.course.doc import *
from utils.exceptions import AppAPIException
class LessonListView(ListAPIView):
serializer_class = LessonSerializer
queryset = Lesson.objects.filter(is_active=True)
@swagger_auto_schema(
operation_description=doc_courses_lesson(),
)
def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)
def get_queryset(self):
course_slug = self.kwargs.get('slug')
course = get_object_or_404(Course, )
course = Course.objects.filter(slug=course_slug).first()
if not course:
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()
total_lessons = Lesson.objects.filter(course=lesson.course, is_active=True).count()
# Calculate the current lesson number in the course
current_lesson_number = Lesson.objects.filter(
course=lesson.course,
is_active=True,
priority__lte=lesson.priority
).count()
# Serialize the current lesson
lesson_data = self.get_serializer(lesson).data
# Add current lesson number and total lessons
lesson_data['current_lesson_number'] = current_lesson_number
lesson_data['total_lessons'] = total_lessons
# 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)