From 5792ba8193adbccdfde8f7f9228ac553fbd33bea Mon Sep 17 00:00:00 2001 From: Christoph Teichmeister <48754645+christoph-teichmeister@users.noreply.github.com> Date: Wed, 11 Sep 2024 15:35:15 +0200 Subject: [PATCH] Allow custom subject delimiter (#19) --- django_pony_express/services/base.py | 3 ++- docs/features/configuration.md | 3 ++- docs/features/internal_api.md | 2 +- tests/services/base/test_base_mail_service.py | 13 +++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/django_pony_express/services/base.py b/django_pony_express/services/base.py index 2bc51e5..410fdcb 100644 --- a/django_pony_express/services/base.py +++ b/django_pony_express/services/base.py @@ -109,6 +109,7 @@ class BaseEmailService: """ SUBJECT_PREFIX = None + SUBJECT_DELIMITER = " - " FROM_EMAIL = None REPLY_TO_ADDRESS = [] @@ -165,7 +166,7 @@ def get_subject(self) -> str: emails. Can be overridden if required. """ if self.SUBJECT_PREFIX: - return f"{self.SUBJECT_PREFIX} - {self.subject}" + return f"{self.SUBJECT_PREFIX}{self.SUBJECT_DELIMITER}{self.subject}" return self.subject def get_from_email(self) -> str: diff --git a/docs/features/configuration.md b/docs/features/configuration.md index f91c89f..bbaa35b 100644 --- a/docs/features/configuration.md +++ b/docs/features/configuration.md @@ -2,7 +2,8 @@ ## Default "FROM" -You can set a subject prefix, so that all your emails look more similar when setting the constant ``SUBJECT_PREFIX``. +You can set a subject prefix, so that all your emails look more similar when setting the constant ``SUBJECT_PREFIX``.\ +Additionally, you can define a custom `SUBJECT_DELIMITER`, which will be added between your custom `SUBJECT_PREFIX` and your `subject`. If you wish to define a custom "from" email, you can do so via the ``FROM_EMAIL`` constant. Take care: If you do not set it, the ``DEFAULT_FROM_EMAIL`` variable from the django settings is used. diff --git a/docs/features/internal_api.md b/docs/features/internal_api.md index eedd28d..8dc775c 100644 --- a/docs/features/internal_api.md +++ b/docs/features/internal_api.md @@ -10,7 +10,7 @@ * ``get_subject()`` - This method combines the constant ``SUBJECT_PREFIX`` with the variable `subject`. Can be overwritten, if required. + This method combines the constants ``SUBJECT_PREFIX`` and ``SUBJECT_DELIMITER`` with the variable `subject`. Can be overwritten, if required. * ``get_from_email()`` diff --git a/tests/services/base/test_base_mail_service.py b/tests/services/base/test_base_mail_service.py index 92ede56..01d279e 100644 --- a/tests/services/base/test_base_mail_service.py +++ b/tests/services/base/test_base_mail_service.py @@ -56,6 +56,19 @@ def test_get_subject_with_prefix(self): service.SUBJECT_PREFIX = prefix service.subject = subject self.assertIn(prefix, service.get_subject()) + self.assertIn(service.SUBJECT_DELIMITER, service.get_subject()) + self.assertIn(subject, service.get_subject()) + + def test_get_subject_with_prefix_and_custom_delimiter(self): + prefix = "Pony Express" + custom_delimiter = " | " + subject = "I am a subject!" + service = BaseEmailService() + service.SUBJECT_PREFIX = prefix + service.SUBJECT_DELIMITER = custom_delimiter + service.subject = subject + self.assertIn(prefix, service.get_subject()) + self.assertIn(custom_delimiter, service.get_subject()) self.assertIn(subject, service.get_subject()) @override_settings(DEFAULT_FROM_EMAIL="noreply@example.com")