|
12 | 12 | from cms.djangoapps.modulestore_migrator.models import ModulestoreMigration |
13 | 13 |
|
14 | 14 |
|
15 | | -class ModulestoreMigrationSerializer(serializers.ModelSerializer): |
| 15 | +class ModulestoreMigrationSerializer(serializers.Serializer): |
16 | 16 | """ |
17 | | - Serializer for the course to library import creation API. |
| 17 | + Serializer for the course or legacylibrary to library V2 import creation API. |
18 | 18 | """ |
19 | 19 |
|
20 | 20 | source = serializers.CharField( # type: ignore[assignment] |
21 | 21 | help_text="The source course or legacy library key to import from.", |
22 | 22 | required=True, |
23 | 23 | ) |
24 | 24 | target = serializers.CharField( |
25 | | - help_text="The target library key to import into.", |
| 25 | + help_text="The target library V2 key to import into.", |
26 | 26 | required=True, |
27 | 27 | ) |
28 | 28 | composition_level = serializers.ChoiceField( |
@@ -54,18 +54,6 @@ class ModulestoreMigrationSerializer(serializers.ModelSerializer): |
54 | 54 | default=False, |
55 | 55 | ) |
56 | 56 |
|
57 | | - class Meta: |
58 | | - model = ModulestoreMigration |
59 | | - fields = [ |
60 | | - 'source', |
61 | | - 'target', |
62 | | - 'target_collection_slug', |
63 | | - 'composition_level', |
64 | | - 'repeat_handling_strategy', |
65 | | - 'preserve_url_slugs', |
66 | | - 'forward_source_to_target', |
67 | | - ] |
68 | | - |
69 | 57 | def get_fields(self): |
70 | 58 | fields = super().get_fields() |
71 | 59 | request = self.context.get('request') |
@@ -100,19 +88,74 @@ def get_forward_source_to_target(self, obj: ModulestoreMigration): |
100 | 88 |
|
101 | 89 | def to_representation(self, instance): |
102 | 90 | """ |
103 | | - Override to customize the serialized representation.""" |
| 91 | + Override to customize the serialized representation. |
| 92 | + """ |
104 | 93 | data = super().to_representation(instance) |
105 | 94 | # Custom logic for forward_source_to_target during serialization |
106 | 95 | data['forward_source_to_target'] = self.get_forward_source_to_target(instance) |
107 | 96 | return data |
108 | 97 |
|
109 | 98 |
|
| 99 | +class BulkModulestoreMigrationSerializer(ModulestoreMigrationSerializer): |
| 100 | + """ |
| 101 | + Serializer for a bulk migration (of several courses or legacy libraries) to a V2 library. |
| 102 | + """ |
| 103 | + sources = serializers.ListField( |
| 104 | + child=serializers.CharField(), |
| 105 | + help_text="The list of sources course or legacy library keys to import from.", |
| 106 | + required=True, |
| 107 | + ) |
| 108 | + |
| 109 | + target_collection_slug_list = serializers.ListField( |
| 110 | + child=serializers.CharField(), |
| 111 | + help_text="The list of target collection slugs within the library to import into. Optional.", |
| 112 | + required=False, |
| 113 | + allow_empty=True, |
| 114 | + default=None, |
| 115 | + ) |
| 116 | + |
| 117 | + create_collections = serializers.BooleanField( |
| 118 | + help_text=( |
| 119 | + "If true and `target_collection_slug_list` is not set, " |
| 120 | + "create the collections in the library where the import will be made" |
| 121 | + ), |
| 122 | + required=False, |
| 123 | + default=False, |
| 124 | + ) |
| 125 | + |
| 126 | + def get_fields(self): |
| 127 | + fields = super().get_fields() |
| 128 | + fields.pop("source", None) |
| 129 | + fields.pop("target_collection_slug", None) |
| 130 | + return fields |
| 131 | + |
| 132 | + def validate_sources(self, value): |
| 133 | + """ |
| 134 | + Validate all the source key format |
| 135 | + """ |
| 136 | + validated_sources = [] |
| 137 | + for v in value: |
| 138 | + try: |
| 139 | + validated_sources.append(LearningContextKey.from_string(v)) |
| 140 | + except InvalidKeyError as exc: |
| 141 | + raise serializers.ValidationError(f"Invalid source key: {str(exc)}") from exc |
| 142 | + return validated_sources |
| 143 | + |
| 144 | + def to_representation(self, instance): |
| 145 | + """ |
| 146 | + Override to customize the serialized representation. |
| 147 | + """ |
| 148 | + if isinstance(instance, list): |
| 149 | + return [super().to_representation(obj) for obj in instance] |
| 150 | + return super().to_representation(instance) |
| 151 | + |
| 152 | + |
110 | 153 | class StatusWithModulestoreMigrationSerializer(StatusSerializer): |
111 | 154 | """ |
112 | 155 | Serializer for the import task status. |
113 | 156 | """ |
114 | 157 |
|
115 | | - parameters = ModulestoreMigrationSerializer(source='modulestoremigration') |
| 158 | + parameters = ModulestoreMigrationSerializer(source='migrations', many=True) |
116 | 159 |
|
117 | 160 | class Meta: |
118 | 161 | model = StatusSerializer.Meta.model |
|
0 commit comments