"""Module contains label copy export logic: metadata generation and history.""" from oto import response from project_manager import config from project_manager.connector import s3 from project_manager.connector import sqs from project_manager.logic import project_manager from project_manager.models import label_copy_history from project_manager.util import handler_util def trigger_project_metadata_generation(headers, project_id): """Trigger csv metadata generation for single project. Validate request headers, project ownership and put generation request to sqs queue. Args: headers (dict): request grass headers. project_id (int): id of project for which metadata should be generated. Return: Response: oto response with project id or errors. """ access_check_response = handler_util.access_check(headers) if not access_check_response: return access_check_response header_user_id = headers.get('Orchard-User-Id', '') header_account_type = access_check_response.message.get('account_type') header_account_id = access_check_response.message.get('account_id') if header_account_id and header_account_type: ownership_response = project_manager.check_project_ownership( project_id, **{header_account_type + '_id': header_account_id}) if not ownership_response: return ownership_response record = label_copy_history.create_job( user_id=header_user_id, project_id=project_id) if not record: return record sqs_response = sqs.put_message_to_sqs( queue_name=config.LCE_SQS_QUEUE, payload={ 'project_id': project_id, 'job_id': record.message}, orchard_user_id=header_user_id) if not sqs_response: return sqs_response return response.Response({'project_id': project_id}) def get_generated_metadata_for_project( project_id, account_type=None, account_id=None): """Get csv metadata generation for single project. Validate request headers, project ownership and put generation request to sqs queue. Args: project_id (int): id of project for which metadata should be generated. account_type (str): Requester's account type. Either header_const.GRASS_ACCOUNT_TYPE_VENDOR or header_const.GRASS_ACCOUNT_TYPE_SUBACCOUNT (optional). account_id (int): Account ID to retrieve imprints for. If the account_type arg is header_const.GRASS_ACCOUNT_TYPE_VENDOR, account_id is a vendor_id. If the account_type arg is header_const.GRASS_ACCOUNT_TYPE_SUBACCOUNT, account_id is a subaccount_id (optional). Return: Response: oto response with s3 url and timestamp or errors. """ if account_id and account_type: ownership_response = project_manager.check_project_ownership( project_id, **{account_type + '_id': account_id}) if not ownership_response: return ownership_response job = label_copy_history.get_job_by_project_id( project_id=project_id) if not job: return job job_data = job.message.to_dict() filename = '{timestamp}_{project_id}.csv'.format( timestamp=job_data['last_change_date'].strftime('%Y%m%d'), project_id=job_data['project_id']) s3_result = s3.get_s3_file_url(filename=filename) if not s3_result: return s3_result return response.Response({ 'download_url': s3_result.message, 'timestamp': str(job_data['last_change_date'])})