def test_send(self): pigeonpost_queue.send(sender=AggregateNews, send_to=self.bob) process_queue() messages = Outbox.objects.all() self.assertEqual(len(messages),1) self.assertEqual(messages[0].user, self.bob)
def test_no_method_or_target(self): """ Test that the pigeon is processed correctly when there are no recipients """ # Then try to send custom news to Bob pigeonpost_queue.send(sender=self.moderated_news, render_email_method='email_moderators') process_queue() messages = Outbox.objects.all() self.assertEqual(len(messages),2)
def save(self, *args, **kwargs): super(ModeratedNews, self).save(*args, **kwargs) if self.published: # sending ModeratedNews to moderators (nearly) immediately, # and sending them to users in 6 hours if the ModeratedNews # items remain published. pigeonpost_queue.send(sender=self, render_email_method='email_news', defer_for=6*60*60) pigeonpost_queue.send(sender=self, render_email_method='email_moderators')
def test_send_to_method(self): """ Test that we can send a pigeon to a specific user """ pigeonpost_queue.send(sender=self.news, render_email_method='email_news', send_to_method='get_everyone_called_bob') process_queue() messages = Outbox.objects.all() self.assertEqual(len(messages),1) self.assertEqual(messages[0].user, self.bob)
def test_no_recipients(self): """ Test that the pigeon is processed correctly when there are no recipients """ # Remove Bob User.objects.filter(username='******').delete() # Then try to send custom news to Bob pigeonpost_queue.send(sender=self.news, render_email_method='email_news', send_to_method='get_everyone_called_bob') process_queue() messages = Outbox.objects.all() self.assertEqual(len(messages),0) pigeon = Pigeon.objects.get(source_id=self.news.id) self.assertEqual(pigeon.to_send, False)
def test_resend_pigeon(self): process_queue(force=True) self.assertEqual(Outbox.objects.count(), 2) self.assertEqual(Outbox.objects.filter(user__username='******').count(), 0) # Now another user signs up chelsea = Profile.objects.get(user__username='******') chelsea.subscribed_to_news = True chelsea.save() # And we resend the pigeon pigeonpost_queue.send(sender=self.message, retry=True) process_queue(force=True) # And chelsea gets a message self.assertEqual(Outbox.objects.count(), 3) self.assertEqual(Outbox.objects.filter(user__username='******').count(), 1)
def generate_comment_pigeons(sender, **kwargs): """ Whenever a comment is received, we create a number of pigeons """ # (use 'comment' from kwargs if changing back to comment_was_posted signal) comment = kwargs.get('instance') post = comment.content_object # The first pigeon is to the post author pigeonpost_queue.send(sender=comment, render_email_method='email_author_about_comment', send_to=post.author) # The second pigeon is for any commenters on the post pigeonpost_queue.send(sender=comment, render_email_method='email_commenters', send_to_method='get_commenters')
def test_updated_scheduled_for(self): """ Sending the same pigeon details just updates scheduled_for """ # First try using defer_for pigeonpost_queue.send(sender=self.message, defer_for=10) # 10 seconds pigeon = Pigeon.objects.get( source_content_type=ContentType.objects.get_for_model(self.message), source_id=self.message.id) delta = pigeon.scheduled_for - datetime.datetime.now() self.assertTrue(delta.seconds<=10) # now try with scheduled_for now = datetime.datetime.now() pigeonpost_queue.send(sender=self.message, scheduled_for=now) pigeon = Pigeon.objects.get( source_content_type=ContentType.objects.get_for_model(self.message), source_id=self.message.id) self.assertEqual(pigeon.scheduled_for, now)
def save(self, *args, **kwargs): just_published_now = False # The first time it is saved it is published automatically if not self.date_published: self.published = True self.date_published = datetime.date.today() self.enable_comments = self.listing.allow_comments just_published_now = True # Check that the author is allowed to post to this list if not self.listing.can_user_post(self.author): raise PermissionDenied super(Post, self).save(*args, **kwargs) # We send out the pigeons after the save statement because pigeonpost needs # the Post.id for source_id. # # Note that all pigeonpost signals won't send an email if there is already # a pigeon with the same parameters and to_send=False. # Staff and moderators can post without being moderated themselves if self.published and not self.author.is_staff and not self.listing.can_user_moderate(self.author): # When the post is saved, the moderators are notified, with a delay pigeonpost_queue.send(sender=self, render_email_method='email_moderator', defer_for=settings.PIGEONPOST_DELAYS['cms.Post']['moderator']) if just_published_now: # We send the author an email to let them know they can edit it pigeonpost_queue.send(sender=self, render_email_method='email_author', defer_for=settings.PIGEONPOST_DELAYS['cms.Post']['author'], send_to=self.author) # Let subscribers to the listing of this post know about it pigeonpost_queue.send(sender=self, render_email_method='email_subscriber', defer_for=settings.PIGEONPOST_DELAYS['cms.Post']['subscriber'], send_to_method='get_subscribers')
def _send_now(self): pigeonpost_queue.send(sender=self.message) # send now process_queue() self.pigeon = Pigeon.objects.get(id=self.pigeon.id)
def test_many_signals_one_pigeon(self): pigeonpost_queue.send(sender=self.message, defer_for=1000) pigeonpost_queue.send(sender=self.message, defer_for=1000) pigeonpost_queue.send(sender=self.message, defer_for=1000) self.assertEqual(Pigeon.objects.count(), 1) pigeonpost_queue.send(sender=self.message) process_queue() pigeonpost_queue.send(sender=self.message) process_queue() pigeonpost_queue.send(sender=self.message) process_queue() self.assertEqual(Pigeon.objects.count(), 1)
def save(self, *args, **kwargs): """ Post the message when it is saved. Defer sending for 6 hours """ super(News, self).save(*args, **kwargs) pigeonpost_queue.send(sender=self, defer_for=6*60*60)