Browse Source

fix lession

master
alireza 1 year ago
parent
commit
141483c4a1
  1. 2
      apps/course/urls.py
  2. 38
      apps/course/views/lesson.py

2
apps/course/urls.py

@ -13,6 +13,8 @@ urlpatterns = [
path('<slug:slug>/attachments/', views.AttachmentListAPIView.as_view(), name='course-attachment-list'),
path('<slug:slug>/glossaries/', views.GlossaryListAPIView.as_view(), name='course-glossary-list'),
path('<slug:slug>/lessons/', views.LessonListView.as_view(), name='course-lesson-list'),
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'),

38
apps/course/views/lesson.py

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