def test_send(self):
        self.assertRaises(AssertionError, send, queue=True, now=True)

        users = [self.user, self.user2]
        send(users, "label", now=True)
        self.assertEqual(len(mail.outbox), 2)
        self.assertIn(self.user.email, mail.outbox[0].to)
        self.assertIn(self.user2.email, mail.outbox[1].to)

        send(users, "label", queue=True)
        self.assertEqual(NoticeQueueBatch.objects.count(), 1)
        batch = NoticeQueueBatch.objects.all()[0]
        notices = cPickle.loads(base64.b64decode(batch.pickled_data))
        self.assertEqual(len(notices), 2)
Exemple #2
0
def new_comment(sender, instance, created, **kwargs):
    # remove this if-block if you want notifications for comment edit too
    if not created:
        return None

    context = {
        'comment': instance,
        'site': Site.objects.get_current(),
    }
    recipients = []

    # add all users who commented the same object to recipients
    for comment in instance.__class__.objects.for_model(instance.content_object):
        if comment.user not in recipients and comment.user != instance.user:
            recipients.append(comment.user)

    # if the commented object is a user then notify him as well
    if isinstance(instance.content_object, models.get_model('auth', 'User')):
        # if he his the one who posts the comment then don't add him to recipients
        if instance.content_object != instance.user and instance.content_object not in recipients:
            recipients.append(instance.content_object)

    notification.send(recipients, 'new_comment', context)
    def test_mixed_user_send(self):
        emails = [self.user, "*****@*****.**"]
        send(emails, "label")
        self.assertEqual(len(mail.outbox), 2)

        emails.append(self.user2)
        send(emails, "label")
        self.assertEqual(len(mail.outbox), 5)

        emails.append("*****@*****.**")
        emails.append("*****@*****.**")
        send(emails, "label")
        self.assertEqual(len(mail.outbox), 10)
        self.assertEqual(len(NoticeHistory.objects.all()), 3)

        emails = [u"*****@*****.**"]
        send(emails, "label")
        self.assertEqual(len(mail.outbox), 11)
 def test_send_default(self):
     # default behaviour, send_now
     users = [self.user, self.user2]
     send(users, "label")
     self.assertEqual(len(mail.outbox), 2)
     self.assertEqual(NoticeQueueBatch.objects.count(), 0)
 def test_non_user_send(self):
     emails = ["*****@*****.**", "*****@*****.**"]
     send(emails, "label")
     self.assertEqual(len(mail.outbox), 2)