Ejemplo n.º 1
0
    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 = CollectionDetailSerializer(
                        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)
Ejemplo n.º 2
0
    def test_export_collection(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)
        collection = OrganizationCollectionFactory()
        collection.add_references([concept1.uri, concept2.uri, mapping.uri])
        collection.refresh_from_db()

        export_collection(collection.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, {
                **CollectionDetailSerializer(collection).data, 'concepts': ANY,
                'mappings': ANY
            })

        exported_concepts = exported_data['concepts']
        expected_concepts = ConceptVersionDetailSerializer(
            [concept2.get_latest_version(),
             concept1.get_latest_version()],
            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.get_latest_version()], many=True).data

        self.assertEqual(len(exported_mappings), 1)
        self.assertEqual(expected_mappings, exported_mappings)

        s3_upload_key = collection.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)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
 def get_detail_serializer(obj):
     return CollectionDetailSerializer(obj)