Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use send_email wrapper to apply subject with server name when not prod #1407

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
117 changes: 34 additions & 83 deletions libsys_airflow/plugins/digital_bookplates/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
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 (
is_production,
send_email_with_server_name,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -133,6 +135,15 @@ def _summary_add_979_email(dag_runs: list, folio_url: str) -> str:
).render(dag_runs=dag_runs, folio_url=folio_url, dag_url=dag_url)


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():
addresses.append(bookplates_email_addr)
return addresses


@task
def bookplates_metadata_email(**kwargs):
"""
Expand All @@ -158,33 +169,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 +193,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 +212,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 @@ -262,27 +225,15 @@ def missing_fields_email(**kwargs):
def summary_add_979_dag_runs(**kwargs):
dag_runs = kwargs["dag_runs"]
additional_email = kwargs.get("email")

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]
to_emails = _to_addresses()
if additional_email:
to_emails.append(additional_email)
html_content = _summary_add_979_email(dag_runs, folio_url)

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,
)
send_email_with_server_name(
to=to_emails,
subject="Summary of Adding 979 fields to MARC Workflows",
html_content=html_content,
)
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
Loading
Loading