Skip to content

Commit

Permalink
Remove the need for SITE_URL in request notifications #53
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Feb 20, 2024
1 parent 8de2f7a commit 4b305e9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 2 additions & 0 deletions dje/tests/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions workflow/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
8 changes: 4 additions & 4 deletions workflow/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = (
Expand All @@ -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
Expand All @@ -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 = (
Expand Down
8 changes: 4 additions & 4 deletions workflow/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")

Expand All @@ -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)

Expand Down

0 comments on commit 4b305e9

Please sign in to comment.