diff --git a/opentech/apply/activity/messaging.py b/opentech/apply/activity/messaging.py index 55584d996f5f59d700e5bfb7d5e0885498666ce9..873de30adfd76ef66d35d5bd873b8a7bdc60d21b 100644 --- a/opentech/apply/activity/messaging.py +++ b/opentech/apply/activity/messaging.py @@ -20,6 +20,7 @@ neat_related = { MESSAGES.TRANSITION: 'old_phase', MESSAGES.APPLICANT_EDIT: 'revision', MESSAGES.EDIT: 'revision', + MESSAGES.COMMENT: 'comment', } @@ -57,20 +58,22 @@ class AdapterBase: # Message type doesn't expect a related object if related: raise ValueError(f"Unexpected 'related' kwarg provided for {message_type}") from None - - if not related: - raise ValueError(f"{message_type} expects a 'related' kwarg") - return {neat_name: related} + return {} + else: + if not related: + raise ValueError(f"{message_type} expects a 'related' kwarg") + return {neat_name: related} def recipients(self, message_type, **kwargs): raise NotImplementedError() - def process(self, message_type, event, request, user, submission, related=None): + def process(self, message_type, event, request, user, submission, related=None, **kwargs): kwargs = { 'request': request, 'user': user, 'submission': submission, 'related': related, + **kwargs, } kwargs.update(self.get_neat_related(message_type, related)) kwargs.update(self.extra_kwargs(message_type, **kwargs)) @@ -301,14 +304,14 @@ class MessengerBackend: def __init__(self, *adpaters): self.adapters = adpaters - def __call__(self, message_type, request, user, submission, **kwargs): - return self.send(message_type, request=request, user=user, submission=submission, **kwargs) + def __call__(self, message_type, request, user, submission, related=None, **kwargs): + return self.send(message_type, request=request, user=user, submission=submission, related=related, **kwargs) - def send(self, message_type, request, user, submission, related): + def send(self, message_type, request, user, submission, related, **kwargs): from .models import Event event = Event.objects.create(type=message_type.name, by=user, submission=submission) for adapter in self.adapters: - adapter.process(message_type, event, request=request, user=user, submission=submission, related=related) + adapter.process(message_type, event, request=request, user=user, submission=submission, related=related, **kwargs) adapters = [ diff --git a/opentech/apply/activity/tests/test_messaging.py b/opentech/apply/activity/tests/test_messaging.py index 77b3ca5924aa1f91523f6d970eb6301711355019..54576e91c79a8a363f03cb2ca41e38f0d260a1dc 100644 --- a/opentech/apply/activity/tests/test_messaging.py +++ b/opentech/apply/activity/tests/test_messaging.py @@ -19,6 +19,7 @@ from ..messaging import ( ActivityAdapter, EmailAdapter, MessengerBackend, + neat_related, MESSAGES, SlackAdapter, ) @@ -46,18 +47,22 @@ class TestAdapter(AdapterBase): class AdapterMixin: adapter = None - def process_kwargs(self, **kwargs): + def process_kwargs(self, message_type, **kwargs): if 'user' not in kwargs: kwargs['user'] = UserFactory() if 'submission' not in kwargs: kwargs['submission'] = ApplicationSubmissionFactory() if 'request' not in kwargs: kwargs['request'] = make_request() + if message_type in neat_related: + kwargs['related'] = kwargs.get('related', 'a thing') + else: + kwargs['related'] = None return kwargs def adapter_process(self, message_type, **kwargs): - kwargs = self.process_kwargs(**kwargs) + kwargs = self.process_kwargs(message_type, **kwargs) self.adapter.process(message_type, event=EventFactory(submission=kwargs['submission']), **kwargs) @@ -139,6 +144,7 @@ class TestMessageBackend(TestCase): self.mocked_adapter = Mock(AdapterBase) self.backend = MessengerBackend self.kwargs = { + 'related': None, 'request': None, 'user': UserFactory(), 'submission': ApplicationSubmissionFactory(), @@ -185,7 +191,7 @@ class TestActivityAdapter(TestCase): user = UserFactory() submission = ApplicationSubmissionFactory() - self.adapter.send_message(message, user=user, submission=submission) + self.adapter.send_message(message, user=user, submission=submission, related=None) self.assertEqual(Activity.objects.count(), 1) activity = Activity.objects.first() @@ -318,14 +324,14 @@ class TestEmailAdapter(AdapterMixin, TestCase): def test_no_email_private_comment(self): comment = CommentFactory(internal=True) - self.adapter_process(MESSAGES.COMMENT, comment=comment, submission=comment.submission) + self.adapter_process(MESSAGES.COMMENT, related=comment, submission=comment.submission) self.assertEqual(len(mail.outbox), 0) def test_no_email_own_comment(self): application = ApplicationSubmissionFactory() comment = CommentFactory(user=application.user, submission=application) - self.adapter_process(MESSAGES.COMMENT, comment=comment, user=comment.user, submission=comment.submission) + self.adapter_process(MESSAGES.COMMENT, related=comment, user=comment.user, submission=comment.submission) self.assertEqual(len(mail.outbox), 0) def test_reviewers_email(self): diff --git a/opentech/apply/activity/views.py b/opentech/apply/activity/views.py index 6a801ea77dcf9569468abb1b4946fd293a42a27c..07ba2d1dcb64df46393c08315b44aaf410260ad0 100644 --- a/opentech/apply/activity/views.py +++ b/opentech/apply/activity/views.py @@ -61,7 +61,7 @@ class CommentFormView(DelegatedViewMixin, CreateView): request=self.request, user=self.request.user, submission=self.object.submission, - comment=self.object, + related=self.object, ) return response