Stop emails from breaking when WAGTAILADMIN_BASE_URL is absent

In this case, workflow notification emails can't include the full URL (because they're triggered from a signal, where we don't have access to a request object) but we don't want them to break outright.

fixes 
pull/9055/head
Matt Westcott 2022-06-13 22:43:51 +01:00 zatwierdzone przez LB Johnston
rodzic a1f72d14bd
commit eab32164fd
4 zmienionych plików z 42 dodań i 16 usunięć

Wyświetl plik

@ -228,9 +228,9 @@ def usage_count_enabled():
return getattr(settings, "WAGTAIL_USAGE_COUNT_ENABLED", False)
@register.simple_tag(takes_context=True)
def base_url_setting(context=None):
return get_admin_base_url(context=context)
@register.simple_tag
def base_url_setting():
return get_admin_base_url()
@register.simple_tag

Wyświetl plik

@ -66,6 +66,14 @@ class TestUserPasswordReset(TestCase, WagtailTestUtils):
self.assertEqual(len(mail.outbox), 1)
self.assertIn("mysite.com", mail.outbox[0].body)
@override_settings(ROOT_URLCONF="wagtail.admin.urls", WAGTAILADMIN_BASE_URL=None)
def test_email_without_base_url(self):
response = self.client.post(
reverse("wagtailadmin_password_reset"), {"email": "siteeditor@example.com"}
)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
@unittest.skipUnless(
settings.AUTH_USER_MODEL == "customuser.CustomUser",
"only applicable to CustomUser",

Wyświetl plik

@ -925,6 +925,7 @@ class TestSubmitToWorkflow(TestCase, WagtailTestUtils):
)
self.assertNotContains(response, "Draft")
@override_settings(WAGTAILADMIN_BASE_URL="http://admin.example.com")
def test_submit_sends_mail(self):
self.submit()
# 3 emails sent:
@ -948,6 +949,35 @@ class TestSubmitToWorkflow(TestCase, WagtailTestUtils):
'The page "Hello world! (simple page)" has been submitted for moderation to workflow "test_workflow" by submitter',
workflow_message.body,
)
self.assertIn("http://admin.example.com/admin/", workflow_message.body)
@override_settings(WAGTAILADMIN_BASE_URL=None)
def test_submit_sends_mail_without_base_url(self):
# With a missing WAGTAILADMIN_BASE_URL setting, we won't be able to construct absolute URLs
# for the email, but we don't want it to fail outright either
self.submit()
# 3 emails sent:
# - to moderator - submitted for approval in moderation stage test_task_1
# - to superuser - submitted for approval in moderation stage test_task_1
# - to superuser - submitted to workflow test_workflow
self.assertEqual(len(mail.outbox), 3)
# the 'submitted to workflow' email should include the submitter's name
workflow_message = None
for msg in mail.outbox:
if (
msg.subject
== 'The page "Hello world! (simple page)" has been submitted to workflow "test_workflow"'
):
workflow_message = msg
break
self.assertTrue(workflow_message)
self.assertIn(
'The page "Hello world! (simple page)" has been submitted for moderation to workflow "test_workflow" by submitter',
workflow_message.body,
)
@mock.patch.object(
EmailMultiAlternatives, "send", side_effect=IOError("Server down")

Wyświetl plik

@ -5,12 +5,10 @@ from django.conf import settings
from wagtail.utils.deprecation import RemovedInWagtail50Warning
def get_admin_base_url(context=None):
def get_admin_base_url():
"""
Gets the base URL for the wagtail admin site. This is set in `settings.WAGTAILADMIN_BASE_URL`,
which was previously `settings.BASE_URL`.
If setting is omitted and this is called in a request context, falls back to
`request.site.root_url` or next the host_name of the request.
"""
admin_base_url = getattr(settings, "WAGTAILADMIN_BASE_URL", None)
@ -21,14 +19,4 @@ def get_admin_base_url(context=None):
)
admin_base_url = settings.BASE_URL
if admin_base_url is None and context is not None:
request = context["request"]
admin_base_url = getattr(request.site, "root_url", None)
if admin_base_url is None:
admin_base_url = request.get_host()
secure_prefix = "http"
if request.is_secure():
secure_prefix = "https"
admin_base_url = secure_prefix + "://" + admin_base_url
return admin_base_url