def _get_video(edx_video_id): """ Get a Video instance, prefetching encoded video and course information. Raises ValVideoNotFoundError if the video cannot be retrieved. """ try: return Video.objects.prefetch_related( "encoded_videos", "courses").get(edx_video_id=edx_video_id) except Video.DoesNotExist: error_message = u"Video not found for edx_video_id: {0}".format( edx_video_id) raise ValVideoNotFoundError(error_message) except Exception: error_message = u"Could not get edx_video_id: {0}".format(edx_video_id) logger.exception(error_message) raise ValInternalError(error_message)
def _get_video(edx_video_id): """ Get a Video instance, prefetching encoded video and course information. Raises ValVideoNotFoundError if the video cannot be retrieved. """ try: encoded_videos = EncodedVideo.objects.select_related("profile") return Video.objects \ .prefetch_related(Prefetch("encoded_videos", queryset=encoded_videos)) \ .prefetch_related("courses") \ .get(edx_video_id=edx_video_id) except Video.DoesNotExist as no_video_error: error_message = f"Video not found for edx_video_id: {edx_video_id}" raise ValVideoNotFoundError(error_message) from no_video_error except Exception as exc: error_message = f"Could not get edx_video_id: {edx_video_id}" logger.exception(error_message) raise ValInternalError(error_message) from exc
def get_video_info_for_course_and_profiles(course_id, profiles): """ Returns a dict of edx_video_ids with a dict of requested profiles. Args: course_id (str): id of the course profiles (list): list of profile_names Returns: (dict): Returns all the profiles attached to a specific edx_video_id { edx_video_id: { 'duration': length of the video in seconds, 'profiles': { profile_name: { 'url': url of the encoding 'file_size': size of the file in bytes }, } }, } Example: Given two videos with two profiles each in course_id 'test_course': { u'edx_video_id_1': { u'duration: 1111, u'profiles': { u'mobile': { 'url': u'http: //www.example.com/meow', 'file_size': 2222 }, u'desktop': { 'url': u'http: //www.example.com/woof', 'file_size': 4444 } } }, u'edx_video_id_2': { u'duration: 2222, u'profiles': { u'mobile': { 'url': u'http: //www.example.com/roar', 'file_size': 6666 }, u'desktop': { 'url': u'http: //www.example.com/bzzz', 'file_size': 8888 } } } } """ # In case someone passes in a key (VAL doesn't really understand opaque keys) course_id = unicode(course_id) try: encoded_videos = EncodedVideo.objects.filter( profile__profile_name__in=profiles, video__courses__course_id=course_id).select_related() except Exception: error_message = u"Could not get encoded videos for course: {0}".format( course_id) logger.exception(error_message) raise ValInternalError(error_message) # DRF serializers were causing extra queries for some reason... return_dict = {} for enc_vid in encoded_videos: # Add duration to edx_video_id return_dict.setdefault(enc_vid.video.edx_video_id, {}).update({ "duration": enc_vid.video.duration, }) # 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