from django.utils.translation import gettext as _ from django.db.models import Sum from rest_framework.response import Response from rest_framework.permissions import AllowAny from university.api.bases.course import CourseEndpoint class ChapterListEndpoint(CourseEndpoint): permission_classes = (AllowAny, ) def get(self, request, *args, **kwargs): course = kwargs['course'] datas = [] for chapter in course.chapter_set.all().order_by('sequence'): datas.append({ 'title': chapter.title, 'description': chapter.description, 'slug': chapter.slug, 'periods': chapter.lesson_set.aggregate(Sum('periods'))['periods__sum'] }) return Response({'detail': _('success'), 'datas': datas})