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

v2.5.0 #24

Merged
merged 1 commit into from
Dec 3, 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
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=["dummy@example.com"], 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, ["dummy@example.com"])

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