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

Added Signals #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions newsletter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter shouldn't be called message if the object passed isn't a message but the submission object.

Suggested change
signals.pre_submit.send(sender=self.__class__, message=self)
signals.pre_submit.send(sender=self.__class__, submission=self)

self.sending = True
self.save()

Expand All @@ -600,6 +602,7 @@ def submit(self):
finally:
self.sending = False
self.save()
signals.post_submit.send(sender=self.__class__, message=self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
signals.post_submit.send(sender=self.__class__, message=self)
signals.post_submit.send(sender=self.__class__, submission=self)


def send_message(self, subscription):
variable_dict = {
Expand Down Expand Up @@ -635,14 +638,14 @@ def send_message(self, subscription):
"text/html"
)

signals.pre_send.send(sender=self.__class__, message=self, email=message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
signals.pre_send.send(sender=self.__class__, message=self, email=message)
signals.pre_send.send(sender=self.__class__, submission=self, message=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(
Expand All @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
signals.post_send.send(sender=self.__class__, message=self, email=message,
signals.post_send.send(sender=self.__class__, submission=self, message=message,

error=error)

@classmethod
def submit_queue(cls):
Expand Down
8 changes: 8 additions & 0 deletions newsletter/signals.py
Original file line number Diff line number Diff line change
@@ -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()
55 changes: 55 additions & 0 deletions tests/test_mailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from newsletter.utils import ACTIONS

from newsletter import signals
from .utils import MailTestCase, UserTestCase, template_exists

NUM_SUBSCRIBED = 2
Expand Down Expand Up @@ -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')