diff --git a/invenio_rdm_records/templates/semantic-ui/invenio_notifications/grant-user-access.create.jinja b/invenio_rdm_records/templates/semantic-ui/invenio_notifications/grant-user-access.create.jinja index 50974e9cd..b5fe7db36 100644 --- a/invenio_rdm_records/templates/semantic-ui/invenio_notifications/grant-user-access.create.jinja +++ b/invenio_rdm_records/templates/semantic-ui/invenio_notifications/grant-user-access.create.jinja @@ -4,7 +4,12 @@ {% set permission = notification.context.permission %} {% set record_title = record.metadata.title %} -{% set record_link = record.links.self_html %} +{# Determine shared link #} +{%- if not record.is_published and permission == "preview" -%} + {% set shared_link = record.links.record_html + "?preview=1" %} +{%- else -%} + {% set shared_link = record.links.self_html %} +{%- endif -%} {% set account_settings_link = "{ui}/account/settings/notifications".format( ui=config.SITE_UI_URL ) @@ -49,11 +54,11 @@ {% endif %} - {%- if record.is_published -%} - {{ _("View the record")}} - {%- else -%} - {{ _("View the draft")}} - {%- endif -%} + + + {{ _("View the record") if record.is_published else _("View the draft") }} + + _ @@ -64,29 +69,29 @@ {%- endblock html_body -%} +{# + Because of whitespace interpretation for plain text we have to: indent, format and strip jinja blocks (-) + just so to get the right output. This is unfortunate for code readability but required for output. +#} {%- block plain_body -%} - {%- if record.is_published -%} - {{ _("You have now permission to {permission} all versions of the record '{record_title}'.").format(record_title=record_title, permission=permission)}} +{%- if record.is_published -%} + {{ _("You have now permission to {permission} all versions of the record '{record_title}'.").format(record_title=record_title, permission=permission)}} +{%- else -%} + {%- if record_title -%} + {{ _("You have now permission to {permission} the draft '{record_title}'.").format(record_title=record_title, permission=permission)}} {%- else -%} - {%- if record_title -%} - {{ _("You have now permission to {permission} the draft '{record_title}'.").format(record_title=record_title, permission=permission)}} - {%- else -%} - {{ _("You have now permission to {permission} the draft.").format(permission=permission)}} - {%- endif -%} + {{ _("You have now permission to {permission} the draft.").format(permission=permission)}} {%- endif -%} +{%- endif -%} {% if message %} -
-
-{{ _("Message:")}} -{{message}} -{% endif %} -{%- if record.is_published -%} - {{ _("View the record: ") }}{{ record_link }} -{%- else -%} - {{ _("View the draft: ") }}{{ record_link }} -{%- endif -%} +{{ _("Message:") }} + +{{ message }} +{%- endif %} + +{{ _("View the record: ") if record.is_published else _("View the draft: ") }}{{ shared_link }} -{{ _("This is an auto-generated message. To manage notifications, visit your account settings: ")}}{{ account_settings_link }} +{{ _("This is an auto-generated message. To manage notifications, visit your account settings: ")}}{{ account_settings_link }} . {%- endblock plain_body -%} diff --git a/tests/services/test_service_access.py b/tests/services/test_service_access.py index 12382765b..3a78995b6 100644 --- a/tests/services/test_service_access.py +++ b/tests/services/test_service_access.py @@ -678,3 +678,36 @@ def test_delete_grant_by_subject_permissions( # assert that now user can not create a new version with pytest.raises(PermissionDeniedError): records_service.new_version(user_with_grant.identity, id_=record.id) + + +def test_preview_draft_link_in_email( + running_app, uploader, minimal_record, community_owner +): + # services + records_service = current_rdm_records.records_service + access_service = records_service.access + # instances + draft = records_service.create(uploader.identity, minimal_record) + + # community_owner is not used because this has anything to do with + # communities. It is used simply because it is a user with "visibility": "public" + # like it is in test_resources_user_access.py + user_id = str(community_owner.id) + grant_payload = { + "grants": [ + { + "subject": {"type": "user", "id": user_id}, + "permission": "preview", + "notify": True, + } + ] + } + mail = running_app.app.extensions.get("mail") + + with mail.record_messages() as outbox: + # This triggers an email notification because of "notify": True + access_service.bulk_create_grants(uploader.identity, draft.id, grant_payload) + + sent_mail = outbox[0] + assert f"/records/{draft.id}?preview=1" in sent_mail.html + assert f"/records/{draft.id}?preview=1" in sent_mail.body