def _process_post(location, transaction_type=''):
    """
    :param dict location:
    :param str transaction_type: Will be assigned to the 'trans_type' field in the exported file
    :return: tuple (response_data, response_status)
    """
    missing_keys = _missing_keys(location)
    if missing_keys:
        return {
            'error_message':
            'Missing keys: {0}'.format(', '.join(missing_keys))
        }, 400

    else:
        file_name = transaction_file_name(location)
        output_fd = BytesIO()
        tiername = application.config['TIERNAME']
        s3_bucket = application.config['S3_BUCKET']
        aws_region = application.config['AWS_REGION']
        destination_key = 'transactions/{0}/{1}'.format(tiername, file_name)
        write_transaction(output_fd,
                          location,
                          transaction_type=transaction_type)
        output_fd.seek(0)
        try:
            upload_to_s3(output_fd, destination_key, s3_bucket, aws_region)
        except (OSError, ValueError, ParamValidationError):
            return 'Unable to write the file', 500
        else:
            return 'File written to s3://{0}/{1}'.format(
                s3_bucket, destination_key), 200
 def test_normal_location(self):
     self.maxDiff = None
     write_transaction(self.fd,
                       self.location_change,
                       transaction_type='Change')
     self.assertEqual(
         self.fd.getvalue(),
         'trans_type=Change\n{0}'.format(self.expected_response).encode())
Example #3
0
def _process_post(location, expected_model, transaction_type=''):
    """
    :param dict location:
    :param dict expected_model:
    :param str transaction_type: Will be assigned to the 'trans_type' field in the exported file
    :return: tuple (response_data, response_status)
    """
    expected_keys = set(iter(expected_model.keys()))

    missing_keys = _missing_keys(location, expected_keys)
    if missing_keys:
        return {
            'error_message':
            'Missing keys: {0}'.format(', '.join(missing_keys))
        }, 400

    else:
        file_name = transaction_file_name(location)
        output_fd = BytesIO()
        s3_bucket = application.config['S3_BUCKET']
        aws_region = application.config['AWS_REGION']
        endpoint = application.config['S3_ENDPOINT_URL']
        destination_key = 'transactions/{0}'.format(file_name)
        write_transaction(output_fd,
                          location,
                          transaction_type=transaction_type)
        output_fd.seek(0)
        try:
            upload_to_s3(output_fd, destination_key, s3_bucket, aws_region,
                         endpoint)
        except (OSError, ValueError, ParamValidationError) as err:
            application.logger.error(
                "An error occurred while attempting to upload the file to S3: "
                + str(err))
            return {'error_message': 'Unable to write the file to S3.'}, 500
        else:
            return 'File written to s3://{0}/{1}'.format(
                s3_bucket, destination_key), 200
 def test_empty_dict(self):
     write_transaction(self.fd, {}, transaction_type='Create')
     self.assertEqual(self.fd.getvalue(), 'trans_type=Create\nDONE'.encode())