def expand_entries_for_channels(self, pending_notification, recipients=[]): """ Expands a PendingNotification to a list of Notification(s) given some recipients. For each recipient, it honors their unsubscription wishes/settings and the channel's own definitions of which actions it can handle. pending_notification: An instance of PendingNotification. recipients: A list of User(s). """ from canvas.notifications.notification_models import Notification actor = pending_notification.actor action = pending_notification.action notifications = [] # Get all the possible channels channels = channel_map.values() for channel_class in channels: # Does this channel know how to handle this type of notification when the user is an actor? if channel_class.enabled_for_actor_action(action, actor, pending_notification): # Note that the "recipient" here is the actor! entry = Notification.from_pending_notification(pending_notification, actor, channel_name=channel_class.__name__) notifications.append(entry) for recipient in recipients: if channel_class.enabled_for_recipient_action(action, recipient, pending_notification): entry = Notification.from_pending_notification(pending_notification, recipient, channel_name=channel_class.__name__) notifications.append(entry) return notifications
def test_24h_digest_email(self): user = create_user(email="*****@*****.**") pn = Actions.digest(user) notification = Notification.from_pending_notification(pn, user, "EmailChannel") email_message = EmailChannel().make_message(notification, force=True) message = EmailChannel().make_email_backend_message(email_message) email_message.record_sent(notification.action)
def test_Notification_from_pn(self): pn = Actions.replied(create_user(), create_comment()) notification = Notification.from_pending_notification(pn, create_user, "EmailChannel") assert notification.recipient for key in pn.data: self.assertEqual(getattr(notification, key), getattr(pn, key)) assert pn.comment assert notification.comment
def expand_entries_for_channels(self, pending_notification, recipients=[]): """ Expands a PendingNotification to a list of Notification(s) given some recipients. For each recipient, it honors their unsubscription wishes/settings and the channel's own definitions of which actions it can handle. pending_notification: An instance of PendingNotification. recipients: A list of User(s). """ from canvas.notifications.notification_models import Notification actor = pending_notification.actor action = pending_notification.action notifications = [] # Get all the possible channels channels = channel_map.values() for channel_class in channels: # Does this channel know how to handle this type of notification when the user is an actor? if channel_class.enabled_for_actor_action(action, actor, pending_notification): # Note that the "recipient" here is the actor! entry = Notification.from_pending_notification( pending_notification, actor, channel_name=channel_class.__name__) notifications.append(entry) for recipient in recipients: if channel_class.enabled_for_recipient_action( action, recipient, pending_notification): entry = Notification.from_pending_notification( pending_notification, recipient, channel_name=channel_class.__name__) notifications.append(entry) return notifications
def test_delivering_email_records_email_sent_metric(self): with override_service('experiment_placer', FakeExperimentPlacer, kwargs={'email_notifications': 'experimental'}): user = create_user(email="*****@*****.**") comment = create_comment() comment2 = create_comment(author=user, replied_comment=comment) pn = Actions.replied(user, comment2) notification = Notification.from_pending_notification(pn, user, "EmailChannel") channel = EmailChannel() with override_service('metrics', FakeMetrics): channel.deliver(notification) self.assertEqual(1, len(Services.metrics.email_sent.records))
def test_user_can_unsubscribe_through_header_link(self): user = create_user(email="*****@*****.**") comment = create_comment() comment2 = create_comment(author=user, replied_comment=comment) pn = Actions.replied(user, comment2) notification = Notification.from_pending_notification(pn, user, "EmailChannel") email_message = EmailChannel().make_message(notification, force=True) message = EmailChannel().make_email_backend_message(email_message) email_message.record_sent(notification.action) assert message.extra_headers unsubscribe_link = message.extra_headers.get("List-Unsubscribe") assert unsubscribe_link # Use should be able to receive notifications ... self.assertTrue(user.kv.subscriptions.can_receive('replied')) # Now, 'curl' the unsubscribe link self.assertStatus(200, unsubscribe_link) # Now, this action should be disabled in the notifications subscriptions. self.assertFalse(user.kv.subscriptions.can_receive('replied'))
def test_user_can_unsubscribe_through_header_link(self): user = create_user(email="*****@*****.**") comment = create_comment() comment2 = create_comment(author=user, replied_comment=comment) pn = Actions.replied(user, comment2) notification = Notification.from_pending_notification(pn, user, "EmailChannel") email_message = EmailChannel().make_message(notification, force=True) message = EmailChannel().make_email_backend_message(email_message) email_message.record_sent(notification.action) assert message.extra_headers unsubscribe_link = message.extra_headers.get("List-Unsubscribe") assert unsubscribe_link # Use should be able to receive notifications ... self.assertTrue(user.kv.subscriptions.can_receive("replied")) # Now, 'curl' the unsubscribe link self.assertStatus(200, unsubscribe_link) # Now, this action should be disabled in the notifications subscriptions. self.assertFalse(user.kv.subscriptions.can_receive("replied"))
def get_email_message(): user = create_user() pn = Actions.digest(user) notification = Notification.from_pending_notification(pn, user, "EmailChannel") return EmailChannel().make_message(notification, force=True)