From 31ae27b5e6f8ef7cbcd5b652ba757ba14f20686c Mon Sep 17 00:00:00 2001 From: Maximilian Moser <maximilian.moser@tuwien.ac.at> Date: Tue, 18 Mar 2025 19:32:54 +0100 Subject: [PATCH] Update publication notification to use a separate jinja template * this is quite a bit simpler than cobbling together the message in a normal python function --- invenio_config_tuw/tasks.py | 59 +++++++------------ .../email/group-notification.jinja | 1 - .../email/record-publication.jinja | 51 ++++++++++++++++ tests/test_misc.py | 10 +++- 4 files changed, 79 insertions(+), 42 deletions(-) create mode 100644 invenio_config_tuw/users/templates/invenio_notifications/email/record-publication.jinja diff --git a/invenio_config_tuw/tasks.py b/invenio_config_tuw/tasks.py index f86bcb8..906c29f 100644 --- a/invenio_config_tuw/tasks.py +++ b/invenio_config_tuw/tasks.py @@ -8,13 +8,11 @@ """Celery tasks running in the background.""" from difflib import HtmlDiff -from typing import Optional from celery import shared_task from celery.schedules import crontab from flask import current_app, url_for from invenio_access.permissions import system_identity -from invenio_accounts.proxies import current_datastore from invenio_db import db from invenio_files_rest.models import FileInstance from invenio_notifications.registry import ( @@ -27,21 +25,15 @@ from .notifications import UserNotificationBuilder @shared_task(ignore_result=True) -def send_publication_notification(recid: str, user_id: Optional[str] = None): +def send_publication_notification(recid: str): """Send the record uploader an email about the publication of their record.""" record = records_service.read(identity=system_identity, id_=recid)._obj - record_title = record["metadata"]["title"] - if user_id is not None: - user = current_datastore.get_user(user_id) - else: - owner = record.parent.access.owner - if owner is not None and owner.owner_type == "user": - user = owner.resolve() - else: - current_app.logger.warn( - f"Couldn't find owner of record '{recid}' for sending email!" - ) - return + record.relations.clean() + if (owner := record.parent.access.owner) is None: + current_app.logger.warn( + f"Record '{recid}' has no owner to notify about its publication!" + ) + return # build the message datacite_test_mode = current_app.config["DATACITE_TEST_MODE"] @@ -49,40 +41,29 @@ def send_publication_notification(recid: str, user_id: Optional[str] = None): doi = record["pids"]["doi"]["identifier"] if datacite_test_mode: - doi_base_url = "https://handle.test.datacite.org" - doi_type = "DOI-like handle" + base_url = "https://handle.test.datacite.org" + pid_type = "DOI-like handle" else: - doi_base_url = "https://doi.org" - doi_type = "DOI" + base_url = "https://doi.org" + pid_type = "DOI" - doi_url = f"{doi_base_url}/{doi}" - link_line = f"It is now available under the following {doi_type}: {doi_url}" - link_line_html = f'It is now available under the following {doi_type}: <a href="{doi_url}">{doi_url}</a>' + pid_url = f"{base_url}/{doi}" else: - landing_page_url = url_for( + pid_type = "URL" + pid_url = url_for( "invenio_app_rdm_records.record_detail", pid_value=record.pid.pid_value, _external=True, ) - link_line = f"It is now available under the following URL: {landing_page_url}" - link_line_html = f'It is now available under the following URL: <a href="{landing_page_url}">{landing_page_url}</a>' - - publish_line = f'Your record "{record_title}" just got published!' - edits_line = "Metadata edits for this record will *not* require another review." - edits_line_html = ( - "Metadata edits for this record will <em>not</em> require another review." - ) - - message = "\n".join([publish_line, link_line, "", edits_line]) - html_message = "<br />".join([publish_line, link_line_html, "", edits_line_html]) # send the notification - notification = UserNotificationBuilder().build( - receiver={"user": user.id}, - subject=f'Your record "{record_title}" was published', - message=message, - html_message=html_message, + notification = UserNotificationBuilder.build( + receiver=owner.dump(), + subject=f'Your record "{record.metadata['title']}" was published', + record=record, + record_pid={"type": pid_type, "url": pid_url}, + template_name="record-publication.jinja", ) broadcast_notification(notification.dumps()) diff --git a/invenio_config_tuw/users/templates/invenio_notifications/email/group-notification.jinja b/invenio_config_tuw/users/templates/invenio_notifications/email/group-notification.jinja index 2473431..1582454 100644 --- a/invenio_config_tuw/users/templates/invenio_notifications/email/group-notification.jinja +++ b/invenio_config_tuw/users/templates/invenio_notifications/email/group-notification.jinja @@ -36,4 +36,3 @@ Hey, {{ receiver.profile.given_name or receiver.profile.full_name or receiver.us From: {{ config.THEME_SITENAME }} {%- endblock md_body -%} - diff --git a/invenio_config_tuw/users/templates/invenio_notifications/email/record-publication.jinja b/invenio_config_tuw/users/templates/invenio_notifications/email/record-publication.jinja new file mode 100644 index 0000000..d4dc681 --- /dev/null +++ b/invenio_config_tuw/users/templates/invenio_notifications/email/record-publication.jinja @@ -0,0 +1,51 @@ +{%- set context = notification.context -%} +{%- set record = context.record -%} +{%- set record_pid = context.record_pid -%} +{%- set receiver = recipient.data -%} + +{#- Subject line for emails #} +{%- block subject -%} + {{ context.subject }} +{%- endblock subject %} + +{#- HTML body for emails #} +{%- block html_body -%} +<p> + Hey, {{ receiver.profile.given_name or receiver.profile.full_name or receiver.username }}! +</p> +<p> + Your record "{{ record.metadata.title }}" just got published! + <br /> + It is now available under the following {{ record_pid.type }}: <a href="{{ record_pid.url }}">{{ record_pid.url }}</a> +</p> +<p> + Metadata edits for this record will <em>not</em> require another review. +</p> +<p> + From: {{ config.THEME_SITENAME }} +</p> +{%- endblock html_body -%} + +{#- Plaintext body for emails #} +{%- block plain_body -%} +Hey, {{ receiver.profile.given_name or receiver.profile.full_name or receiver.username }}! + +Your record "{{ record.metadata.title }}" just got published! +It is now available under the following {{ record_pid.type }}: {{ record_pid.url }} + +Metadata edits for this record will *not* require another review. + +From: {{ config.THEME_SITENAME }} +{%- endblock plain_body -%} + +{#- Markdown body for chat #} +{%- block md_body -%} +Hey, {{ receiver.profile.given_name or receiver.profile.full_name or receiver.username }}! + +Your record "{{ record.metadata.title }}" just got published! +It is now available under the following {{ record_pid.type }}: [{{ record_pid.url }}]({{ record_pid.url }}) + +Metadata edits for this record will *not* require another review. + +From: {{ config.THEME_SITENAME }} +{%- endblock md_body -%} diff --git a/tests/test_misc.py b/tests/test_misc.py index 3e56618..94d747a 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -32,9 +32,15 @@ def test_send_publication_notification_email(example_record, mocker): "context": { "receiver": {"user": record.parent.access.owned_by.resolve().id}, "subject": f'Your record "{title}" was published', - "message": f'Your record "{title}" just got published!\nIt is now available under the following URL: https://localhost/records/{recid}\n\nMetadata edits for this record will *not* require another review.', - "html_message": f'Your record "{title}" just got published!<br />It is now available under the following URL: <a href="https://localhost/records/{recid}">https://localhost/records/{recid}</a><br /><br />Metadata edits for this record will <em>not</em> require another review.', + "record": dict(record), + "record_pid": { + "type": "URL", + "url": f"https://localhost/records/{recid}", + }, + "template_name": "record-publication.jinja", + "message": None, "plain_message": None, + "html_message": None, "md_message": None, }, } -- GitLab