Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set preheader #53

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions emark/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ def get_subject(self, **context):
)
return self.subject % context

def get_preheader(self):
"""
Return the email's preheader.

A brief text that recipients will see in their inbox before opening the email
along with the subject. Unless explicitly set, the preheader will be the first
paragraph of the email's body.
Can be useful to grab the recipient's attention and/or simplify their workflow
(e.g. by providing a one-time-password).
"""
return ""

def get_markdown(self, context, utm):
template = self.get_template()
markdown_string = loader.get_template(template).render(context)
Expand Down Expand Up @@ -243,6 +255,7 @@ def render(self, tracking_uuid=None):
context |= utm_params
self.subject = self.get_subject(**context)
context["subject"] = self.subject
context["preheader"] = self.get_preheader()
self.markdown = self.get_markdown(context, utm_params)
self.html = self.get_html(
markdown_string=self.markdown,
Expand Down
2 changes: 1 addition & 1 deletion emark/templates/emark/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<body>
{% block preheader %}
{% spaceless %}
<span class="preheader"></span>
<span class="preheader">{{ preheader }}</span>
{% endspaceless %}
{% endblock preheader %}
<table role="presentation"
Expand Down
19 changes: 19 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class MarkdownEmailTestWithSubject(MarkdownEmailTest):
subject = "Which Donut Are You?"


class MarkdownEmailTestWithPreheader(MarkdownEmailTest):
preheader = "Donuts events are back!"


class TestMarkdownEmail:
@pytest.fixture(autouse=True)
def _add_test_template(self, settings):
Expand Down Expand Up @@ -240,6 +244,21 @@ def test_get_subject(self):
)
assert email_message.subject == "Which Donut Are You?"

def test_get_preheader__missing(self):
msg = emark.message.MarkdownEmail(
template="template.md",
language="en-US",
context={"donut_name": "HoneyNuts", "donut_type": "Honey"},
)
msg.get_preheader() == ""

def test_get_preheader(self):
email_message = MarkdownEmailTestWithPreheader(
language="en-US",
context={"donut_name": "HoneyNuts", "donut_type": "Honey"},
)
assert email_message.preheader == "Donuts events are back!"

def test_set_utm_attributes(self):
email_message = MarkdownEmailTestWithSubject(
language="en-US",
Expand Down