Skip to content

Commit

Permalink
Allow custom subject delimiter (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-teichmeister authored Sep 11, 2024
1 parent b70c22a commit 5792ba8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion django_pony_express/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class BaseEmailService:
"""

SUBJECT_PREFIX = None
SUBJECT_DELIMITER = " - "
FROM_EMAIL = None
REPLY_TO_ADDRESS = []

Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion docs/features/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/features/internal_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()``
Expand Down
13 changes: 13 additions & 0 deletions tests/services/base/test_base_mail_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]")
Expand Down

0 comments on commit 5792ba8

Please sign in to comment.