コード例 #1
0
 def test_with_site_number_with_trailing_spaces(self):
     self.assertEqual(
         transaction_file_name({
             'agencyCode': 'USGS',
             'siteNumber': '0123456789   ',
             'updated': '2017-10-03 13:30:45'
         }), 'mlr.USGS.0123456789.20171003133045')
コード例 #2
0
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
コード例 #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
コード例 #4
0
 def test_with_no_updated(self):
     self.assertEqual(transaction_file_name({'agencyCode': 'USGS', 'siteNumber': '0123456789012'}),
                      'mlr.0123456789012.')
コード例 #5
0
 def test_no_site_number(self):
     self.assertEqual(transaction_file_name({'agencyCode': 'USGS', 'updated': '2017-10-03 13:30:45'}), 'mlr..20171003133045')
コード例 #6
0
 def test_remove_spaces_on_left(self):
     self.assertEqual(
         transaction_file_name({
             'agencyCode': '  USGS  ',
             'siteNumber': '0123456789012'
         }), 'mlr.USGS.0123456789012.')