|
11 | 11 | from rest_framework import status |
12 | 12 | from rest_framework.exceptions import ParseError |
13 | 13 | from rest_framework.mixins import ListModelMixin |
14 | | -from rest_framework.permissions import IsAdminUser |
| 14 | +from rest_framework.permissions import IsAdminUser, IsAuthenticated |
15 | 15 | from rest_framework.response import Response |
| 16 | +from rest_framework.views import APIView |
16 | 17 | from rest_framework.viewsets import GenericViewSet |
17 | 18 | from user_tasks.models import UserTaskStatus |
18 | 19 | from user_tasks.views import StatusViewSet |
| 20 | +from opaque_keys.edx.keys import CourseKey |
19 | 21 |
|
20 | | -from cms.djangoapps.modulestore_migrator.api import start_bulk_migration_to_library, start_migration_to_library |
| 22 | +from cms.djangoapps.modulestore_migrator.api import ( |
| 23 | + start_migration_to_library, |
| 24 | + start_bulk_migration_to_library, |
| 25 | + get_all_migrations_info, |
| 26 | +) |
21 | 27 | from openedx.core.djangoapps.content.course_overviews.models import CourseOverview |
22 | 28 | from openedx.core.djangoapps.content_libraries import api as lib_api |
23 | 29 | from openedx.core.lib.api.authentication import BearerAuthenticationAllowInactiveUser |
| 30 | +from common.djangoapps.student.auth import has_studio_write_access |
24 | 31 |
|
25 | 32 | from ...models import ModulestoreMigration |
26 | 33 | from .serializers import ( |
27 | 34 | BulkModulestoreMigrationSerializer, |
| 35 | + MigrationInfoResponseSerializer, |
28 | 36 | LibraryMigrationCourseSerializer, |
29 | 37 | ModulestoreMigrationSerializer, |
30 | 38 | StatusWithModulestoreMigrationsSerializer, |
@@ -338,6 +346,103 @@ def cancel(self, request, *args, **kwargs): |
338 | 346 | raise NotImplementedError |
339 | 347 |
|
340 | 348 |
|
| 349 | +class MigrationInfoViewSet(APIView): |
| 350 | + """ |
| 351 | + Retrieve migration information for a list of source courses or libraries. |
| 352 | +
|
| 353 | + It returns the target library information associated with each successfully migrated source. |
| 354 | +
|
| 355 | + API Endpoints |
| 356 | + ------------- |
| 357 | + GET /api/modulestore_migrator/v1/migration-info/ |
| 358 | + Retrieve migration details for one or more sources. |
| 359 | +
|
| 360 | + Query parameters: |
| 361 | + source_keys (list[str]): List of course or library keys to check. |
| 362 | + Example: ?source_keys=course-v1:edX+DemoX+2024_T1&source_keys=library-v1:orgX+lib_2 |
| 363 | +
|
| 364 | + Example request: |
| 365 | + GET /api/modulestore_migrator/v1/migration-info/?source_keys=course-v1:edX+DemoX+2024_T1 |
| 366 | +
|
| 367 | + Example response: |
| 368 | + { |
| 369 | + "course-v1:edX+DemoX+2024_T1": [ |
| 370 | + { |
| 371 | + "target_key": "library-v1:orgX+lib_2", |
| 372 | + "target_title": "Demo Library", |
| 373 | + "target_collection_key": "col-v2:1234abcd", |
| 374 | + "target_collection_title": "Default Collection", |
| 375 | + "source_key": "course-v1:edX+DemoX+2024_T1" |
| 376 | + } |
| 377 | + ], |
| 378 | + "library-v1:orgX+lib_2": [ |
| 379 | + { |
| 380 | + "target_key": "library-v1:orgX+lib_2", |
| 381 | + "target_title": "Demo Library", |
| 382 | + "target_collection_key": "col-v2:1234abcd", |
| 383 | + "target_collection_title": "Default Collection", |
| 384 | + "source_key": "course-v1:edX+DemoX+2024_T1" |
| 385 | + }, |
| 386 | + { |
| 387 | + "target_key": "library-v1:orgX+lib_2", |
| 388 | + "target_title": "Demo Library", |
| 389 | + "target_collection_key": "col-v2:1234abcd", |
| 390 | + "target_collection_title": "Default Collection", |
| 391 | + "source_key": "course-v1:edX+DemoX+2024_T1" |
| 392 | + } |
| 393 | + ] |
| 394 | + } |
| 395 | + """ |
| 396 | + |
| 397 | + permission_classes = (IsAuthenticated,) |
| 398 | + authentication_classes = ( |
| 399 | + BearerAuthenticationAllowInactiveUser, |
| 400 | + JwtAuthentication, |
| 401 | + SessionAuthenticationAllowInactiveUser, |
| 402 | + ) |
| 403 | + |
| 404 | + @apidocs.schema( |
| 405 | + parameters=[ |
| 406 | + apidocs.string_parameter( |
| 407 | + "source_keys", |
| 408 | + apidocs.ParameterLocation.QUERY, |
| 409 | + description="List of source keys to consult", |
| 410 | + ), |
| 411 | + ], |
| 412 | + responses={ |
| 413 | + 200: MigrationInfoResponseSerializer, |
| 414 | + 400: "Missing required parameter: source_keys", |
| 415 | + 401: "The requester is not authenticated.", |
| 416 | + }, |
| 417 | + ) |
| 418 | + def get(self, request): |
| 419 | + """ |
| 420 | + Handle the migration info `GET` request |
| 421 | + """ |
| 422 | + source_keys = request.query_params.getlist("source_keys") |
| 423 | + |
| 424 | + if not source_keys: |
| 425 | + return Response( |
| 426 | + {"detail": "Missing required parameter: source_keys"}, |
| 427 | + status=status.HTTP_400_BAD_REQUEST |
| 428 | + ) |
| 429 | + |
| 430 | + # Check permissions for each source_key: |
| 431 | + # Skip the source if the key is invalid or if the user doesn't have permissions |
| 432 | + source_keys_validated = [] |
| 433 | + for source_key in source_keys: |
| 434 | + try: |
| 435 | + key = CourseKey.from_string(source_key) |
| 436 | + if has_studio_write_access(request.user, key): |
| 437 | + source_keys_validated.append(key) |
| 438 | + except InvalidKeyError: |
| 439 | + continue |
| 440 | + |
| 441 | + data = get_all_migrations_info(source_keys_validated) |
| 442 | + serializer = MigrationInfoResponseSerializer(data) |
| 443 | + return Response(serializer.data) |
| 444 | + |
| 445 | + |
341 | 446 | @apidocs.schema_for( |
342 | 447 | "list", |
343 | 448 | "List all course migrations to a library.", |
|
0 commit comments