コード例 #1
0
def _get_children(course_id, children, category, now):
    result = []

    items = [get_item(course_id, category, item_id)
             for item_id in _get_item_id_list(children, category)]
    items = filter(lambda x: x and x.start < now, items)

    for item in items:
        data = {
            "display_name": item.display_name,
            "location": str(item.location),
            "start": item.start,
            "children": item.children,
        }
        result.append(data)

    return result
コード例 #2
0
    def get(self, request, course_name, format=None):
        """ Get course navigational elements """
        course = get_course(course_name)
        now = datetime.now(UTC())

        # try:
        #     courses = CourseEnrollment.objects.get(
        #         user=request.user, course_id=course_name, is_active=True)
        # except:
        #     return Response(status=status.HTTP_404_NOT_FOUND)

        is_enrolled = CourseEnrollment.is_enrolled(request.user, course_name)
        if not is_enrolled:
            return Response(status=status.HTTP_404_NOT_FOUND)

        result = {
            "success": True,
            "course": {
                "display_name": course.display_name,
                "location": str(course.location),
                "start": course.start,
                "children": _get_children(course_name, course.children, 'chapter', now),
            }
        }

        chapters = result['course']['children']
        for chapter in chapters:
            sequentials = chapter['children'] = _get_children(
                course_name, chapter['children'], 'sequential', now)
            for sequential in sequentials:
                verticals = sequential['children'] = _get_children(
                    course_name, sequential['children'], 'vertical', now)
                for vertical in verticals:
                    children = [get_item(course_name, 'video', item_id) for item_id in _get_item_id_list(
                        vertical['children'], 'video')]
                    children = filter(lambda x: x and x.start < now, children)
                    vertical['children'] = []
                    course_key = SlashSeparatedCourseKey.from_string(course_name)
                    for video in children:
                        data = {
                            'display_name': video.display_name,
                            'location': str(video.location),
                            'start': video.start,
                            'track_zh': get_track_zh(video, course_key),
                            'track_en': get_track_en(video, course_key),
                            'source': get_video_source(video),
                        }
                        try:
                            ccsource = video.ccsource.strip()
                            data['length'] = VideoInfo.objects.get(
                                vid=ccsource).duration
                        except:
                            data['length'] = 0

                        vertical['children'].append(data)

        return Response(result)