Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions edxval/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,18 +532,28 @@ def create_profile(profile_name):
raise ValCannotCreateError(err.message_dict) from err


def _get_video_qset(prefetch_related=False):
"""
Get a Video queryset, optionally prefetching encoded video and course information.
"""
video_qset = Video.objects
if prefetch_related:
encoded_videos = EncodedVideo.objects.select_related("profile")
course_videos = CourseVideo.objects.select_related("video_image")
video_qset = video_qset \
.prefetch_related(Prefetch("encoded_videos", queryset=encoded_videos)) \
.prefetch_related(Prefetch("courses", queryset=course_videos))
return video_qset


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)
return _get_video_qset(prefetch_related=True).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
Expand Down Expand Up @@ -649,7 +659,7 @@ def _get_videos_for_filter(video_filter, sort_field=None, sort_dir=SortDirection
the given field and direction, with ties broken by edx_video_id to ensure a
total order.
"""
videos = Video.objects.filter(**video_filter)
videos = _get_video_qset(prefetch_related=True).filter(**video_filter)
paginator_context = {}

if sort_field:
Expand Down
Loading