Skip to content

Commit

Permalink
v2.5.0 (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: sk-rama
  • Loading branch information
GitRon authored Dec 3, 2024
1 parent 7291cb1 commit 5664838
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

* *2.5.0* (2024-12-03)
* Added connection param to `BaseEmailService` (Thx to @sk-rama)

* *2.4.3* (2024-11-15)
* Move logic to render HTML and text content to dedicated methods

Expand Down
2 changes: 1 addition & 1 deletion django_pony_express/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Class-based emails including a test suite for Django"""

__version__ = "2.4.3"
__version__ = "2.5.0"
5 changes: 5 additions & 0 deletions django_pony_express/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import html2text
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.core.mail.backends.base import BaseEmailBackend
from django.db.models import QuerySet
from django.template.loader import render_to_string
from django.utils import translation
Expand Down Expand Up @@ -123,12 +124,14 @@ class BaseEmailService:
cc_email_list = []
bcc_email_list = []
attachment_list = []
connection = None

def __init__(
self,
recipient_email_list: Optional[Union[list, tuple, str]] = None,
context_data: Optional[dict] = None,
attachment_list: Optional[list] = None,
connection: BaseEmailBackend = None,
**kwargs,
) -> None:
"""
Expand All @@ -148,6 +151,7 @@ def __init__(
self.recipient_email_list = recipient_email_list if recipient_email_list else []
self.context_data = context_data if context_data else {}
self.attachment_list = attachment_list if attachment_list else []
self.connection = connection

def _get_logger(self) -> logging.Logger:
self._logger = logging.getLogger(PONY_LOGGER_NAME) if self._logger is None else self._logger
Expand Down Expand Up @@ -270,6 +274,7 @@ def _build_mail_object(self) -> EmailMultiAlternatives:
bcc=self.get_bcc_emails(),
reply_to=self.get_reply_to_emails(),
to=self.recipient_email_list,
connection=self.connection,
)
msg.attach_alternative(html_content, "text/html")

Expand Down
17 changes: 17 additions & 0 deletions tests/services/base/test_base_mail_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import mock

from django.conf import settings
from django.core import mail
from django.core.mail import EmailMultiAlternatives
from django.test import TestCase, override_settings
from django.utils import translation
Expand All @@ -27,6 +28,22 @@ def test_init_recipient_and_context_are_initialised_empty(self):
self.assertEqual(service.recipient_email_list, [])
self.assertEqual(service.context_data, {})

def test_init_connection_regular(self):
connection = mail.get_connection()

service = BaseEmailService(recipient_email_list=["[email protected]"], connection=connection)

service.subject = "My subject"
service.template_name = "testapp/test_email.html"

result = service.process()
self.assertIs(result, 1)

# Assert email was "sent"
self.assertGreater(len(mail.outbox), 0)
self.assertEqual(mail.outbox[0].subject, "My subject")
self.assertEqual(mail.outbox[0].to, ["[email protected]"])

def test_get_logger_logger_not_set(self):
service = BaseEmailService()
email_logger = service._get_logger()
Expand Down

0 comments on commit 5664838

Please sign in to comment.