""" Export Status CRUD operations """ from sqlalchemy import update from bulkperformancerights import config from bulkperformancerights.models.export_status import ExportStatus from bulkperformancerights.connectors import mysql from bulkperformancerights.logic import job from bulkperformancerights.logic import user from bulkperformancerights.templates import email input_file_name = 'Manage Rights Template V1.0.json' HTML_EMAIL_CONTENT = ''' Hey there,

We're happy to let you know that your requested spreadsheet is now available for download on the Master Rights Management page in the Workstation. For a detailed step-by-step and pro tips, please visit our Product Guide in the Help Center.

Your Team at The Orchard ''' TEXT_EMAIL_BODY = ''' Hey there, We're happy to let you know that your requested spreadsheet is now available for download on the Master Rights Management {manage_rights_url} page in the Workstation. For a detailed step-by-step and pro tips, please visit our Product Guide {user_guide_url} in the Help Center. Your Team at The Orchard ''' EMAIL_SUBJECT = 'The metadata spreadsheet you requested is now available' user_guide_url = '{}/helpcenter/guides#masterrightsmgmt'.format( config.WORKSTATION_BASE_URL) manage_rights_url = '{}/managerights'.format(config.WORKSTATION_BASE_URL) def get(user_type, user_id, job_id): """Get export status Args: user_type (str): 'vendor' or 'subaccount', see logic/user.py user_id (int): indentifier of user for which to get export status job_id (int): optional, job id to get Return: (dict): either a single ExportStatus or multiple """ session = mysql.session() if job_id is None: export_statuses = session.query(ExportStatus).filter_by( user_type=user_type, user_id=user_id) response = [decorate(stat.dict()) for stat in export_statuses] else: # using first() because there should only ever be one export_status = session.query(ExportStatus).filter_by(job_id=job_id).\ first() if not export_status: session.close() assert export_status, 'no job found with that id' response = decorate(export_status.dict()) session.close() return response def create(user_type, user_id): """Create export status record Initial job_status is 'querying' Args: input_file_path (str): path to input file user_type (str): type of user user_id (int): id of user Return: (ExportStatus): ExportStatus model """ export_status = ExportStatus( input_file_path=input_file_name, job_status='querying', user_type=user_type, user_id=user_id) Session = mysql.session() Session.add(export_status) Session.commit() Session.close() return export_status def amend(export_status, **kwargs): """Amend an export_status record called amend because we are using sqlalchemy's 'update' method Args: export_status (ExportStatus): export status record to be updated **kwargs: Possible values are any fields available on ExportStatus Return: (ResultProxy): result of the update operation """ Session = mysql.session() update_stmt = update(ExportStatus).where( ExportStatus.job_id == export_status.job_id).values(**kwargs) result = Session.execute(update_stmt) Session.commit() Session.close() return result def decorate(export_status): """Decorates an export_status record: constructs the location on s3 This is meant for outward facing representations of export_status Args: export_status (ExportStatus): export_status to be decorated Return: (ExportStatus): decorated export status object """ user_dict = {'type': export_status.get('user_type'), 'id': export_status.get('user_id')} export_status['input_file_path'] = job.s3_path_for_user_from_file( export_status.get('input_file_path'), user_dict) return export_status def send_complete_mail(ses_conn, user_id, user_email, user_type='vendor'): """Email user that job is complete Args: ses_conn (boto.ses.Ses): SES connection user_id (int): user_email (str): email address to where an email is sent user_type (str): vendor or subaccount """ user_info = user.user_info(user_id, user_type) vendor_logo_path = user_info.get('vendor_logo_path') vendor_contact_email = user_info.get('vendor_contact_email') return ses_conn.send_email( source=config.EMAIL_FROM_ADDRESS, subject=EMAIL_SUBJECT, body=TEXT_EMAIL_BODY.format( user_guide_url=user_guide_url, manage_rights_url=manage_rights_url), html_body=email.generate_body( vendor_logo_path, vendor_contact_email, 'Bulk Performance Rights', HTML_EMAIL_CONTENT.format( user_guide_url=user_guide_url, manage_rights_url=manage_rights_url)), to_addresses=[user_email])