Skip to content

Commit

Permalink
Fixed broken email header when hosting static files remotely (#5543)
Browse files Browse the repository at this point in the history
Fixes #5525
  • Loading branch information
edu2004eu authored and gasman committed Sep 6, 2019
1 parent 0938c8e commit b9cfc24
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ Contributors
* Dani Hodovic
* Janne Alatalo
* Colin Klein
* Eduard Luca

Translators
===========
Expand Down
1 change: 1 addition & 0 deletions docs/releases/2.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions wagtail/admin/templates/wagtailadmin/notifications/base.html
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -86,7 +86,7 @@
<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>
Expand Down
11 changes: 11 additions & 0 deletions wagtail/admin/templatetags/wagtailadmin_tags.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
19 changes: 18 additions & 1 deletion wagtail/admin/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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

Expand Down Expand Up @@ -42,3 +42,20 @@ def test_uploaded_avatar(self):

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)

0 comments on commit b9cfc24

Please sign in to comment.