@@ -371,3 +371,55 @@ def cancel(self, request, *args, **kwargs):
371371 We disable this endpoint to avoid confusion.
372372 """
373373 raise NotImplementedError
374+
375+
376+ @apidocs .schema_for (
377+ "list" ,
378+ "List all course migrations to a library." ,
379+ responses = {
380+ 201 : LibraryMigrationCourseSerializer ,
381+ 401 : "The requester is not authenticated." ,
382+ 403 : "The requester does not have permission to access the library." ,
383+ },
384+ )
385+ class LibraryCourseMigrationViewSet (GenericViewSet , ListModelMixin ):
386+ """
387+ Show infomation about migrations related to a destination library.
388+ """
389+
390+ serializer_class = LibraryMigrationCourseSerializer
391+ pagination_class = None
392+ queryset = ModulestoreMigration .objects .all ().select_related ('target_collection' , 'target' , 'task_status' )
393+
394+ def get_serializer_context (self ):
395+ """
396+ Add course name list to the serializer context.
397+
398+ We need to display the course names in the migration view, and we get all of
399+ them here to avoid futher queries.
400+ """
401+ context = super ().get_serializer_context ()
402+ queryset = self .get_queryset ()
403+ course_keys = queryset .values_list ('source__key' , flat = True )
404+ courses = CourseOverview .get_all_courses (course_keys = course_keys )
405+ context ['course_names' ] = dict ((str (course .id ), course .display_name ) for course in courses )
406+ return context
407+
408+ def get_queryset (self ):
409+ """
410+ Override the default queryset to filter by the library key and check permissions.
411+ """
412+ queryset = super ().get_queryset ()
413+ lib_key_str = self .kwargs ['lib_key_str' ]
414+ try :
415+ library_key = LibraryLocatorV2 .from_string (lib_key_str )
416+ except InvalidKeyError as exc :
417+ raise ParseError (detail = f"Malformed library key: { lib_key_str } " ) from exc
418+ lib_api .require_permission_for_library_key (
419+ library_key ,
420+ self .request .user ,
421+ lib_api .permissions .CAN_VIEW_THIS_CONTENT_LIBRARY
422+ )
423+ queryset = queryset .filter (target__key = library_key , source__key__startswith = 'course-v1' )
424+
425+ return queryset
0 commit comments