From 4b305e9a50159b1d2cfcb00dceb9a203432e45e3 Mon Sep 17 00:00:00 2001 From: tdruez Date: Tue, 20 Feb 2024 14:49:13 +0100 Subject: [PATCH] Remove the need for SITE_URL in request notifications #53 Signed-off-by: tdruez --- dje/tests/test_notification.py | 2 ++ workflow/api.py | 4 ++-- workflow/notification.py | 8 ++++---- workflow/views.py | 8 ++++---- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/dje/tests/test_notification.py b/dje/tests/test_notification.py index c4023ace..cb42ae40 100644 --- a/dje/tests/test_notification.py +++ b/dje/tests/test_notification.py @@ -89,6 +89,7 @@ def test_send_notification_email(self): send_notification_email(request, self.owner, notification.ADDITION) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, f'Added Owner: "{self.owner}"') + self.assertIn(f"http://testserver{self.owner.get_admin_url()}", mail.outbox[0].body) # Sending a notification on a empty string object # Nothing is sent as it's not dataspace related @@ -125,6 +126,7 @@ def test_send_notification_email_on_queryset(self): self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(queryset), 1) self.assertEqual(mail.outbox[0].subject, f'Updated Owner: "{self.owner}"') + self.assertIn(f"http://testserver{self.owner.get_admin_url()}", mail.outbox[0].body) # Using an empty queryset, nothing is sent send_notification_email_on_queryset(request, [], notification.CHANGE) diff --git a/workflow/api.py b/workflow/api.py index 3834cd16..b4c3fdd7 100644 --- a/workflow/api.py +++ b/workflow/api.py @@ -391,11 +391,11 @@ def get_queryset(self): def perform_create(self, serializer): super().perform_create(serializer) - send_request_notification(serializer.instance, created=True) + send_request_notification(self.request, serializer.instance, created=True) def perform_update(self, serializer): super().perform_update(serializer) - send_request_notification(serializer.instance, created=False) + send_request_notification(self.request, serializer.instance, created=False) serializer.instance.events.create( user=self.request.user, text="Request edited.", diff --git a/workflow/notification.py b/workflow/notification.py index a3615fc9..8c117ade 100644 --- a/workflow/notification.py +++ b/workflow/notification.py @@ -27,7 +27,7 @@ def get_recipient_emails(recipients, cc_emails): return list(emails) -def send_request_notification(req, created, extra=None): +def send_request_notification(http_request, req, created, extra=None): """ Send an email notification following a Request creation or edition. An email is sent to the users involved in the Request, based on their @@ -41,7 +41,7 @@ def send_request_notification(req, created, extra=None): "content_object_verbose": content_object_verbose, "action": "submitted" if created else "updated", "action_user": action_user, - "site_url": settings.SITE_URL.rstrip("/"), + "site_url": http_request.build_absolute_uri(location="/").rstrip("/"), } subject = ( @@ -68,7 +68,7 @@ def send_request_notification(req, created, extra=None): ) -def send_request_comment_notification(comment, closed=False): +def send_request_comment_notification(http_request, comment, closed=False): """ Send an email notification following the addition of a comment on a Request. An email is sent to the users involved in the Request except for the @@ -85,7 +85,7 @@ def send_request_comment_notification(comment, closed=False): "req": req, "action": "closed" if closed else "commented", "content_object_verbose": content_object_verbose, - "site_url": settings.SITE_URL.rstrip("/"), + "site_url": http_request.build_absolute_uri(location="/").rstrip("/"), } subject = ( diff --git a/workflow/views.py b/workflow/views.py index 1231f35e..efdc5d77 100644 --- a/workflow/views.py +++ b/workflow/views.py @@ -108,7 +108,7 @@ def request_add_view(request, template_uuid): if instance.is_draft: msg = "Your request was saved as a draft and self-assigned to you." else: - send_request_notification(instance, created=True) + send_request_notification(request, instance, created=True) msg = ( f"Your request was successfully submitted as {instance} with an " f"email notification to the assignee, and a copy to you.\n" @@ -171,7 +171,7 @@ def request_edit_view(request, request_uuid): f"an email notification to the requester and the assignee." ) extra = {"description": f"Updated: {updated_labels}."} - send_request_notification(instance, created=False, extra=extra) + send_request_notification(request, instance, created=False, extra=extra) request_instance.events.create( user=request.user, @@ -253,7 +253,7 @@ def request_details_view(request, request_uuid): event_type=RequestEvent.CLOSED, dataspace=request_instance.dataspace, ) - send_request_comment_notification(event_instance, closed=True) + send_request_comment_notification(request, event_instance, closed=True) messages.success(request, f"Request {request_instance} closed") return redirect("workflow:request_list") @@ -276,7 +276,7 @@ def request_details_view(request, request_uuid): text=comment_content, dataspace=request_instance.dataspace, ) - send_request_comment_notification(comment) + send_request_comment_notification(request, comment) messages.success(request, f"Comment for Request {request_instance} added.") return redirect(request_instance)