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

Always send email_queued signal when queuing emails #469

Merged
merged 1 commit into from
Jun 12, 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
13 changes: 11 additions & 2 deletions post_office/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ def send_messages(self, email_messages):
email messages sent.
"""
from .mail import create
from .models import STATUS
from .models import STATUS, Email
from .utils import create_attachments
from .signals import email_queued

if not email_messages:
return

default_priority = get_default_priority()
num_sent = 0
emails = []
for email_message in email_messages:
subject = email_message.subject
from_email = email_message.from_email
Expand Down Expand Up @@ -62,8 +65,14 @@ def send_messages(self, email_messages):

email.attachments.add(*attachments)

if get_default_priority() == 'now':
emails.append(email)

if default_priority == 'now':
status = email.dispatch()
if status == STATUS.sent:
num_sent += 1

if default_priority != 'now':
email_queued.send(sender=Email, emails=emails)

return num_sent
3 changes: 2 additions & 1 deletion post_office/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def send(

if priority == PRIORITY.now:
email.dispatch(log_level=log_level)
email_queued.send(sender=Email, emails=[email])
elif commit:
email_queued.send(sender=Email, emails=[email])

return email

Expand Down
17 changes: 17 additions & 0 deletions post_office/tests/test_backends.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from email.mime.image import MIMEImage
from unittest import mock

from django.conf import settings
from django.core.files.images import File
from django.core.mail import EmailMultiAlternatives, send_mail, EmailMessage
Expand All @@ -18,7 +20,7 @@
"""

def send_messages(self, email_messages):
raise Exception('Fake Error')

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django3.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.2

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.8/Django4.1

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.0

Fake Error

Check failure on line 23 in post_office/tests/test_backends.py

View workflow job for this annotation

GitHub Actions / Python3.10/Django4.0

Fake Error


class BackendTest(TestCase):
Expand Down Expand Up @@ -164,3 +166,18 @@
email = Email.objects.latest('id')
self.assertEqual(email.status, STATUS.sent)
self.assertEqual(num_sent, 1)

@override_settings(
EMAIL_BACKEND='post_office.EmailBackend',
POST_OFFICE={
'DEFAULT_PRIORITY': 'medium',
'BACKENDS': {'default': 'django.core.mail.backends.dummy.EmailBackend'}
}
)
@mock.patch('post_office.signals.email_queued.send')
def test_email_queued_signal(self, mock):
# If DEFAULT_PRIORITY is not "now", the email_queued signal should be sent
send_mail('Test', 'Message', '[email protected]', ['[email protected]'])
email = Email.objects.latest('id')
self.assertEqual(email.status, STATUS.queued)
self.assertEqual(mock.call_count, 1)
3 changes: 3 additions & 0 deletions post_office/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from post_office import cache
from .models import Email, PRIORITY, STATUS, EmailTemplate, Attachment
from .settings import get_default_priority
from .signals import email_queued
from .validators import validate_email_with_name


Expand All @@ -28,6 +29,8 @@ def send_mail(subject, message, from_email, recipient_list, html_message='',
if priority == PRIORITY.now:
for email in emails:
email.dispatch()
else:
email_queued.send(sender=Email, emails=emails)
return emails


Expand Down
Loading