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

Move logic to render HTML and text content to dedicated methods, fixing #21. #22

Merged
merged 4 commits into from
Nov 15, 2024
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
25 changes: 15 additions & 10 deletions django_pony_express/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ def _add_attachments(self, msg: EmailMultiAlternatives):

return msg

def _generate_html_content(self, mail_attributes: dict) -> str:
return render_to_string(self.template_name, mail_attributes)

def _generate_text_content(self, mail_attributes: dict, html_content: str) -> str:
# Render TXT body part if a template is explicitly set, otherwise convert HTML template to plain text
if not self.template_txt_name:
h = html2text.HTML2Text()
# Set body width to "infinite" to avoid weird line breaks
h.body_width = 0
return h.handle(html_content)
else:
return render_to_string(self.template_txt_name, mail_attributes)

def _build_mail_object(self) -> EmailMultiAlternatives:
"""
This method creates a mail object. It collects the required variables, sets the subject and makes sure that
Expand All @@ -245,16 +258,8 @@ def _build_mail_object(self) -> EmailMultiAlternatives:
mail_attributes = self.get_context_data()

# Render HTML body content
html_content = render_to_string(self.template_name, mail_attributes)

# Render TXT body part if a template is explicitly set, otherwise convert HTML template to plain text
if not self.template_txt_name:
h = html2text.HTML2Text()
# Set body width to "infinite" to avoid weird line breaks
h.body_width = 0
text_content = h.handle(html_content)
else:
text_content = render_to_string(self.template_txt_name, mail_attributes)
html_content = self._generate_html_content(mail_attributes)
text_content = self._generate_text_content(mail_attributes, html_content)

# Build mail object
msg = EmailMultiAlternatives(
Expand Down
37 changes: 37 additions & 0 deletions tests/services/base/test_base_mail_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,40 @@ def test_process_with_error(self):
def test_process_is_valid_invalid(self, *args):
factory = BaseEmailService()
self.assertEqual(factory.process(), 0)

def test_html_templates_rendering(self):
my_var = "Lorem ipsum dolor!"
service = BaseEmailService()
service.template_name = "testapp/test_email.html"
msg_html = service._generate_html_content({"my_var": my_var})

# Assertions
self.assertIsInstance(msg_html, str)

self.assertIn("Lorem ipsum dolor", msg_html)
self.assertNotIn("I am a different content", msg_html)

def test_text_templates_rendering(self):
my_var = "Lorem ipsum dolor!"
service = BaseEmailService()
service.template_txt_name = "testapp/test_email.txt"
msg_html = service._generate_text_content({"my_var": my_var}, "")

# Assertions
self.assertIsInstance(msg_html, str)

self.assertIn("Lorem ipsum dolor", msg_html)
self.assertIn("I am a different content", msg_html)
self.assertNotIn("Current date test", msg_html)

def test_text_templates_rendering_fallback(self):
my_var = "Lorem ipsum dolor!"
service = BaseEmailService()
msg_html = service._generate_text_content({"my_var": my_var}, "Lorem ipsum dolor")

# Assertions
self.assertIsInstance(msg_html, str)

self.assertIn("Lorem ipsum dolor", msg_html)
self.assertNotIn("I am a different content", msg_html)
self.assertNotIn("Current date test", msg_html)