from rest_framework.exceptions import NotFound from university.api.base import Endpoint from university.models import Lesson class LessonEndpoint(Endpoint): def convert_args(self, request, course_slug, chapter_slug, lesson_slug, *args, **kwargs): try: lesson = Lesson.objects.select_related('chapter', 'chapter__course').get( slug=lesson_slug, chapter__slug=chapter_slug, chapter__course__slug=course_slug) chapter = lesson.chapter course = chapter.course except Lesson.DoesNotExist: raise NotFound if not (course.visable_when_no_perm or request.user.has_course(course)): raise NotFound kwargs['course'] = course kwargs['chapter'] = chapter kwargs['lesson'] = lesson return (args, kwargs)