Files
edx-platform/lms/djangoapps/mobile_api/video_outlines/serializers.py
David Ormsbee 4f5d8b30de Basic Mobile API (v0.5) and Video Abstraction Layer integration.
Note that the features in this release are opt-in, and course and video
behavior will remain the same unless a course explicitly opts in.

Major pieces of functionality with this commit:

Allows the listing of a user's enrollments, course videos, and updates. In
order to make a course available for mobile use, course staff must explicitly
set the Course Advanced Setting "Mobile Course Available" to true. Course staff
will always see their own courses through the Mobile API regardless of this
setting, but students will only be allowed to see a course through the Mobile
API if this setting is set to "true". By default, a Course will *not* be
available for mobile use.

This is a Django app for video resource management. It is completely optional,
and is intended to allow video and operations teams to create new encodings of
videos (e.g. low res for mobile) and change CDNs without having to edit course
data directly. Course teams can now use a "EdX Video ID" setting for Videos,
which will leverage VAL. Video units that do not fill in an "EdX Video ID" will
behave exactly as they always have.

* The Mobile API is enabled with the ENABLE_MOBILE_REST_API feature flag.
* VAL is enabled with the ENABLE_VIDEO_ABSTRACTION_LAYER_API feature flag.
* VAL and the Mobile API both require ENABLE_OAUTH2_PROVIDER).
* The Mobile API is a read-only API, but VAL requires database migrations.
* Applications that make use of either the Mobile API or VAL must be registered
  with the OAuth2 provider app in Django Admin.
2014-09-23 12:31:46 -04:00

141 lines
4.8 KiB
Python

from rest_framework.reverse import reverse
from courseware.access import has_access
from edxval.api import (
get_video_info_for_course_and_profile, ValInternalError
)
class BlockOutline(object):
def __init__(self, course_id, start_block, categories_to_outliner, request):
"""Create a BlockOutline using `start_block` as a starting point."""
self.start_block = start_block
self.categories_to_outliner = categories_to_outliner
self.course_id = course_id
self.request = request # needed for making full URLS
self.local_cache = {}
try:
self.local_cache['course_videos'] = get_video_info_for_course_and_profile(
unicode(course_id), "mobile_low"
)
except ValInternalError:
self.local_cache['course_videos'] = {}
def __iter__(self):
child_to_parent = {}
stack = [self.start_block]
# path should be optional
def path(block):
block_path = []
while block in child_to_parent:
block = child_to_parent[block]
if block is not self.start_block:
block_path.append({
'name': block.display_name,
'category': block.category,
})
return reversed(block_path)
def find_urls(block):
block_path = []
while block in child_to_parent:
block = child_to_parent[block]
block_path.append(block)
course, chapter, section, unit = list(reversed(block_path))[:4]
position = 1
unit_name = unit.url_name
for block in section.children:
if block.name == unit_name:
break
position += 1
kwargs = dict(
course_id=course.id.to_deprecated_string(),
chapter=chapter.url_name,
section=section.url_name
)
section_url = reverse(
"courseware_section",
kwargs=kwargs,
request=self.request,
)
kwargs['position'] = position
unit_url = reverse(
"courseware_position",
kwargs=kwargs,
request=self.request,
)
return unit_url, section_url
user = self.request.user
while stack:
curr_block = stack.pop()
if curr_block.category in self.categories_to_outliner:
if not has_access(user, 'load', curr_block, course_key=self.course_id):
continue
summary_fn = self.categories_to_outliner[curr_block.category]
block_path = list(path(block))
unit_url, section_url = find_urls(block)
yield {
"path": block_path,
"named_path": [b["name"] for b in block_path[:-1]],
"unit_url": unit_url,
"section_url": section_url,
"summary": summary_fn(self.course_id, curr_block, self.request, self.local_cache)
}
if curr_block.has_children:
for block in reversed(curr_block.get_children()):
stack.append(block)
child_to_parent[block] = curr_block
def video_summary(course, course_id, video_descriptor, request, local_cache):
# First try to check VAL for the URLs we want.
val_video_info = local_cache['course_videos'].get(video_descriptor.edx_video_id, {})
if val_video_info:
video_url = val_video_info['url']
# Then fall back to VideoDescriptor fields for video URLs
elif video_descriptor.html5_sources:
video_url = video_descriptor.html5_sources[0]
else:
video_url = video_descriptor.source
# If we have the video information from VAL, we also have duration and size.
duration = val_video_info.get('duration', None)
size = val_video_info.get('file_size', 0)
# Transcripts...
transcript_langs = video_descriptor.available_translations(verify_assets=False)
transcripts = {
lang: reverse(
'video-transcripts-detail',
kwargs={
'course_id': unicode(course_id),
'block_id': video_descriptor.scope_ids.usage_id.block_id,
'lang': lang
},
request=request,
)
for lang in transcript_langs
}
return {
"video_url": video_url,
"video_thumbnail_url": None,
"duration": duration,
"size": size,
"name": video_descriptor.display_name,
"transcripts": transcripts,
"language": video_descriptor.transcript_language,
"category": video_descriptor.category,
"id": unicode(video_descriptor.scope_ids.usage_id),
}