From f84e202ade81aabe2fd85bd60eecba3b26898f01 Mon Sep 17 00:00:00 2001 From: Anthony Monthe Date: Mon, 22 Feb 2021 02:13:37 +0000 Subject: [PATCH] Added Signals --- newsletter/models.py | 18 +++++++++----- newsletter/signals.py | 8 +++++++ tests/test_mailing.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 newsletter/signals.py diff --git a/newsletter/models.py b/newsletter/models.py index 65331fed..9267d763 100644 --- a/newsletter/models.py +++ b/newsletter/models.py @@ -19,6 +19,7 @@ from distutils.version import LooseVersion +from . import signals from .fields import DynamicImageField from .utils import ( make_activation_code, get_default_sites, ACTIONS @@ -584,6 +585,7 @@ def submit(self): assert self.publish_date < now(), \ 'Something smells fishy; submission time in future.' + signals.pre_submit.send(sender=self.__class__, message=self) self.sending = True self.save() @@ -600,6 +602,7 @@ def submit(self): finally: self.sending = False self.save() + signals.post_submit.send(sender=self.__class__, message=self) def send_message(self, subscription): variable_dict = { @@ -635,14 +638,14 @@ def send_message(self, subscription): "text/html" ) + signals.pre_send.send(sender=self.__class__, message=self, email=message) + logger.debug( + gettext('Submitting message to: %s.'), + subscription + ) + error = None try: - logger.debug( - gettext('Submitting message to: %s.'), - subscription - ) - message.send() - except Exception as e: # TODO: Test coverage for this branch. logger.error( @@ -651,6 +654,9 @@ def send_message(self, subscription): {'subscription': subscription, 'error': e} ) + error = e + signals.post_send.send(sender=self.__class__, message=self, email=message, + error=error) @classmethod def submit_queue(cls): diff --git a/newsletter/signals.py b/newsletter/signals.py new file mode 100644 index 00000000..adab7e8f --- /dev/null +++ b/newsletter/signals.py @@ -0,0 +1,8 @@ +import django.dispatch + + +pre_submit = django.dispatch.Signal() +post_submit = django.dispatch.Signal() + +pre_send = django.dispatch.Signal() +post_send = django.dispatch.Signal() diff --git a/tests/test_mailing.py b/tests/test_mailing.py index 267cda8b..e936f74b 100644 --- a/tests/test_mailing.py +++ b/tests/test_mailing.py @@ -16,6 +16,7 @@ ) from newsletter.utils import ACTIONS +from newsletter import signals from .utils import MailTestCase, UserTestCase, template_exists NUM_SUBSCRIBED = 2 @@ -490,3 +491,57 @@ def assertSentEmailIsProper(self, action): self.assertEmailAlternativeBodyContains( 'override for %s.html' % action ) + + +class SubmitSignalTestCase(MailingTestCase): + def test_pre_submit(self): + self.called = False + def handler(sender, message, **kwargs): + self.called = True + signals.pre_submit.connect(handler, dispatch_uid='test-pre-submit') + sub = Submission.from_message(self.m) + sub.submit() + self.assertTrue(self.called) + signals.post_send.disconnect(dispatch_uid='test-pre-submit') + + def test_post_submit(self): + self.called = False + def handler(sender, message, **kwargs): + self.called = True + signals.post_submit.connect(handler, dispatch_uid='test-post-submit') + sub = Submission.from_message(self.m) + sub.submit() + self.assertTrue(self.called) + signals.post_send.disconnect(dispatch_uid='test-post-submit') + + def test_pre_send(self): + self.called = 0 + def handler(sender, message, **kwargs): + self.called += 1 + signals.pre_send.connect(handler, dispatch_uid='test-pre-send') + sub = Submission.from_message(self.m) + sub.submit() + self.assertEqual(self.called, 2) + signals.post_send.disconnect(dispatch_uid='test-pre-send') + + def test_post_send(self): + self.called = 0 + def handler(sender, message, **kwargs): + self.called += 1 + signals.post_send.connect(handler, dispatch_uid='test-post-send') + sub = Submission.from_message(self.m) + sub.submit() + self.assertEqual(self.called, 2) + signals.post_send.disconnect(dispatch_uid='test-post-send') + + @mock.patch('django.core.mail.EmailMultiAlternatives.send', side_effect=Exception()) + def test_post_send_failed(self, mock_send): + self.called = 0 + def handler(sender, message, error, **kwargs): + self.assertIsNotNone(error) + self.called += 1 + signals.post_send.connect(handler, dispatch_uid='test-post-send') + sub = Submission.from_message(self.m) + sub.submit() + self.assertEqual(self.called, 2) + signals.post_send.disconnect(dispatch_uid='test-post-send')