views.py 4.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
"""
Video Outlines

We only provide the listing view for a video outline, and video outlines are
only displayed at the course level. This is because it makes it a lot easier to
optimize and reason about, and it avoids having to tackle the bigger problem of
general XBlock representation in this rather specialized formatting.
"""
from functools import partial

from django.http import Http404, HttpResponse

13
from rest_framework import generics
14 15 16 17 18 19
from rest_framework.response import Response
from opaque_keys.edx.locator import BlockUsageLocator

from xmodule.exceptions import NotFoundError
from xmodule.modulestore.django import modulestore

20
from ..utils import mobile_view, mobile_course_access
21 22 23
from .serializers import BlockOutline, video_summary


24
@mobile_view()
25
class VideoSummaryList(generics.ListAPIView):
Mark Hoeber committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    """
    **Use Case**

        Get a list of all videos in the specified course. You can use the
        video_url value to access the video file.

    **Example request**:

        GET /api/mobile/v0.5/video_outlines/courses/{organization}/{course_number}/{course_run}

    **Response Values**

        An array of videos in the course. For each video:

            * section_url: The URL to the first page of the section that
41
              contains the video in the Learning Management System.
Mark Hoeber committed
42

43 44
            * path: An array containing category, name, and id values specifying the
              complete path the the video in the courseware hierarchy. The
Mark Hoeber committed
45 46 47 48
              following categories values are included: "chapter", "sequential",
              and "vertical". The name value is the display name for that object.

            * unit_url: The URL to the unit contains the video in the Learning
49
              Management System.
Mark Hoeber committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

            * named_path: An array consisting of the display names of the
              courseware objects in the path to the video.

            * summary:  An array of data about the video that includes:

                * category:  The type of component, in this case always "video".

                * video_thumbnail_url: The URL to the thumbnail image for the
                  video, if available.

                * language: The language code for the video.

                * name:  The display name of the video.

                * video_url: The URL to the video file. Use this value to access
                  the video.

                * duration: The length of the video, if available.

                * transcripts: An array of language codes and URLs to available
                  video transcripts. Use the URL value to access a transcript
                  for the video.

                * id: The unique identifier for the video.

                * size: The size of the video file
    """
78

79 80
    @mobile_course_access(depth=None)
    def list(self, request, course, *args, **kwargs):
81 82
        video_outline = list(
            BlockOutline(
83
                course.id,
84
                course,
85 86 87 88 89 90 91
                {"video": partial(video_summary, course)},
                request,
            )
        )
        return Response(video_outline)


92
@mobile_view()
93
class VideoTranscripts(generics.RetrieveAPIView):
Mark Hoeber committed
94 95 96 97 98 99 100 101
    """
    **Use Case**

        Use to get a transcript for a specified video and language.

    **Example request**:

        GET /api/mobile/v0.5/video_outlines/transcripts/{organization}/{course_number}/{course_run}/{video ID}/{language code}
102

Mark Hoeber committed
103 104 105
    **Response Values**

        An HttpResponse with an SRT file download.
106 107 108

    """

109 110
    @mobile_course_access()
    def get(self, request, course, *args, **kwargs):
111 112 113 114
        block_id = kwargs['block_id']
        lang = kwargs['lang']

        usage_key = BlockUsageLocator(
115
            course.id, block_type="video", block_id=block_id
116 117 118 119 120
        )
        try:
            video_descriptor = modulestore().get_item(usage_key)
            content, filename, mimetype = video_descriptor.get_transcript(lang=lang)
        except (NotFoundError, ValueError, KeyError):
Dave St.Germain committed
121
            raise Http404(u"Transcript not found for {}, lang: {}".format(block_id, lang))
122 123 124 125 126

        response = HttpResponse(content, content_type=mimetype)
        response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)

        return response