Skip to content

Commit

Permalink
access notification: provide correct draft preview link
Browse files Browse the repository at this point in the history
  • Loading branch information
fenekku committed Oct 23, 2024
1 parent 6309328 commit d5c27f8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -49,11 +54,11 @@
{% endif %}
</tr>
<tr>
{%- if record.is_published -%}
<td><a href="{{ record_link }}" class="button">{{ _("View the record")}}</a></td>
{%- else -%}
<td><a href="{{ record_link }}" class="button">{{ _("View the draft")}}</a></td>
{%- endif -%}
<td>
<a href="{{ shared_link }}" class="button">
{{ _("View the record") if record.is_published else _("View the draft") }}
</a>
</td>
</tr>
<tr>
<td><strong>_</strong></td>
Expand All @@ -64,29 +69,29 @@
</table>
{%- 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 %}
<br>
<br>
{{ _("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 -%}
33 changes: 33 additions & 0 deletions tests/services/test_service_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d5c27f8

Please sign in to comment.