From 6fd749be1a6f696e4d594cf972dabe76ea97ab4e Mon Sep 17 00:00:00 2001 From: Simran Mattu Date: Thu, 10 Oct 2024 19:32:52 +0000 Subject: [PATCH] Minor fixes to processing reports and future testing --- etc/woudc-contributor-feedback.txt | 4 +- woudc_data_registry/controller.py | 39 ++++++++----------- woudc_data_registry/util.py | 60 +++++++++++++++--------------- 3 files changed, 47 insertions(+), 56 deletions(-) diff --git a/etc/woudc-contributor-feedback.txt b/etc/woudc-contributor-feedback.txt index e86f561..3d9e6fb 100644 --- a/etc/woudc-contributor-feedback.txt +++ b/etc/woudc-contributor-feedback.txt @@ -8,7 +8,7 @@ Where possible, your submitted files have been manually repaired to contain the If your station, platform, agency or contact information have been updated then please reply to this message with the updated information. Thank you for your support of WOUDC. -email_summary +$EMAIL_SUMMARY World Ozone and Ultraviolet Radiation Data Centre @@ -17,7 +17,7 @@ Environment and Climate Change Canada 4905 Dufferin Street Toronto, ON M3H 5T4 Canada -Email: ec.woudc.ec@canada.ca +Email: woudc@ec.gc.ca Website: https://woudc.org Data policy: https://woudc.org/about/data-policy.php Data submissions: ftp://ftp.woudc.org diff --git a/woudc_data_registry/controller.py b/woudc_data_registry/controller.py index c972c69..a5136b0 100644 --- a/woudc_data_registry/controller.py +++ b/woudc_data_registry/controller.py @@ -275,6 +275,8 @@ def generate_emails(ctx, working_dir): @click.argument('failed_files', type=click.File('r')) def send_feedback(ctx, failed_files, test, ops): """Send operating reports to contributors. """ + + LOGGER.debug("test: {} ops: {}".format(test, ops)) with open(config.WDR_TEMPLATE_PATH, 'r') as file: message = file.read() @@ -288,39 +290,30 @@ def send_feedback(ctx, failed_files, test, ops): cc_addresses = [config.WDR_EMAIL_CC] bcc_addresses = [config.WDR_EMAIL_BCC] + LOGGER.info('Configs all set to send feedback to contributors') + for contributor in template_collection: acronym = contributor[0].split(' ')[0].lower() message = message.replace( - 'email_summary', "\n".join(contributor[1:])) + "$EMAIL_SUMMARY", "\n".join(contributor[1:])) specific_subject = subject.replace('contributor_acronym', acronym) + if test: - to_email_addresses = [config.WDR_EMAIL_TO] + to_email_addresses = config.WDR_EMAIL_TO.split(",") subject = ( - 'TEST: WOUDC data processing report (contributor_acronym)') - send_email( - message, - specific_subject, - from_email_address, - to_email_addresses, - host, - port, - cc_addresses, - bcc_addresses - ) + 'TEST: WOUDC data processing report ({})'.format(acronym)) + LOGGER.info('Sending Test data report to agency: {} with emails to: {}'.format(acronym, to_email_addresses)) + send_email(message, subject, from_email_address, to_email_addresses, + host, port, cc_addresses, bcc_addresses) elif ops: to_email_addresses = [ email.strip() for email in contributor[0].split(' ')[1] .translate(str.maketrans("", "", "()")).split(";")] - send_email( - message, - specific_subject, - from_email_address, - to_email_addresses, - host, - port, - cc_addresses, - bcc_addresses - ) + LOGGER.info('Sending data report to agency: {} with emails to: {}'.format(acronym, to_email_addresses)) + send_email(message, specific_subject, from_email_address, to_email_addresses, + host, port, cc_addresses, bcc_addresses) + LOGGER.debug('Sent email to {} with emails to ()'.format(acronym, to_email_addresses)) + LOGGER.info('Processing Reports have been sent') data.add_command(ingest) diff --git a/woudc_data_registry/util.py b/woudc_data_registry/util.py index 414694e..ca08b7f 100644 --- a/woudc_data_registry/util.py +++ b/woudc_data_registry/util.py @@ -55,18 +55,9 @@ RFC3339_DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ' -def send_email( - message, - subject, - from_email_address, - to_email_addresses, - host, - port, - cc_addresses=None, - bcc_addresses=None, - secure=False, - from_email_password=None - ): +def send_email(message, subject, from_email_address, to_email_addresses, + host, port, cc_addresses=None, bcc_addresses=None, secure=False, + from_email_password=None): """ Send email @@ -101,6 +92,7 @@ def send_email( send_statuses = [] cc = False + LOGGER.debug('cc: {}' .format(cc_addresses)) # cc if all([ cc_addresses is not None, @@ -108,6 +100,8 @@ def send_email( ]): to_email_addresses += cc_addresses cc = True + + LOGGER.debug('bcc: {}' .format(bcc_addresses)) # bcc if all([ bcc_addresses is not None, @@ -115,28 +109,32 @@ def send_email( ]): to_email_addresses += bcc_addresses + LOGGER.debug('to_email: {}' .format(to_email_addresses)) if isinstance(to_email_addresses, str): to_email_addresses = to_email_addresses.split(';') - for to in to_email_addresses: - # set up the message - msg = MIMEMultipart() - msg['From'] = from_email_address - msg['To'] = to - if cc: - msg['cc'] = ','.join(cc_addresses) - msg['Subject'] = subject - msg.attach(MIMEText(message, 'plain')) - text = msg.as_string() - # send message - try: - send_status = server.sendmail(msg['From'], msg['To'], text) - send_statuses.append(send_status) - except Exception as err: - msg = 'Unable to send mail from: {} to {}: {}'.format( - msg['From'], msg['To'], err) - LOGGER.error(msg) - raise err + # set up the message + msg = MIMEMultipart() + msg['From'] = from_email_address + msg['To'] = ', '.join(to_email_addresses) # Join all emails into one string separated by commas + if cc: + msg['Cc'] = ', '.join(cc_addresses) # Add CC addresses if they exist + msg['Subject'] = subject + msg.attach(MIMEText(message, 'plain')) + + # Convert the message to a string + text = msg.as_string() + LOGGER.debug('Message: {}' .format(text)) + + # send message + try: + LOGGER.debug('Sending a data report to the groups of emails: {}'.format(to_email_addresses)) + send_status = server.sendmail(msg['From'], to_email_addresses + cc_addresses, text) + send_statuses.append(send_status) + except Exception as err: + error_msg = 'Unable to send mail from: {} to {}: {}'.format(msg['From'], msg['To'], err) + LOGGER.error(error_msg) + raise err server.quit()