|
3 | 3 |
|
4 | 4 | from rest_framework.serializers import ValidationError
|
5 | 5 |
|
6 |
| -from metax_api.models import CatalogRecord, DataCatalog, Contract |
| 6 | +from metax_api.models import CatalogRecord, DataCatalog, Contract, Common |
7 | 7 | from metax_api.services import CatalogRecordService as CRS, CommonService
|
8 | 8 | from .common_serializer import CommonSerializer
|
9 | 9 | from .contract_serializer import ContractSerializer
|
@@ -90,7 +90,28 @@ def update(self, instance, validated_data):
|
90 | 90 | return super(CatalogRecordSerializer, self).update(instance, validated_data)
|
91 | 91 |
|
92 | 92 | def create(self, validated_data):
|
93 |
| - return super(CatalogRecordSerializer, self).create(validated_data) |
| 93 | + if self._migration_override_requested(): |
| 94 | + |
| 95 | + # any custom stuff before create that my be necessary for migration purposes |
| 96 | + |
| 97 | + if 'preferred_identifier' in validated_data['research_dataset']: |
| 98 | + # store pid, since it will be overwritten during create otherwise |
| 99 | + pid = validated_data['research_dataset']['preferred_identifier'] |
| 100 | + |
| 101 | + res = super().create(validated_data) |
| 102 | + |
| 103 | + if self._migration_override_requested(): |
| 104 | + |
| 105 | + # any custom stuff after create that my be necessary for migration purposes |
| 106 | + |
| 107 | + if 'preferred_identifier' in validated_data['research_dataset']: |
| 108 | + # save original pid provided by the requestor |
| 109 | + res.research_dataset['preferred_identifier'] = pid |
| 110 | + |
| 111 | + # save, while bypassing normal save-related procedures in CatalogRecord model |
| 112 | + super(Common, res).save() |
| 113 | + |
| 114 | + return res |
94 | 115 |
|
95 | 116 | def to_representation(self, instance):
|
96 | 117 | res = super(CatalogRecordSerializer, self).to_representation(instance)
|
@@ -341,6 +362,15 @@ def _get_data_catalog_relation(self, identifier_value):
|
341 | 362 | except DataCatalog.DoesNotExist:
|
342 | 363 | raise ValidationError({ 'data_catalog': ['identifier %s not found' % str(identifier_value)]})
|
343 | 364 |
|
| 365 | + def _migration_override_requested(self): |
| 366 | + """ |
| 367 | + Check presence of query parameter ?migration_override, which enables some specific actions during |
| 368 | + the request, at this point useful only for migration operations. |
| 369 | + """ |
| 370 | + if 'request' in self.context: |
| 371 | + return CommonService.get_boolean_query_param(self.context['request'], 'migration_override') |
| 372 | + return False |
| 373 | + |
344 | 374 | def _set_dataset_schema(self):
|
345 | 375 | data_catalog = None
|
346 | 376 | if self._operation_is_create():
|
|
0 commit comments