Skip to content

Commit

Permalink
Move logic to render HTML and text content to dedicated methods, fixing
Browse files Browse the repository at this point in the history
#21. (#22)

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

* `_generate_html_content()` and `_generate_text_content()` to protected methods.

* added tests fo `_generate_html_content()` and `_generate_text_content()`.

* linting

---------

Co-authored-by: Willem Van Onsem <[email protected]>
  • Loading branch information
KommuSoft and Willem Van Onsem authored Nov 15, 2024
1 parent cb0aeb6 commit 8c93bda
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
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)

0 comments on commit 8c93bda

Please sign in to comment.