|
6 | 6 | import edx_api_doc_tools as apidocs |
7 | 7 | from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication |
8 | 8 | from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser |
| 9 | +from opaque_keys import InvalidKeyError |
| 10 | +from opaque_keys.edx.locator import LibraryLocatorV2 |
| 11 | +from rest_framework import status |
| 12 | +from rest_framework.exceptions import ParseError |
| 13 | +from rest_framework.mixins import ListModelMixin |
9 | 14 | from rest_framework.permissions import IsAdminUser |
10 | 15 | from rest_framework.response import Response |
11 | | -from rest_framework import status |
| 16 | +from rest_framework.viewsets import GenericViewSet |
12 | 17 | from user_tasks.models import UserTaskStatus |
13 | 18 | from user_tasks.views import StatusViewSet |
14 | 19 |
|
15 | | -from cms.djangoapps.modulestore_migrator.api import start_migration_to_library, start_bulk_migration_to_library |
| 20 | +from cms.djangoapps.modulestore_migrator.api import start_bulk_migration_to_library, start_migration_to_library |
| 21 | +from openedx.core.djangoapps.content.course_overviews.models import CourseOverview |
| 22 | +from openedx.core.djangoapps.content_libraries import api as lib_api |
16 | 23 | from openedx.core.lib.api.authentication import BearerAuthenticationAllowInactiveUser |
17 | 24 |
|
| 25 | +from ...models import ModulestoreMigration |
18 | 26 | from .serializers import ( |
19 | | - StatusWithModulestoreMigrationSerializer, |
20 | | - ModulestoreMigrationSerializer, |
21 | 27 | BulkModulestoreMigrationSerializer, |
| 28 | + LibraryMigrationCourseSerializer, |
| 29 | + ModulestoreMigrationSerializer, |
| 30 | + StatusWithModulestoreMigrationSerializer |
22 | 31 | ) |
23 | 32 |
|
24 | | - |
25 | 33 | log = logging.getLogger(__name__) |
26 | 34 |
|
27 | 35 |
|
@@ -281,3 +289,52 @@ def create(self, request, *args, **kwargs): |
281 | 289 | serializer = self.get_serializer(task_status) |
282 | 290 |
|
283 | 291 | return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 292 | + |
| 293 | + |
| 294 | +@apidocs.schema_for( |
| 295 | + "list", |
| 296 | + "List all course migrations to a library.", |
| 297 | + responses={ |
| 298 | + 201: LibraryMigrationCourseSerializer, |
| 299 | + 401: "The requester is not authenticated.", |
| 300 | + 403: "The requester does not have permission to access the library.", |
| 301 | + }, |
| 302 | +) |
| 303 | +class LibraryCourseMigrationViewSet(GenericViewSet, ListModelMixin): |
| 304 | + """ |
| 305 | + Show infomation about migrations related to a destination library. |
| 306 | + """ |
| 307 | + |
| 308 | + serializer_class = LibraryMigrationCourseSerializer |
| 309 | + pagination_class = None |
| 310 | + queryset = ModulestoreMigration.objects.all().select_related('target_collection', 'target', 'task_status') |
| 311 | + |
| 312 | + def get_serializer_context(self): |
| 313 | + """ |
| 314 | + Add course name list to the serializer context. |
| 315 | + """ |
| 316 | + context = super().get_serializer_context() |
| 317 | + queryset = self.get_queryset() |
| 318 | + course_keys = queryset.values_list('source__key', flat=True) |
| 319 | + courses = CourseOverview.get_all_courses(course_keys=course_keys) |
| 320 | + context['course_names'] = dict((str(course.id), course.display_name) for course in courses) |
| 321 | + return context |
| 322 | + |
| 323 | + def get_queryset(self): |
| 324 | + """ |
| 325 | + Override the default queryset to filter by the library key. |
| 326 | + """ |
| 327 | + queryset = super().get_queryset() |
| 328 | + lib_key_str = self.kwargs['lib_key_str'] |
| 329 | + try: |
| 330 | + library_key = LibraryLocatorV2.from_string(lib_key_str) |
| 331 | + except InvalidKeyError as exc: |
| 332 | + raise ParseError(detail=f"Malformed library key: {lib_key_str}") from exc |
| 333 | + lib_api.require_permission_for_library_key( |
| 334 | + library_key, |
| 335 | + self.request.user, |
| 336 | + lib_api.permissions.CAN_VIEW_THIS_CONTENT_LIBRARY |
| 337 | + ) |
| 338 | + queryset = queryset.filter(target__key=library_key, source__key__startswith='course-v1') |
| 339 | + |
| 340 | + return queryset |
0 commit comments