Commit 6bba9e86 by christopher lee

MA-169 return all requested profiles

get_video_info_for_course_and_profile only returned one profile.
This has been changed to get_video_info_for_course_and_profiles
where it now takes a list of profiles and then returns the profiles
found.
parent ea9c5408
...@@ -278,12 +278,10 @@ def get_videos_for_ids( ...@@ -278,12 +278,10 @@ def get_videos_for_ids(
return (VideoSerializer(video).data for video in videos) return (VideoSerializer(video).data for video in videos)
def get_video_info_for_course_and_profile(course_id, profile_name): def get_video_info_for_course_and_profiles(course_id, profiles):
""" """
Returns a dict of edx_video_ids with a dict of requested profiles. Returns a dict of edx_video_ids with a dict of requested profiles.
If the profiles or video is not found, urls will be blank.
Args: Args:
course_id (str): id of the course course_id (str): id of the course
profiles (list): list of profile_names profiles (list): list of profile_names
...@@ -292,48 +290,51 @@ def get_video_info_for_course_and_profile(course_id, profile_name): ...@@ -292,48 +290,51 @@ def get_video_info_for_course_and_profile(course_id, profile_name):
edx_video_id edx_video_id
{ {
edx_video_id: { edx_video_id: {
profile_name: { 'duration': length of the video in seconds,
'url': url of the encoding 'profiles': {
'duration': length of the video in seconds profile_name: {
'file_size': size of the file in bytes 'url': url of the encoding
}, 'file_size': size of the file in bytes
},
}
}, },
} }
Example: Example:
Given two videos with two profiles each in course_id 'test_course': Given two videos with two profiles each in course_id 'test_course':
{ {
u'edx_video_id_1': { u'edx_video_id_1': {
u'mobile': { u'duration: 1111,
'url': u'http: //www.example.com/meow', u'profiles': {
'duration': 1111, u'mobile': {
'file_size': 2222 'url': u'http: //www.example.com/meow',
}, 'file_size': 2222
u'desktop': { },
'url': u'http: //www.example.com/woof', u'desktop': {
'duration': 3333, 'url': u'http: //www.example.com/woof',
'file_size': 4444 'file_size': 4444
}
} }
}, },
u'edx_video_id_2': { u'edx_video_id_2': {
u'mobile': { u'duration: 2222,
'url': u'http: //www.example.com/roar', u'profiles': {
'duration': 5555, u'mobile': {
'file_size': 6666 'url': u'http: //www.example.com/roar',
}, 'file_size': 6666
u'desktop': { },
'url': u'http: //www.example.com/bzzz', u'desktop': {
'duration': 7777, 'url': u'http: //www.example.com/bzzz',
'file_size': 8888 'file_size': 8888
}
} }
} }
} }
""" """
#TODO This function needs unit tests. Write them when addressing MA-169
# In case someone passes in a key (VAL doesn't really understand opaque keys) # In case someone passes in a key (VAL doesn't really understand opaque keys)
course_id = unicode(course_id) course_id = unicode(course_id)
try: try:
encoded_videos = EncodedVideo.objects.filter( encoded_videos = EncodedVideo.objects.filter(
profile__profile_name=profile_name, profile__profile_name__in=profiles,
video__courses__course_id=course_id video__courses__course_id=course_id
).select_related() ).select_related()
except Exception: except Exception:
...@@ -342,36 +343,39 @@ def get_video_info_for_course_and_profile(course_id, profile_name): ...@@ -342,36 +343,39 @@ def get_video_info_for_course_and_profile(course_id, profile_name):
raise ValInternalError(error_message) raise ValInternalError(error_message)
# DRF serializers were causing extra queries for some reason... # DRF serializers were causing extra queries for some reason...
return { return_dict = {}
enc_vid.video.edx_video_id: { for enc_vid in encoded_videos:
"url": enc_vid.url, # Add duration to edx_video_id
"file_size": enc_vid.file_size, return_dict.setdefault(enc_vid.video.edx_video_id, {}).update(
"duration": enc_vid.video.duration, {
} "duration": enc_vid.video.duration,
for enc_vid in encoded_videos }
} )
# Add profile information to edx_video_id's profiles
return_dict[enc_vid.video.edx_video_id].setdefault("profiles", {}).update(
{enc_vid.profile.profile_name: {
"url": enc_vid.url,
"file_size": enc_vid.file_size,
}}
)
return return_dict
def copy_course_videos(old_course_id, new_course_id): def copy_course_videos(source_course_id, destination_course_id):
""" """
Adds the new_course_id to the videos taken from the old_course_id Adds the destination_course_id to the videos taken from the source_course_id
Args: Args:
old_course_id: The original course_id source_course_id: The original course_id
new_course_id: The new course_id of the rerun destination_course_id: The new course_id where the videos will be copied
""" """
if old_course_id == new_course_id: if source_course_id == destination_course_id:
raise ValueError( return
"Both course_id's are the same: {}".format(new_course_id)
) videos = Video.objects.filter(courses__course_id=unicode(source_course_id))
if Video.objects.filter(courses__course_id=unicode(new_course_id)):
raise ValCannotCreateError(
"New course id already exists: {}".format(new_course_id)
)
videos = Video.objects.filter(courses__course_id=unicode(old_course_id))
for video in videos: for video in videos:
CourseVideo.objects.create( CourseVideo.objects.get_or_create(
video=video, video=video,
course_id=new_course_id course_id=destination_course_id
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment