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

Bugfix: Deactivate translation after build mail object #26

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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.1* (2025-02-19)
* Fixed a bug, where translations were not deactivated after sending an email

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

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.5.0"
__version__ = "2.5.1"
3 changes: 3 additions & 0 deletions django_pony_express/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ def _build_mail_object(self) -> EmailMultiAlternatives:
# Add attachments (if available)
msg = self._add_attachments(msg)

# Deactivate translation
translation.deactivate()

# Return mail object
return msg

Expand Down
16 changes: 16 additions & 0 deletions tests/services/base/test_base_mail_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ def test_build_mail_object_translation_works(self):
self.assertIn("vrijdag", msg_obj.body)
self.assertIn("vrijdag", msg_obj.alternatives[0][0])

@freeze_time("2020-06-26")
@override_settings(LANGUAGE_CODE="de")
@mock.patch.object(BaseEmailService, "get_translation", return_value="nl-BE")
def test_build_mail_object_deactivates_language_afterwards(self, *args):
service = BaseEmailService(recipient_email_list="[email protected]")
service.template_name = "testapp/test_email.html"
msg_obj = service._build_mail_object()

# Assertions
# Assert, that email was rendered in nl-BE language
self.assertIn("vrijdag", msg_obj.body)
self.assertIn("vrijdag", msg_obj.alternatives[0][0])

# Assert, system language is back to "de"
self.assertEqual(settings.LANGUAGE_CODE, "de")

def test_is_valid_positive_case(self):
email = "[email protected]"
subject = "Test email"
Expand Down