Skip to content

Commit

Permalink
Minor fixes to processing reports and future testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Simran Mattu committed Oct 10, 2024
1 parent a30c9a2 commit 6fd749b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 56 deletions.
4 changes: 2 additions & 2 deletions etc/woudc-contributor-feedback.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
39 changes: 16 additions & 23 deletions woudc_data_registry/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
60 changes: 29 additions & 31 deletions woudc_data_registry/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -101,42 +92,49 @@ def send_email(

send_statuses = []
cc = False
LOGGER.debug('cc: {}' .format(cc_addresses))
# cc
if all([
cc_addresses is not None,
cc_addresses != ['']
]):
to_email_addresses += cc_addresses
cc = True

LOGGER.debug('bcc: {}' .format(bcc_addresses))
# bcc
if all([
bcc_addresses is not None,
bcc_addresses != ['']
]):
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()

Expand Down

0 comments on commit 6fd749b

Please sign in to comment.