Exemplo n.º 1
0
    def _create(self, request, data_opener):

        try:
            checksum = get_hash(data_opener)
            key = checksum
        except Exception as e:
            raise ValidationException(e.args, '(not computed)',
                                      status.HTTP_400_BAD_REQUEST)

        serializer = self.get_serializer(
            data={
                'key': key,
                'data_opener': data_opener,
                'description': request.data.get('description'),
                'name': request.data.get('name'),
                'checksum': checksum
            })

        try:
            serializer.is_valid(raise_exception=True)
        except Exception as e:
            raise ValidationException(e.args, '(not computed)',
                                      status.HTTP_400_BAD_REQUEST)
        else:
            # create on ledger + db
            return self.commit(serializer, request)
Exemplo n.º 2
0
    def commit(self, serializer, request):
        # create on local db
        try:
            instance = self.perform_create(serializer)
        except IntegrityError as e:
            try:
                pkhash = re.search(r'\(pkhash\)=\((\w+)\)', e.args[0]).group(1)
            except IndexError:
                pkhash = ''
            err_msg = 'A objective with this description file already exists.'
            return {'message': err_msg, 'pkhash': pkhash}, status.HTTP_409_CONFLICT
        except Exception as e:
            raise Exception(e.args)

        # init ledger serializer
        ledger_data = {
            'test_data_sample_keys': request.data.getlist('test_data_sample_keys', []),
            'test_data_manager_key': request.data.get('test_data_manager_key', ''),
            'name': request.data.get('name'),
            # XXX workaround because input is a QueryDict and not a JSON object. This
            #     is due to the fact that we are sending file object and body in a
            #     single HTTP request
            'permissions': {
                'public': request.data.get('permissions_public'),
                'authorized_ids': request.data.getlist('permissions_authorized_ids', []),
            },
            'metrics_name': request.data.get('metrics_name'),
        }
        ledger_data.update({'instance': instance})
        ledger_serializer = LedgerObjectiveSerializer(data=ledger_data,
                                                      context={'request': request})

        if not ledger_serializer.is_valid():
            # delete instance
            instance.delete()
            raise ValidationError(ledger_serializer.errors)

        # create on ledger
        try:
            data = ledger_serializer.create(ledger_serializer.validated_data)
        except LedgerTimeout as e:
            data = {'pkhash': [x['pkhash'] for x in serializer.data], 'validated': False}
            raise LedgerException(data, e.status)
        except LedgerConflict as e:
            raise ValidationException(e.msg, e.pkhash, e.status)
        except LedgerError as e:
            instance.delete()
            raise LedgerException(str(e.msg), e.status)
        except Exception:
            instance.delete()
            raise

        d = dict(serializer.data)
        d.update(data)

        return d
Exemplo n.º 3
0
    def commit(self, serializer, request):
        # create on local db
        try:
            instance = self.perform_create(serializer)
        except Exception as e:
            raise Exception(e.args)

        # init ledger serializer
        ledger_data = {
            'test_data_sample_keys':
            request.data.get('test_data_sample_keys') or [],
            'test_data_manager_key':
            request.data.get('test_data_manager_key'),
            'name':
            request.data.get('name'),
            'permissions':
            request.data.get('permissions'),
            'metrics_name':
            request.data.get('metrics_name'),
            'metadata':
            request.data.get('metadata')
        }
        ledger_data.update({'instance': instance})
        ledger_serializer = LedgerObjectiveSerializer(
            data=ledger_data, context={'request': request})

        if not ledger_serializer.is_valid():
            # delete instance
            instance.delete()
            raise ValidationError(ledger_serializer.errors)

        # create on ledger
        try:
            data = ledger_serializer.create(get_channel_name(request),
                                            ledger_serializer.validated_data)
        except LedgerTimeout as e:
            raise LedgerException('timeout', e.status)
        except LedgerConflict as e:
            raise ValidationException(e.msg, e.key, e.status)
        except LedgerError as e:
            instance.delete()
            raise LedgerException(str(e.msg), e.status)
        except Exception:
            instance.delete()
            raise

        d = dict(serializer.data)
        d.update(data)

        return d
Exemplo n.º 4
0
    def _create(self, request):
        metrics = request.data.get('metrics')
        description = request.data.get('description')

        try:
            checksum = get_hash(description)
        except Exception as e:
            raise ValidationException(e.args, '(not computed)',
                                      status.HTTP_400_BAD_REQUEST)

        serializer = self.get_serializer(data={
            'metrics': metrics,
            'description': description,
            'checksum': checksum
        })

        try:
            serializer.is_valid(raise_exception=True)
        except Exception as e:
            raise ValidationException(e.args, '(not computed)',
                                      status.HTTP_400_BAD_REQUEST)
        else:
            # create on ledger + db
            return self.commit(serializer, request)
Exemplo n.º 5
0
    def commit(self, serializer, request):
        # create on db
        instance = self.perform_create(serializer)

        ledger_data = {
            'name': request.data.get('name'),
            # XXX workaround because input is a QueryDict and not a JSON object. This
            #     is due to the fact that we are sending file object and body in a
            #     single HTTP request
            'permissions': {
                'public':
                request.data.get('permissions_public'),
                'authorized_ids':
                request.data.getlist('permissions_authorized_ids', []),
            },
        }

        # init ledger serializer
        ledger_data.update({'instance': instance})
        ledger_serializer = LedgerCompositeAlgoSerializer(
            data=ledger_data, context={'request': request})
        if not ledger_serializer.is_valid():
            # delete instance
            instance.delete()
            raise ValidationError(ledger_serializer.errors)

        # create on ledger
        try:
            data = ledger_serializer.create(ledger_serializer.validated_data)
        except LedgerTimeout as e:
            data = {
                'pkhash': [x['pkhash'] for x in serializer.data],
                'validated': False
            }
            raise LedgerException(data, e.status)
        except LedgerConflict as e:
            raise ValidationException(e.msg, e.pkhash, e.status)
        except LedgerError as e:
            instance.delete()
            raise LedgerException(str(e.msg), e.status)
        except Exception:
            instance.delete()
            raise

        d = dict(serializer.data)
        d.update(data)

        return d
Exemplo n.º 6
0
    def _create(self, request, file):

        pkhash = get_hash(file)
        serializer = self.get_serializer(data={
            'pkhash': pkhash,
            'file': file,
            'description': request.data.get('description')
        })

        try:
            serializer.is_valid(raise_exception=True)
        except Exception as e:
            st = status.HTTP_400_BAD_REQUEST
            if find_primary_key_error(e):
                st = status.HTTP_409_CONFLICT
            raise ValidationException(e.args, pkhash, st)
        else:
            # create on ledger + db
            return self.commit(serializer, request)
Exemplo n.º 7
0
    def commit(self, serializer, request):
        # create on db
        instance = self.perform_create(serializer)

        ledger_data = {
            'name': request.data.get('name'),
            'permissions': request.data.get('permissions'),
            'metadata': request.data.get('metadata')
        }

        # init ledger serializer
        ledger_data.update({'instance': instance})
        ledger_serializer = LedgerAlgoSerializer(data=ledger_data,
                                                 context={'request': request})
        if not ledger_serializer.is_valid():
            # delete instance
            instance.delete()
            raise ValidationError(ledger_serializer.errors)

        # create on ledger
        try:
            data = ledger_serializer.create(get_channel_name(request),
                                            ledger_serializer.validated_data)
        except LedgerTimeout as e:
            if isinstance(serializer.data, list):
                key = [x['key'] for x in serializer.data]
            else:
                key = [serializer.data['key']]
            data = {'key': key, 'validated': False}
            raise LedgerException(data, e.status)
        except LedgerConflict as e:
            raise ValidationException(e.msg, e.key, e.status)
        except LedgerError as e:
            instance.delete()
            raise LedgerException(str(e.msg), e.status)
        except Exception:
            instance.delete()
            raise

        d = dict(serializer.data)
        d.update(data)

        return d
Exemplo n.º 8
0
    def commit(serializer, ledger_data):
        instances = serializer.save()
        # init ledger serializer
        ledger_data.update({'instances': instances})
        ledger_serializer = LedgerDataSampleSerializer(data=ledger_data)

        if not ledger_serializer.is_valid():
            # delete instance
            for instance in instances:
                instance.delete()
            raise ValidationError(ledger_serializer.errors)

        # create on ledger
        try:
            data = ledger_serializer.create(ledger_serializer.validated_data)
        except LedgerTimeout as e:
            data = {
                'pkhash': [x['pkhash'] for x in serializer.data],
                'validated': False
            }
            raise LedgerException(data, e.status)
        except LedgerConflict as e:
            raise ValidationException(e.msg, e.pkhash, e.status)
        except LedgerError as e:
            for instance in instances:
                instance.delete()
            raise LedgerException(str(e.msg), e.status)
        except Exception:
            for instance in instances:
                instance.delete()
            raise

        st = get_success_create_code()

        # update validated to True in response
        if 'pkhash' in data and data['validated']:
            for d in serializer.data:
                if d['pkhash'] in data['pkhash']:
                    d.update({'validated': data['validated']})

        return serializer.data, st
Exemplo n.º 9
0
    def _create(self, request, data_manager_keys, test_only):

        # compute_data will uncompress data archives to paths which will be
        # hardlinked thanks to datasample pre_save signal.
        # In all other cases, we need to remove those references.

        if not data_manager_keys:
            raise Exception("missing or empty field 'data_manager_keys'")

        self.check_datamanagers(data_manager_keys)  # can raise

        paths_to_remove = []

        try:
            # will uncompress data archives to paths
            computed_data = self.compute_data(request, paths_to_remove)

            serializer = self.get_serializer(data=computed_data, many=True)

            try:
                serializer.is_valid(raise_exception=True)
            except Exception as e:
                pkhashes = [x['pkhash'] for x in computed_data]
                st = status.HTTP_400_BAD_REQUEST
                if find_primary_key_error(e):
                    st = status.HTTP_409_CONFLICT
                raise ValidationException(e.args, pkhashes, st)
            else:

                # create on ledger + db
                ledger_data = {
                    'test_only': test_only,
                    'data_manager_keys': data_manager_keys
                }
                data, st = self.commit(serializer,
                                       ledger_data)  # pre_save signal executed
                return data, st
        finally:
            for gpath in paths_to_remove:
                shutil.rmtree(gpath, ignore_errors=True)