Skip to content

Commit 2bf1129

Browse files
rpenidoanfbermudezme
authored andcommitted
feat: add library migration list endpoint [FC-0112] (openedx#37567)
This PR adds the `/api/modulestore_migrator/v1/library/:libraryId/migrations/courses/` endpoint, which returns all course migrations for a target library.
1 parent a703c22 commit 2bf1129

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

cms/djangoapps/modulestore_migrator/rest_api/v1/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,11 @@
1111
ROUTER = SimpleRouter()
1212
ROUTER.register(r'migrations', MigrationViewSet, basename='migrations')
1313
ROUTER.register(r'bulk_migration', BulkMigrationViewSet, basename='bulk-migration')
14+
ROUTER.register(
15+
r'library/(?P<lib_key_str>[^/.]+)/migrations/courses',
16+
LibraryCourseMigrationViewSet,
17+
basename='library-migrations',
18+
)
19+
1420

1521
urlpatterns = ROUTER.urls

cms/djangoapps/modulestore_migrator/rest_api/v1/views.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)