"""Converter logic, communicating with ows-xlsx """ import json import requests from time import sleep from bulkperformancerights.logic import user from bulkperformancerights import config polling_wait = 10 exception_template = 'Manage Rights Exception Report V1.0.xlsx' metadata_template = 'Manage Rights Template V1.0.xlsx' def json_to_xlsx(bucket, template, filename, user_type, user_id): """Request ows-xlsx converts json to xlsx, long poll for completion Args: bucket (boto.s3.bucket.Bucket): s3 bucket template (str): template xlsx filename used for conversion filename (str): filename of json user_type (str): type of user (vendor or subaccount) user_id (int): identifier of user """ user_dict = {'type': user_type, 'id': user_id} user_s3_path = user.prefix_for_user(user_dict) error_json_to_xlsx_payload = {'owner_type': user_type, 'owner_id': user_id, 'filename': filename, 'template': template} convert_json_response = requests.post('{}jsontoxlsx'.format( config.xlsx_to_json_base_url), data=error_json_to_xlsx_payload) assert convert_json_response.status_code == 200, \ 'conversion failed, message: {}'.format(convert_json_response.text) # TODO (OrCharles): We are allowing for one jsontoxlsx conversion per user # When the jsontoxlsx service is updated to handle more # we will use the job_id provided job_id = '{}/{}'.format(user_type, user_id) converted_xlsx_path = None while converted_xlsx_path is None: status_response = json_to_xlsx_status(job_id) conversion_status = status_response.get('job_status') assert conversion_status != 'FAILURE', \ 'json to xlsx conversion failed' if conversion_status == 'SUCCESS': converted_xlsx_path = status_response.get('xlsx_path') sleep(polling_wait) # COPY FILE FROM XLSX SERVICE TO managerights/ xlsx_error_filename = filename.replace('.json', '.xlsx') error_xslx_source_key = bucket.get_key(converted_xlsx_path) error_xlsx_dest_key_name = '{}{}{}'.format(config.prefix, user_s3_path, xlsx_error_filename) error_xslx_source_key.copy(bucket, error_xlsx_dest_key_name) return xlsx_error_filename def xlsx_to_json(bucket, filename, user_type, user_id): """Request ows-xlsx converts json to xlsx, long poll for completion Args: bucket (boto.s3.bucket.Bucket): s3 bucket filename (str): filename of json user_type (str): type of user (vendor or subaccount) user_id (int): identifier of user """ user_dict = {'type': user_type, 'id': user_id} user_s3_path = user.prefix_for_user(user_dict) xlsx_user_s3_path = user.xlsx_to_json_prefix_for_user(user_dict) dest_key_name = '{}xlsx/{}{}'.format(config.xlsx_prefix, xlsx_user_s3_path, filename) source_key_name = config.prefix + user_s3_path + filename # strip the prefixed / so this is a valid key source_key = bucket.get_key(source_key_name[1:]) source_key.copy(bucket, dest_key_name) xlsx_to_json_base_url = config.xlsx_to_json_base_url xlsx_to_json_payload = {'owner_type': user_type, 'owner_id': user_id, 'filename': filename} return requests.post('{}xlsxtojson'.format(xlsx_to_json_base_url), data=xlsx_to_json_payload) def json_to_xlsx_status(job_id): """Retrieves conversion status of job from ows-xlsx Args: job_id (str): job identifier provided from ows-xlsx Returns: (dict): either with single key of 'job_status' or if job_status is 'SUCCESS' an accompanying path to the converted file """ status_response = requests.get('{}jsontoxlsx/status/{}'.format( config.xlsx_to_json_base_url, job_id)) return json.loads(status_response.text)