Skip to content

Commit

Permalink
Use send_email wrapper to apply subject with server name when not prod
Browse files Browse the repository at this point in the history
  • Loading branch information
jgreben committed Nov 5, 2024
1 parent 5bb1a11 commit 998aab6
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 172 deletions.
16 changes: 10 additions & 6 deletions libsys_airflow/plugins/data_exports/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from airflow.configuration import conf
from airflow.decorators import task
from airflow.models import Variable
from airflow.utils.email import send_email
from libsys_airflow.plugins.shared.utils import send_email_with_server_name

from libsys_airflow.plugins.shared.utils import is_production

Expand Down Expand Up @@ -132,7 +132,9 @@ def generate_holdings_errors_emails(error_reports: dict):

html_content = _oclc_report_html(report, library)

send_email(to=to_emails, subject=subject_line, html_content=html_content)
send_email_with_server_name(
to=to_emails, subject=subject_line, html_content=html_content
)


def generate_oclc_new_marc_errors_email(error_reports: dict):
Expand Down Expand Up @@ -168,7 +170,9 @@ def generate_oclc_new_marc_errors_email(error_reports: dict):

html_content = _oclc_report_html(report, library)

send_email(to=to_emails, subject=subject_line, html_content=html_content)
send_email_with_server_name(
to=to_emails, subject=subject_line, html_content=html_content
)


def generate_multiple_oclc_identifiers_email(multiple_codes: list):
Expand All @@ -189,7 +193,7 @@ def generate_multiple_oclc_identifiers_email(multiple_codes: list):
html_content = _oclc_identifiers(multiple_codes, folio_url)

if is_production():
send_email(
send_email_with_server_name(
to=[
devs_email,
cohort_emails["business"],
Expand All @@ -203,7 +207,7 @@ def generate_multiple_oclc_identifiers_email(multiple_codes: list):
)
else:
folio_url = folio_url.replace("https://", "").replace(".stanford.edu", "")
send_email(
send_email_with_server_name(
to=[
devs_email,
],
Expand Down Expand Up @@ -269,7 +273,7 @@ def failed_transmission_email(files: list, **kwargs):
run_url,
)

send_email(
send_email_with_server_name(
to=[
devs_to_email_addr,
],
Expand Down
107 changes: 29 additions & 78 deletions libsys_airflow/plugins/digital_bookplates/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from airflow.configuration import conf
from airflow.decorators import task
from airflow.models import Variable
from airflow.utils.email import send_email

from libsys_airflow.plugins.shared.utils import is_production
from libsys_airflow.plugins.shared.utils import send_email_with_server_name

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -158,33 +158,17 @@ def bookplates_metadata_email(**kwargs):
logger.info("Updated bookplate metadata to send in email")

logger.info("Generating email of fetch digital bookplate metadata run")
devs_to_email_addr = Variable.get("EMAIL_DEVS")
bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL")
folio_url = Variable.get("FOLIO_URL")

html_content = _new_updated_bookplates_email_body(
new=new_bookplates,
updated=updated_bookplates,
)

if is_production():
send_email(
to=[
bookplates_email_addr,
devs_to_email_addr,
],
subject="Digital bookplates new and updated metadata",
html_content=html_content,
)
else:
folio_url = folio_url.replace("https://", "").replace(".stanford.edu", "")
send_email(
to=[
devs_to_email_addr,
],
subject=f"{folio_url} - Digital bookplates new and updated metadata",
html_content=html_content,
)
send_email_with_server_name(
to=to_addresses(),
subject="Digital bookplates new and updated metadata",
html_content=html_content,
)


@task
Expand All @@ -198,38 +182,17 @@ def deleted_from_argo_email(**kwargs):
logger.info("No Deleted Druids from Argo")
return

devs_to_email_addr = Variable.get("EMAIL_DEVS")
bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL")
folio_url = Variable.get("FOLIO_URL")

html_content = _deleted_from_argo_email_body(deleted_druids)

if is_production():
send_email(
to=[
bookplates_email_addr,
devs_to_email_addr,
],
subject="Deleted Druids from Argo for Digital bookplates",
html_content=html_content,
)
else:
folio_url = folio_url.replace("https://", "").replace(".stanford.edu", "")
send_email(
to=[
devs_to_email_addr,
],
subject=f"{folio_url} - Deleted Druids from Argo for Digital bookplate",
html_content=html_content,
)
send_email_with_server_name(
to=to_addresses(),
subject="Deleted Druids from Argo for Digital bookplates",
html_content=html_content,
)


@task
def missing_fields_email(**kwargs):
devs_to_email_addr = Variable.get("EMAIL_DEVS")
bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL")
folio_url = Variable.get("FOLIO_URL")

failures = kwargs["failures"]

if len(failures) < 1:
Expand All @@ -238,22 +201,11 @@ def missing_fields_email(**kwargs):

html_content = _missing_fields_body(failures)

if is_production():
send_email(
to=[
bookplates_email_addr,
devs_to_email_addr,
],
subject="Missing Fields for Digital Bookplates",
html_content=html_content,
)
else:
folio_env = folio_url.replace("https://", "").replace(".stanford.edu", "")
send_email(
to=[devs_to_email_addr],
subject=f"{folio_env} - Missing Fields for Digital Bookplates",
html_content=html_content,
)
send_email_with_server_name(
to=to_addresses(),
subject="Missing Fields for Digital Bookplates",
html_content=html_content,
)

return True

Expand All @@ -265,24 +217,23 @@ def summary_add_979_dag_runs(**kwargs):

folio_url = Variable.get("FOLIO_URL")
devs_to_email_addr = Variable.get("EMAIL_DEVS")
bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL")

to_emails = [devs_to_email_addr]
if additional_email:
to_emails.append(additional_email)
html_content = _summary_add_979_email(dag_runs, folio_url)

send_email_with_server_name(
to=to_addresses(),
subject="Summary of Adding 979 fields to MARC Workflows",
html_content=html_content,
)


def to_addresses():
devs_to_email_addr = Variable.get("EMAIL_DEVS")
bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL")
addresses = [devs_to_email_addr]
if is_production():
to_emails.append(bookplates_email_addr)
send_email(
to=to_emails,
subject="Summary of Adding 979 fields to MARC Workflows",
html_content=html_content,
)
else:
folio_env = folio_url.replace("https://", "").replace(".stanford.edu", "")
send_email(
to=to_emails,
subject=f"{folio_env} - Summary of Adding 979 fields to MARC",
html_content=html_content,
)
addresses.append(bookplates_email_addr)
return addresses
14 changes: 3 additions & 11 deletions libsys_airflow/plugins/folio/encumbrances/email.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import re

from airflow.models import Variable
from airflow.utils.email import send_email
from jinja2 import Template

from libsys_airflow.plugins.shared.utils import is_production
from libsys_airflow.plugins.shared.utils import send_email_with_server_name


def email_log(**kwargs):
Expand All @@ -28,7 +25,7 @@ def email_log(**kwargs):
to_addresses.append(lane_email)

with open(log_file, 'r') as fo:
send_email(
send_email_with_server_name(
to=to_addresses,
subject=subject(library=library),
html_content=_email_body(fo),
Expand All @@ -48,9 +45,4 @@ def _email_body(log):

def subject(**kwargs):
library = kwargs.get("library", "")
folio_url = Variable.get("FOLIO_URL", "Test or Stage")
if is_production():
return f"Fix Encumbrances for {library}"
else:
folio_url = re.sub('https?://', '', folio_url)
return f"{folio_url} - Fix Encumbrances for {library}"
return f"Fix Encumbrances for {library}"
8 changes: 5 additions & 3 deletions libsys_airflow/plugins/folio/helpers/bw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from jinja2 import Template

from airflow.models import Variable
from airflow.utils.email import send_email
from libsys_airflow.plugins.shared.utils import send_email_with_server_name

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -123,7 +123,7 @@ def email_bw_summary(devs_email, task_instance):
)
if user_email and len(user_email) > 0:
to_addresses.append(user_email)
send_email(
send_email_with_server_name(
to=to_addresses,
subject=f"Boundwith Summary for file {file_name}",
html_content=html_content,
Expand All @@ -140,7 +140,9 @@ def email_failure(context):

html_body = _bw_error_body(ti, params)

send_email(to=to_addresses, subject=f"Error {ti.task_id}", html_content=html_body)
send_email_with_server_name(
to=to_addresses, subject=f"Error {ti.task_id}", html_content=html_body
)


def create_bw_record(**kwargs) -> dict:
Expand Down
24 changes: 12 additions & 12 deletions libsys_airflow/plugins/orafin/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from jinja2 import Environment, Template

from airflow.models import Variable
from airflow.utils.email import send_email

from libsys_airflow.plugins.orafin.models import Invoice
from libsys_airflow.plugins.orafin.payments import models_converter
from libsys_airflow.plugins.shared.utils import send_email_with_server_name

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -223,7 +223,7 @@ def generate_excluded_email(invoices_reasons: list, folio_url: str):
sul_html_content = _excluded_email_body(sul_invoices, folio_url)
if len(sul_html_content.strip()) > 0:
logger.info(f"Sending email to {sul_to_email_addr} for SUL rejected invoices")
send_email(
send_email_with_server_name(
to=[
sul_to_email_addr,
devs_to_email_addr,
Expand All @@ -238,7 +238,7 @@ def generate_excluded_email(invoices_reasons: list, folio_url: str):
logger.info(
f"Sending email to {bus_to_email_addr} for Business rejected invoices"
)
send_email(
send_email_with_server_name(
to=[bus_to_email_addr, devs_to_email_addr],
subject="Rejected Invoices for Business",
html_content=bus_html_content,
Expand All @@ -247,7 +247,7 @@ def generate_excluded_email(invoices_reasons: list, folio_url: str):
law_html_content = _excluded_email_body(law_invoices, folio_url)
if len(law_html_content.strip()) > 0:
logger.info(f"Sending email to {law_to_email_addr} for Law rejected invoices")
send_email(
send_email_with_server_name(
to=[
law_to_email_addr,
devs_to_email_addr,
Expand Down Expand Up @@ -293,7 +293,7 @@ def generate_invoice_error_email(invoice_id: str, folio_url: str, ti=None):
folio_url=folio_url, invoice_id=invoice_id, row=ap_report_row
)

send_email(
send_email_with_server_name(
to=[
sul_to_email_addr,
bus_to_email_addr,
Expand Down Expand Up @@ -352,7 +352,7 @@ def generate_ap_error_report_email(folio_url: str, ti=None) -> int:
missing_invoices_df, cancelled_invoices_df, paid_invoices_df, folio_url
)

send_email(
send_email_with_server_name(
to=[
sul_to_email_addr,
law_to_email_addr,
Expand Down Expand Up @@ -392,7 +392,7 @@ def generate_ap_paid_report_email(folio_url: str, task_instance=None):
logger.info(
f"Sending email to {sul_to_email_addr} for {len(sul_invoices):,} invoices"
)
send_email(
send_email_with_server_name(
to=[
sul_to_email_addr,
devs_to_email_addr,
Expand All @@ -409,7 +409,7 @@ def generate_ap_paid_report_email(folio_url: str, task_instance=None):
logger.info(
f"Sending email to {bus_to_email_addr} for {len(bus_invoices):,} invoices"
)
send_email(
send_email_with_server_name(
to=[bus_to_email_addr, devs_to_email_addr],
subject=f"Paid Invoices from {ap_report_name} for Business",
html_content=business_html_content,
Expand All @@ -424,7 +424,7 @@ def generate_ap_paid_report_email(folio_url: str, task_instance=None):
logger.info(
f"Sending email to {law_to_email_addr} for {len(law_invoices):,} invoices"
)
send_email(
send_email_with_server_name(
to=[
law_to_email_addr,
devs_to_email_addr,
Expand Down Expand Up @@ -456,7 +456,7 @@ def generate_summary_email(invoices: list, folio_url: str):
logger.info(
f"Sending email to {sul_to_email_addr} for {len(sul_invoices)} invoices"
)
send_email(
send_email_with_server_name(
to=[
sul_to_email_addr,
devs_to_email_addr,
Expand All @@ -471,7 +471,7 @@ def generate_summary_email(invoices: list, folio_url: str):
logger.info(
f"Sending email to {bus_to_email_addr} for {len(bus_invoices):,} invoices"
)
send_email(
send_email_with_server_name(
to=[bus_to_email_addr, devs_to_email_addr],
subject="Approved Invoices Sent to AP for Business",
html_content=business_html_content,
Expand All @@ -483,7 +483,7 @@ def generate_summary_email(invoices: list, folio_url: str):
logger.info(
f"Sending email to {law_to_email_addr} for {len(law_invoices):,} invoices"
)
send_email(
send_email_with_server_name(
to=[
law_to_email_addr,
devs_to_email_addr,
Expand Down
Loading

0 comments on commit 998aab6

Please sign in to comment.