From 28a5e85fa99b6748f3f1c9975603487ea99ff99e Mon Sep 17 00:00:00 2001 From: Maximilian Moser <maximilian.moser@tuwien.ac.at> Date: Thu, 20 Mar 2025 09:56:11 +0100 Subject: [PATCH] Use jinja templates for reminder emails --- invenio_config_tuw/curations/tasks.py | 36 +++--------- .../email/acceptance-reminder.jinja | 45 +++++++++++++++ .../email/review-reminder.jinja | 56 +++++++++++++++++++ 3 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 invenio_config_tuw/users/templates/invenio_notifications/email/acceptance-reminder.jinja create mode 100644 invenio_config_tuw/users/templates/invenio_notifications/email/review-reminder.jinja diff --git a/invenio_config_tuw/curations/tasks.py b/invenio_config_tuw/curations/tasks.py index 7956786..e8a7236 100644 --- a/invenio_config_tuw/curations/tasks.py +++ b/invenio_config_tuw/curations/tasks.py @@ -79,8 +79,6 @@ def send_acceptance_reminder_to_uploader(recid: str): draft = records_service.read_draft(identity=system_identity, id_=recid)._obj if (owner := draft.parent.access.owned_by) is None: return - else: - owner = owner.resolve() # NOTE: this requires the UI app, which is the base for the celery app deposit_form_url = url_for( @@ -88,21 +86,13 @@ def send_acceptance_reminder_to_uploader(recid: str): pid_value=draft.pid.pid_value, _external=True, ) - title = draft.metadata["title"] - message = ( - f'Reminder: Your record "{title}" has been reviewed and is ready for publication.\n' - f"You can publish it on the deposit form: {deposit_form_url}" - ) - html_message = ( - f'Reminder: Your record "{title}" has been reviewed and is ready for publication.<br />' - f'You can publish it on the <a href="{deposit_form_url}">deposit form</a>.' - ) notification = UserNotificationBuilder.build( - receiver={"user": owner.id}, + receiver=owner.dumps(), subject="â„¹ï¸ Reminder: Your record is ready for publication!", - message=message, - html_message=html_message, + draft=draft, + deposit_form_url=deposit_form_url, + template_name="acceptance-reminder.jinja", ) broadcast_notification(notification.dumps()) @@ -112,12 +102,10 @@ def send_open_requests_reminder_to_reviewers(request_ids: List[str]): """Send a reminder notification about open curation requests to the reviewers.""" from .notifications import GroupNotificationBuilder - reviewer_role_name = current_app.config["CURATIONS_MODERATION_ROLE"] - plain_lines, html_lines = [], [] + requests = [] for reqid in request_ids: # we assume that only a single request has the same UUID - request = list(requests_service.search(system_identity, q=f"uuid:{reqid}"))[0] - title = request["title"] + request, *_ = list(requests_service.search(system_identity, q=f"uuid:{reqid}")) # NOTE: the reported "self_html" URL is currently broken # (points to "/requests/..." rather than "/me/requests/...") @@ -127,18 +115,12 @@ def send_open_requests_reminder_to_reviewers(request_ids: List[str]): _external=True, ) - plain_lines.append(f'* "{title}": {request_url}') - html_lines.append(f'<li>"<a href="{request_url}">{title}</a>"</li>') + requests.append({"request": dict(request), "request_url": request_url}) - plain_list = "\n".join(plain_lines) - message = f"Reminder: Please review the following requests, they've been waiting for a response for a while:\n{plain_list}" - html_list = "<ul>" + "".join(html_lines) + "</ul>" - html_message = f"<p>Reminder: Please review the following requests, they've been waiting for a response for a while:</p><p>{html_list}</p>" notification = GroupNotificationBuilder.build( - receiver={"group": reviewer_role_name}, + receiver={"group": current_app.config["CURATIONS_MODERATION_ROLE"]}, subject="âš ï¸ Reminder: There are some open curation requests", - message=message, - html_message=html_message, + requests=requests, ) broadcast_notification(notification.dumps()) diff --git a/invenio_config_tuw/users/templates/invenio_notifications/email/acceptance-reminder.jinja b/invenio_config_tuw/users/templates/invenio_notifications/email/acceptance-reminder.jinja new file mode 100644 index 0000000..7ee0870 --- /dev/null +++ b/invenio_config_tuw/users/templates/invenio_notifications/email/acceptance-reminder.jinja @@ -0,0 +1,45 @@ +{%- set context = notification.context -%} +{%- set publisher = context.publisher -%} +{%- set draft = context.draft -%} +{%- set deposit_form_url = context.deposit_form_url -%} +{%- 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> + <strong>Reminder:</strong> + Your record "{{ draft.metadata.title }}" has been reviewed and is ready for publication.<br /> + You can publish it on the <a href="{{ deposit_form_url }}">deposit form</a>. +</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 }}! + +*Reminder:* Your record "{{ draft.metadata.title }}" has been reviewed and is ready for publication. +You can publish it on the deposit form: {{ deposit_form_url }} + +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 }}! + +**Reminder:** Your record "{{ draft.metadata.title }}" has been reviewed and is ready for publication. +You can publish it on the deposit form: [{{ deposit_form_url }}]({{ deposit_form_url }}) + +From: {{ config.THEME_SITENAME }} +{%- endblock md_body -%} diff --git a/invenio_config_tuw/users/templates/invenio_notifications/email/review-reminder.jinja b/invenio_config_tuw/users/templates/invenio_notifications/email/review-reminder.jinja new file mode 100644 index 0000000..cc82e84 --- /dev/null +++ b/invenio_config_tuw/users/templates/invenio_notifications/email/review-reminder.jinja @@ -0,0 +1,56 @@ +{%- set context = notification.context -%} +{%- set publisher = context.publisher -%} +{%- set requests = context.requests -%} +{%- 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> + <strong>Reminder:</strong> + Please review the following requests, they've been waiting for a response for a while: +</p> +<p> + <ul> + {%- for request in requests -%} + <li>"<a href="{{ request.request_url }}">{{ request.request.title }}</a>"</li> + {%- endfor -%} + </ul> +</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 }}! + +*Reminder:* Please review the following requests, they've been waiting for a response for a while:\n{plain_list} + +{%- for request in requests -%} + * "{{ request.request.title }}": {{ request.request_url }} +{%- endfor -%} + +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 }}! + +**Reminder:** Please review the following requests, they've been waiting for a response for a while: + +{%- for request in requests -%} + * "{{ request.request.title }}": {{ request.request_url }} +{%- endfor -%} + +From: {{ config.THEME_SITENAME }} +{%- endblock md_body -%} -- GitLab