From b23e5f28cfec9d3b930d8135fc5bfc86e801c413 Mon Sep 17 00:00:00 2001 From: nwhco Date: Wed, 15 Jan 2025 12:18:47 +0100 Subject: [PATCH] debug lesson --- apps/course/serializers/lesson.py | 19 ++++++++++++++++++- apps/course/views/lesson.py | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/apps/course/serializers/lesson.py b/apps/course/serializers/lesson.py index 43c6f6b..c90f900 100644 --- a/apps/course/serializers/lesson.py +++ b/apps/course/serializers/lesson.py @@ -9,11 +9,28 @@ from apps.quiz.serializers import QuizListSerializer class LessonSerializer(serializers.ModelSerializer): is_complated = serializers.SerializerMethodField() quizs = serializers.SerializerMethodField() + permission = serializers.SerializerMethodField() class Meta: model = Lesson - fields = ['id', 'title', 'priority', 'is_active', 'duration', 'content_type', 'content_file', 'video_link', 'is_complated', 'quizs'] + fields = ['id', 'title', 'priority', 'is_active', 'permission','duration', 'content_type', 'content_file', 'video_link', 'is_complated', 'quizs'] + def get_permission(self, obj): + if student := self._get_authenticated_user(): + if not self._is_participant(student, obj.course): + return False + return True + return False + + def _get_authenticated_user(self): + """Helper method to retrieve the authenticated user from the context.""" + request = self.context.get('request') + return request.user if request and request.user.is_authenticated else None + + def _is_participant(self, student, course): + """Helper method to check if a student is a participant in the given course.""" + return Participant.objects.filter(student=student, course=course).exists() + def get_is_complated(self, obj): request = self.context.get('request') if not request or not request.user.is_authenticated: diff --git a/apps/course/views/lesson.py b/apps/course/views/lesson.py index c4cde86..6bf42c2 100644 --- a/apps/course/views/lesson.py +++ b/apps/course/views/lesson.py @@ -28,7 +28,7 @@ class LessonListView(ListAPIView): def get_queryset(self): course_slug = self.kwargs.get('slug') - course = get_object_or_404(Course, ) + course = get_object_or_404(Course, slug=course_slug) course = Course.objects.filter(slug=course_slug).first() if not course: raise AppAPIException({"message": "course not found"}, status_code=status.HTTP_404_NOT_FOUND)