def create(self, request, *args, **kwargs): head_object = self.get_queryset().first() version = request.data.pop('id', None) payload = { "mnemonic": head_object.mnemonic, "id": head_object.mnemonic, "name": head_object.name, **request.data, "organization_id": head_object.organization_id, "user_id": head_object.user_id, 'version': version } serializer = self.get_serializer(data=payload) if serializer.is_valid(): try: instance = serializer.create_version(payload) if serializer.is_valid(): serializer = SourceDetailSerializer( instance, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED) except IntegrityError as ex: return Response(dict( error=str(ex), detail=VERSION_ALREADY_EXISTS.format(version)), status=status.HTTP_409_CONFLICT) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_get_200_zip(self): response = self.client.get(self.organization.sources_url, HTTP_COMPRESS='true', format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/zip') content = json.loads( zipfile.ZipFile(response.rendered_content.filelike).read( 'export.json').decode('utf-8')) self.assertEqual(content, []) source = OrganizationSourceFactory(organization=self.organization) response = self.client.get( self.organization.sources_url + '?verbose=true', HTTP_COMPRESS='true', ) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/zip') content = json.loads( zipfile.ZipFile(response.rendered_content.filelike).read( 'export.json').decode('utf-8')) self.assertEqual(content, SourceDetailSerializer([source], many=True).data)
class MappingDetailSerializer(MappingListSerializer): type = CharField(source='resource_type', read_only=True) uuid = CharField(source='id', read_only=True) extras = JSONField(required=False, allow_null=True) created_by = CharField(source='created_by.username', read_only=True) updated_by = CharField(source='created_by.username', read_only=True) parent_id = IntegerField(required=True, write_only=True) map_type = CharField(required=True) to_concept_url = CharField(required=False) from_concept_url = CharField(required=False) from_concept = ConceptDetailSerializer() to_concept = ConceptDetailSerializer() from_source = SourceDetailSerializer() to_source = SourceDetailSerializer() created_on = DateTimeField(source='created_at', read_only=True) updated_on = DateTimeField(source='updated_at', read_only=True) class Meta: model = Mapping fields = MappingListSerializer.Meta.fields + ( 'type', 'uuid', 'extras', 'created_on', 'updated_on', 'created_by', 'updated_by', 'parent_id', 'internal_reference_id', ) def create(self, validated_data): mapping = Mapping.persist_new(data=validated_data, user=self.context.get('request').user) if mapping.errors: self._errors.update(mapping.errors) return mapping def update(self, instance, validated_data): errors = Mapping.create_new_version_for( instance, validated_data, self.context.get('request').user) if errors: self._errors.update(errors) return instance
def test_export_source(self, s3_mock): # pylint: disable=too-many-locals s3_mock.url_for = Mock(return_value='https://s3-url') s3_mock.upload_file = Mock() source = OrganizationSourceFactory() concept1 = ConceptFactory(parent=source) concept2 = ConceptFactory(parent=source) mapping = MappingFactory(from_concept=concept2, to_concept=concept1, parent=source) source_v1 = OrganizationSourceFactory(mnemonic=source.mnemonic, organization=source.organization, version='v1') concept1.sources.add(source_v1) concept2.sources.add(source_v1) mapping.sources.add(source_v1) export_source(source_v1.id) # pylint: disable=no-value-for-parameter latest_temp_dir = get_latest_dir_in_path('/tmp/') zipped_file = zipfile.ZipFile(latest_temp_dir + '/export.zip') exported_data = json.loads( zipped_file.read('export.json').decode('utf-8')) self.assertEqual( exported_data, { **SourceDetailSerializer(source_v1).data, 'concepts': ANY, 'mappings': ANY }) exported_concepts = exported_data['concepts'] expected_concepts = ConceptVersionDetailSerializer( [concept2, concept1], many=True).data self.assertEqual(len(exported_concepts), 2) self.assertIn(expected_concepts[0], exported_concepts) self.assertIn(expected_concepts[1], exported_concepts) exported_mappings = exported_data['mappings'] expected_mappings = MappingDetailSerializer([mapping], many=True).data self.assertEqual(len(exported_mappings), 1) self.assertEqual(expected_mappings, exported_mappings) s3_upload_key = source_v1.export_path s3_mock.upload_file.assert_called_once_with(key=s3_upload_key, file_path=latest_temp_dir + '/export.zip', binary=True) s3_mock.url_for.assert_called_once_with(s3_upload_key) import shutil shutil.rmtree(latest_temp_dir)
def build_resource_data(obj): resource = obj.resource if not resource: return None resource_type = resource.resource_type.lower() if resource_type == 'source': from core.sources.serializers import SourceDetailSerializer return SourceDetailSerializer(resource).data if resource_type == 'collection': from core.collections.serializers import CollectionDetailSerializer return CollectionDetailSerializer(resource).data if resource_type == 'organization': from core.orgs.serializers import OrganizationDetailSerializer return OrganizationDetailSerializer(resource).data return None
def update(self, request, *args, **kwargs): self.object = self.get_object() head = self.object.head if not head: return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED) # pragma: no cover external_id = get(request.data, 'version_external_id') if external_id: request.data['external_id'] = external_id # pragma: no cover serializer = self.get_serializer(self.object, data=request.data, partial=True) if serializer.is_valid(): self.object = serializer.save(force_update=True) if serializer.is_valid(): serializer = SourceDetailSerializer(self.object, context={'request': request}) return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_detail_serializer(obj): return SourceDetailSerializer(obj)