def _send_staffing_request_by_mail(self, email, message): html_message = html_from_plaintext(message) # Slack does not accept html tags, so we want to let markdown add some # simple things like <p> send_mail('A new task is available for you', message, settings.ORCHESTRA_NOTIFICATIONS_FROM_EMAIL, [email], html_message=html_message)
def test_filtering_no_send(self, mock_mail): # Test when comm_type is not allowed comm_pref = CommunicationPreference.objects.get( worker=self.worker, communication_type=self.comm_type) comm_pref.methods.email = ~CommunicationPreference.methods.email comm_pref.save() self.assertFalse(comm_pref.can_email()) send_mail(subject='test_subject', message='test_message', from_email='*****@*****.**', recipient_list=self.recipient_list, communication_type=self.comm_type) self.assertFalse(mock_mail.called)
def test_filtering_send_with_preference(self, mock_mail): # Test when comm_type is allowed send_mail(subject='test_subject', message='test_message', from_email='*****@*****.**', recipient_list=self.recipient_list, communication_type=self.comm_type ) mock_mail.assert_called_once_with( 'test_subject', 'test_message', '*****@*****.**', self.recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None )
def test_filtering_send_with_preference(self, mock_mail): # Test when comm_type is allowed send_mail(subject='test_subject', message='test_message', from_email='*****@*****.**', recipient_list=self.recipient_list, communication_type=self.comm_type) mock_mail.assert_called_once_with('test_subject', 'test_message', '*****@*****.**', self.recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None)
def test_filtering_no_send(self, mock_mail): # Test when comm_type is not allowed comm_pref = CommunicationPreference.objects.get( worker=self.worker, communication_type=self.comm_type ) comm_pref.methods.email = ~CommunicationPreference.methods.email comm_pref.save() self.assertFalse(comm_pref.can_email()) send_mail(subject='test_subject', message='test_message', from_email='*****@*****.**', recipient_list=self.recipient_list, communication_type=self.comm_type ) self.assertFalse(mock_mail.called)
def test_email_mocked(self, mock_mail): """ Verify that we mock email sending. """ # Test when no comm_type is given send_mail(subject='test_subject', message='test_message', from_email='*****@*****.**', recipient_list=self.recipient_list, ) mock_mail.assert_called_once_with( 'test_subject', 'test_message', '*****@*****.**', [settings.ORCHESTRA_MOCK_TO_EMAIL], fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None )
def test_filtering_no_preference(self, mock_mail): """ Verify that we correctly filter users based on their email preferences. """ # Test when no comm_type is given send_mail(subject='test_subject', message='test_message', from_email='*****@*****.**', recipient_list=self.recipient_list ) mock_mail.assert_called_once_with( 'test_subject', 'test_message', '*****@*****.**', self.recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None )
def test_filtering_no_preference(self, mock_mail): """ Verify that we correctly filter users based on their email preferences. """ # Test when no comm_type is given send_mail(subject='test_subject', message='test_message', from_email='*****@*****.**', recipient_list=self.recipient_list) mock_mail.assert_called_once_with('test_subject', 'test_message', '*****@*****.**', self.recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None)
def test_email_mocked(self, mock_mail): """ Verify that we mock email sending. """ # Test when no comm_type is given send_mail( subject='test_subject', message='test_message', from_email='*****@*****.**', recipient_list=self.recipient_list, ) mock_mail.assert_called_once_with('test_subject', 'test_message', '*****@*****.**', [settings.ORCHESTRA_MOCK_TO_EMAIL], fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None)
def notify_status_change(task, previous_status=None): """ Notify workers after task has changed state """ task_assignments = assignment_history(task) current_task_assignment = current_assignment(task) current_worker = None if current_task_assignment: current_worker = current_task_assignment.worker message_info = None # Notify worker when task initially picked up if task.status == Task.Status.PROCESSING: message_info = { 'subject': "You've been assigned to a new task!", 'message': ("You've been assigned to a new task. We can't wait " "to see the great things you'll do!"), 'recipient_list': [current_worker.user.email] } # Notify worker when assignment selected for review elif task.status == Task.Status.PENDING_REVIEW: message_info = { 'subject': 'Your task is under review!', 'message': ('Thanks for all your hard work, {}! The following ' 'task was randomly selected for review by another ' 'expert; you should hear back soon!').format( current_worker.user.username), 'recipient_list': [current_worker.user.email] } # Notify worker when assignment rejected elif task.status == Task.Status.POST_REVIEW_PROCESSING: message_info = { 'subject': 'Your task has been returned', 'message': ('Your reviewer sent back your task for a bit more ' 'polish. Check out the feedback as soon as you can!'), 'recipient_list': [current_worker.user.email] } # Notify all workers on a task when it has been completed elif task.status == Task.Status.COMPLETE: message_info = { 'subject': 'Task complete!', 'message': 'Congratulations! The task you worked on is complete.', 'recipient_list': [ assignment.worker.user.email for assignment in task_assignments if assignment.worker and assignment.worker.user.email ] } # Notify reviewer when task pending update is ready for re-review, but not # for a task moving from PENDING_REVIEW to REVIEWING elif (task.status == Task.Status.REVIEWING and previous_status == Task.Status.POST_REVIEW_PROCESSING): message_info = { 'subject': 'A task is ready for re-review!', 'message': ('A task has been updated and is ready for ' 're-review!'), 'recipient_list': [current_worker.user.email] } # Notify all workers on a task when it has been aborted elif task.status == Task.Status.ABORTED: message_info = { 'subject': 'A task you were working on has been ended', 'message': ('Unfortunately, the task you were working on has ' 'been ended. Please reach out to us if you think this ' 'has been done in error.'), 'recipient_list': [ assignment.worker.user.email for assignment in task_assignments if assignment.worker and assignment.worker.user.email ] } _notify_internal_slack_status_change(task, current_worker) if task.project.slack_group_id: _notify_experts_slack_status_change(task, current_worker) if message_info is not None: message_info['message'] += _task_information(task) comm_type = ( CommunicationPreference.CommunicationType.TASK_STATUS_CHANGE.value) send_mail(from_email=settings.ORCHESTRA_NOTIFICATIONS_FROM_EMAIL, communication_type=comm_type, fail_silently=True, **message_info)
def notify_status_change(task, previous_status=None): """ Notify workers after task has changed state """ task_assignments = assignment_history(task) current_task_assignment = current_assignment(task) current_worker = None if current_task_assignment: current_worker = current_task_assignment.worker message_info = None # Notify worker when task initially picked up if task.status == Task.Status.PROCESSING: message_info = { 'subject': "You've been assigned to a new task!", 'message': ("You've been assigned to a new task. We can't wait " "to see the great things you'll do!"), 'recipient_list': [current_worker.user.email] } # Notify worker when assignment selected for review elif task.status == Task.Status.PENDING_REVIEW: message_info = { 'subject': 'Your task is under review!', 'message': ('Thanks for all your hard work, {}! The following ' 'task was randomly selected for review by another ' 'expert; you should hear back soon!').format( current_worker.user.username), 'recipient_list': [current_worker.user.email] } # Notify worker when assignment rejected elif task.status == Task.Status.POST_REVIEW_PROCESSING: message_info = { 'subject': 'Your task has been returned', 'message': ('Your reviewer sent back your task for a bit more ' 'polish. Check out the feedback as soon as you can!'), 'recipient_list': [current_worker.user.email] } # Notify all workers on a task when it has been completed elif task.status == Task.Status.COMPLETE: message_info = { 'subject': 'Task complete!', 'message': 'Congratulations! The task you worked on is complete.', 'recipient_list': [assignment.worker.user.email for assignment in task_assignments if assignment.worker and assignment.worker.user.email] } # Notify reviewer when task pending update is ready for re-review, but not # for a task moving from PENDING_REVIEW to REVIEWING elif (task.status == Task.Status.REVIEWING and previous_status == Task.Status.POST_REVIEW_PROCESSING): message_info = { 'subject': 'A task is ready for re-review!', 'message': ('A task has been updated and is ready for ' 're-review!'), 'recipient_list': [current_worker.user.email] } # Notify all workers on a task when it has been aborted elif task.status == Task.Status.ABORTED: message_info = { 'subject': 'A task you were working on has been ended', 'message': ('Unfortunately, the task you were working on has ' 'been ended. Please reach out to us if you think this ' 'has been done in error.'), 'recipient_list': [assignment.worker.user.email for assignment in task_assignments if assignment.worker and assignment.worker.user.email] } _notify_internal_slack_status_change(task, current_worker) if task.project.slack_group_id: _notify_experts_slack_status_change(task, current_worker) if message_info is not None: message_info['message'] += _task_information(task) comm_type = (CommunicationPreference.CommunicationType .TASK_STATUS_CHANGE.value) send_mail(from_email=settings.ORCHESTRA_NOTIFICATIONS_FROM_EMAIL, communication_type=comm_type, fail_silently=True, **message_info)