diff --git a/CHANGELOG.txt b/CHANGELOG.txt index ba3a2d8a61..7a10805c89 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -34,6 +34,7 @@ Changelog * Fix: Very long words in page listings are now broken where necessary (Kevin Howbrook) * Fix: Language chosen in user preferences no longer persists on subsequent requests (Bojan Mihelac) * Fix: Prevent new block IDs from being assigned on repeated calls to `StreamBlock.get_prep_value` (Colin Klein) + * Fix: Prevent broken images in notification emails when static files are hosted on a remote domain (Eduard Luca) 2.6.1 (05.08.2019) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 0225806b28..4eb70fdfdb 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -396,6 +396,7 @@ Contributors * Dani Hodovic * Janne Alatalo * Colin Klein +* Eduard Luca Translators =========== diff --git a/docs/releases/2.7.rst b/docs/releases/2.7.rst index 93723d8b4c..e029e227ee 100644 --- a/docs/releases/2.7.rst +++ b/docs/releases/2.7.rst @@ -58,6 +58,7 @@ Bug fixes * Very long words in page listings are now broken where necessary (Kevin Howbrook) * Language chosen in user preferences no longer persists on subsequent requests (Bojan Mihelac) * Prevent new block IDs from being assigned on repeated calls to ``StreamBlock.get_prep_value`` (Colin Klein) + * Prevent broken images in notification emails when static files are hosted on a remote domain (Eduard Luca) Upgrade considerations diff --git a/wagtail/admin/templates/wagtailadmin/notifications/base.html b/wagtail/admin/templates/wagtailadmin/notifications/base.html index 9234257bbd..8b83d98cbb 100644 --- a/wagtail/admin/templates/wagtailadmin/notifications/base.html +++ b/wagtail/admin/templates/wagtailadmin/notifications/base.html @@ -1,4 +1,4 @@ -{% load i18n static %} +{% load i18n wagtailadmin_tags %} <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> @@ -86,7 +86,7 @@ body[yahoo] .text { <table width="600" border="0" align="center" class="mobile" cellspacing="0" cellpadding="0"> <tr> <td> - {% block branding_logo %}<img width="100%" border="0" style="display: block;" alt="" src="{{ settings.BASE_URL }}{% static 'wagtailadmin/images/email-header.jpg' %}" />{% endblock %} + {% block branding_logo %}<img width="100%" border="0" style="display: block;" alt="" src="{% notification_static 'wagtailadmin/images/email-header.jpg' %}" />{% endblock %} </td> </tr> </table> diff --git a/wagtail/admin/templatetags/wagtailadmin_tags.py b/wagtail/admin/templatetags/wagtailadmin_tags.py index 3b6d1ab2ab..c5f63d1a88 100644 --- a/wagtail/admin/templatetags/wagtailadmin_tags.py +++ b/wagtail/admin/templatetags/wagtailadmin_tags.py @@ -1,6 +1,8 @@ import itertools import json +from urllib.parse import urljoin + from django import template from django.conf import settings from django.contrib.admin.utils import quote @@ -480,3 +482,12 @@ def avatar_url(user, size=50): @register.simple_tag def js_translation_strings(): return mark_safe(json.dumps(get_js_translation_strings())) + + +@register.simple_tag +def notification_static(path): + """ + Variant of the {% static %}` tag for use in notification emails - tries to form + a full URL using BASE_URL if the static URL isn't already a full URL. + """ + return urljoin(base_url_setting(), static(path)) diff --git a/wagtail/admin/tests/test_templatetags.py b/wagtail/admin/tests/test_templatetags.py index 265c390510..b80e9ad897 100644 --- a/wagtail/admin/tests/test_templatetags.py +++ b/wagtail/admin/tests/test_templatetags.py @@ -2,7 +2,7 @@ from django.contrib.auth import get_user_model from django.test import TestCase from django.test.utils import override_settings -from wagtail.admin.templatetags.wagtailadmin_tags import avatar_url +from wagtail.admin.templatetags.wagtailadmin_tags import avatar_url, notification_static from wagtail.images.tests.utils import get_test_image_file from wagtail.users.models import UserProfile @@ -42,3 +42,20 @@ class TestAvatarTemplateTag(TestCase): url = avatar_url(self.test_user) self.assertIn('custom-avatar', url) + + +class TestNotificationStaticTemplateTag(TestCase): + @override_settings(STATIC_URL='/static/') + def test_local_notification_static(self): + url = notification_static('wagtailadmin/images/email-header.jpg') + self.assertEqual('/static/wagtailadmin/images/email-header.jpg', url) + + @override_settings(STATIC_URL='/static/', BASE_URL='http://localhost:8000') + def test_local_notification_static_baseurl(self): + url = notification_static('wagtailadmin/images/email-header.jpg') + self.assertEqual('http://localhost:8000/static/wagtailadmin/images/email-header.jpg', url) + + @override_settings(STATIC_URL='https://s3.amazonaws.com/somebucket/static/', BASE_URL='http://localhost:8000') + def test_remote_notification_static(self): + url = notification_static('wagtailadmin/images/email-header.jpg') + self.assertEqual('https://s3.amazonaws.com/somebucket/static/wagtailadmin/images/email-header.jpg', url)