generated from NASA-PDS/template-repo-java
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #360 from NASA-PDS/283-weekly-roundup
283 weekly roundup email notification
- Loading branch information
Showing
18 changed files
with
461 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os.path | ||
from pds_doi_service.core.actions.roundup import run as run_weekly_roundup | ||
from pds_doi_service.core.db.doi_database import DOIDataBase | ||
|
||
from pds_doi_service.core.util.config_parser import DOIConfigUtil | ||
from pds_doi_service.core.util.general_util import get_logger | ||
|
||
if __name__ == '__main__': | ||
""" | ||
Send an email consisting of a summary of all DOIs updated in the previous week (i.e. between the previous Sunday | ||
and the Monday before that, inclusive), with a JSON attachment for those DoiRecords. | ||
Should be run in a crontab, preferably on Monday, for example: | ||
0 0 * * MON . path/to/doi-service/venv/bin/python path/to/doi-service/scripts/email_weekly_roundup.py | ||
""" | ||
|
||
logging = get_logger('email_weekly_roundup') | ||
config = DOIConfigUtil.get_config() | ||
db_filepath = os.path.abspath(config['OTHER']['db_file']) | ||
sender_email_address = config['OTHER']['emailer_sender'] | ||
receiver_email_address = config['OTHER']['emailer_receivers'] | ||
db = DOIDataBase(db_filepath) | ||
|
||
run_weekly_roundup(db, sender_email_address, receiver_email_address) | ||
|
||
logging.info('Completed DOI weekly roundup email transmission') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# | ||
# Copyright 2022, by the California Institute of Technology. ALL RIGHTS | ||
# RESERVED. United States Government Sponsorship acknowledged. Any commercial | ||
# use must be negotiated with the Office of Technology Transfer at the | ||
# California Institute of Technology. | ||
# | ||
""" | ||
====== | ||
roundup.py | ||
====== | ||
Contains functions for sending email notifications for recently-updated DOIs. | ||
""" | ||
import json | ||
import logging | ||
import os | ||
from datetime import date | ||
from datetime import datetime | ||
from datetime import timedelta | ||
from email import encoders | ||
from email.mime.base import MIMEBase | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from typing import Dict | ||
from typing import List | ||
|
||
import jinja2 | ||
from pds_doi_service.core.db.doi_database import DOIDataBase | ||
from pds_doi_service.core.entities.doi import DoiRecord | ||
from pds_doi_service.core.util.emailer import Emailer as PDSEmailer | ||
from pkg_resources import resource_filename | ||
|
||
|
||
def get_start_of_local_week() -> datetime: | ||
"""Return the start of the local-timezones week as a tz-aware datetime""" | ||
today = datetime.now().date() | ||
start_of_today = datetime(today.year, today.month, today.day) | ||
return start_of_today.astimezone() - timedelta(days=today.weekday()) | ||
|
||
|
||
def fetch_dois_modified_between(begin: datetime, end: datetime, database: DOIDataBase) -> List[DoiRecord]: | ||
doi_records = database.select_latest_records({}) | ||
return [r for r in doi_records if begin <= r.date_added < end or begin <= r.date_updated < end] | ||
|
||
|
||
def get_email_content_template(template_filename: str = "email_weekly_roundup.jinja2"): | ||
template_filepath = resource_filename(__name__, os.path.join("templates", template_filename)) | ||
logging.info(f"Using template {template_filepath}") | ||
with open(template_filepath, "r") as infile: | ||
template = jinja2.Template(infile.read()) | ||
return template | ||
|
||
|
||
def prepare_doi_record_for_template(record: DoiRecord) -> Dict[str, str]: | ||
"""Map a DoiRecord to the set of information required for rendering it in the template""" | ||
update_type = "submitted" if record.date_added == record.date_updated else "updated" | ||
prepared_record = { | ||
"datacite_id": record.doi, | ||
"pds_id": record.identifier, | ||
"update_type": update_type, | ||
"last_modified": record.date_updated, | ||
"status": record.status.title(), | ||
} | ||
|
||
return prepared_record | ||
|
||
|
||
def prepare_email_html_content(first_date: date, last_date: date, modified_doi_records: List[DoiRecord]) -> str: | ||
template = get_email_content_template() | ||
template_dict = { | ||
"first_date": first_date, | ||
"last_date": last_date, | ||
"doi_records": [prepare_doi_record_for_template(r) for r in modified_doi_records], | ||
} | ||
|
||
full_content = template.render(template_dict) | ||
logging.info(full_content) | ||
return full_content | ||
|
||
|
||
def attach_json_data(filename: str, doi_records: List[DoiRecord], msg: MIMEMultipart) -> None: | ||
data = json.dumps([r.to_json_dict() for r in doi_records]) | ||
part = MIMEText(data, "plain", "utf-8") | ||
part.set_charset("utf-8") | ||
encoders.encode_base64(part) | ||
part.add_header("Content-Disposition", f"attachment; filename={filename}") | ||
msg.attach(part) | ||
|
||
|
||
def prepare_email_message( | ||
sender_email: str, receiver_email: str, first_date: date, last_date: date, modified_doi_records: List[DoiRecord] | ||
) -> MIMEMultipart: | ||
email_subject = f"DOI WEEKLY ROUNDUP: {first_date} through {last_date}" | ||
|
||
msg = MIMEMultipart() | ||
msg["From"] = sender_email | ||
msg["Subject"] = email_subject | ||
msg["To"] = receiver_email | ||
|
||
email_content = prepare_email_html_content(first_date, last_date, modified_doi_records) | ||
msg.attach(MIMEText(email_content, "html")) | ||
attachment_filename = f"updated_dois_{first_date.isoformat()}_{last_date.isoformat()}.json" | ||
attach_json_data(attachment_filename, modified_doi_records, msg) | ||
|
||
return msg | ||
|
||
|
||
def run(database: DOIDataBase, sender_email: str, receiver_email: str) -> None: | ||
""" | ||
Send an email consisting of a summary of all DOIs updated in the previous week (i.e. between the previous Sunday | ||
and the Monday before that, inclusive), with a JSON attachment for those DoiRecords. | ||
""" | ||
target_week_begin = get_start_of_local_week() - timedelta(days=7) | ||
target_week_end = target_week_begin + timedelta(days=7, microseconds=-1) | ||
last_date_of_week = (target_week_end - timedelta(microseconds=1)).date() | ||
|
||
modified_doi_records = fetch_dois_modified_between(target_week_begin, target_week_end, database) | ||
|
||
msg = prepare_email_message( | ||
sender_email, receiver_email, target_week_begin.date(), last_date_of_week, modified_doi_records | ||
) | ||
|
||
emailer = PDSEmailer() | ||
emailer.send_message(msg) |
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
src/pds_doi_service/core/actions/templates/email_weekly_roundup.jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h2>DataCite DOI Weekly Roundup</h2> | ||
{% if doi_records %} | ||
<p>The following DOIs were reserved, updated or released during the week {{ first_date.strftime('%m/%d') }} through {{ last_date.strftime('%m/%d') }}:</p> | ||
<ul> | ||
{% for doi in doi_records %} | ||
<li>{{ doi.datacite_id }} {{ doi.pds_id }} ({{ doi.status }}) {{ doi.update_type }} {{ doi.last_modified.strftime('%Y-%m-%d') }}</li> | ||
{% endfor %} | ||
</ul> | ||
{% else %} | ||
<p>No DOI records were reserved, updated or released modified during the week {{ first_date.strftime('%m/%d') }} through {{ last_date.strftime('%m/%d') }}.</p> | ||
{% endif %} | ||
|
||
<p><a href="mailto:Jordan.Padams@jpl.nasa.gov?subject=doi-service DOI Weekly Roundup">Contact Us</a></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/pds_doi_service/core/actions/test/data/roundup/roundup_email_attachment.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[ | ||
{ | ||
"identifier": "urn:nasa:pds:product_11111::1.0", | ||
"status": "Pending", | ||
"date_added": "2022-09-09T17:44:30.165920+00:00", | ||
"date_updated": "2022-09-09T17:44:30.165920+00:00", | ||
"submitter": "img-submitter@jpl.nasa.gov", | ||
"title": "Laboratory Shocked Feldspars Bundle", | ||
"type": "Collection", | ||
"subtype": "PDS4 Collection", | ||
"node_id": "img", | ||
"doi": "10.17189/11111", | ||
"transaction_key": "./transaction_history/img/2020-06-15T18:42:45.653317", | ||
"is_latest": true | ||
}, | ||
{ | ||
"identifier": "urn:nasa:pds:product_22222::1.0", | ||
"status": "Pending", | ||
"date_added": "2022-08-14T17:44:30.165940+00:00", | ||
"date_updated": "2022-09-09T17:44:30.165940+00:00", | ||
"submitter": "img-submitter@jpl.nasa.gov", | ||
"title": "Laboratory Shocked Feldspars Bundle", | ||
"type": "Collection", | ||
"subtype": "PDS4 Collection", | ||
"node_id": "img", | ||
"doi": "10.17189/22222", | ||
"transaction_key": "./transaction_history/img/2020-06-15T18:42:45.653317", | ||
"is_latest": true | ||
} | ||
] |
13 changes: 13 additions & 0 deletions
13
src/pds_doi_service/core/actions/test/data/roundup/roundup_email_body.jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h2>DataCite DOI Weekly Roundup</h2> | ||
|
||
<p>The following DOIs were reserved, updated or released during the week {{ week_start.strftime('%m/%d') }} through {{ week_end.strftime('%m/%d') }}:</p> | ||
<ul> | ||
|
||
<li>10.17189/11111 urn:nasa:pds:product_11111::1.0 (Pending) submitted {{ modifications_date.isoformat() }}</li> | ||
|
||
<li>10.17189/22222 urn:nasa:pds:product_22222::1.0 (Pending) updated {{ modifications_date.isoformat() }}</li> | ||
|
||
</ul> | ||
|
||
|
||
<p><a href="mailto:Jordan.Padams@jpl.nasa.gov?subject=doi-service DOI Weekly Roundup">Contact Us</a></p> |
Oops, something went wrong.