def save(self, sender, parent_msg=None): recipients = self.cleaned_data['recipient'] subject = self.cleaned_data['subject'] body = self.cleaned_data['body'] message_list = [] for r in recipients: msg = Message( sender = sender, recipient = r, subject = subject, body = body, ) if parent_msg is not None: msg.parent_msg = parent_msg parent_msg.replied_at = datetime.datetime.now() parent_msg.save() msg.save() message_list.append(msg) if notification: if parent_msg is not None: notification.send([sender], "messages_replied", {'message': msg,}) notification.send([r], "messages_reply_received", {'message': msg,}) else: notification.send([sender], "messages_sent", {'message': msg,}) notification.send([r], "messages_received", {'message': msg,}) return message_list
class SendTestCase(TestCase): def setUp(self): self.user1 = User.objects.create_user('user1', '*****@*****.**', '123456') self.user2 = User.objects.create_user('user2', '*****@*****.**', '123456') self.msg1 = Message(sender=self.user1, recipient=self.user2, subject='Subject Text', body='Body Text') self.msg1.save() def testBasic(self): self.assertEquals(self.msg1.sender, self.user1) self.assertEquals(self.msg1.recipient, self.user2) self.assertEquals(self.msg1.subject, 'Subject Text') self.assertEquals(self.msg1.body, 'Body Text') self.assertEquals(self.user1.sent_messages.count(), 1) self.assertEquals(self.user1.received_messages.count(), 0) self.assertEquals(self.user2.received_messages.count(), 1) self.assertEquals(self.user2.sent_messages.count(), 0)
class DeleteTestCase(TestCase): def setUp(self): self.user1 = User.objects.create_user('user3', '*****@*****.**', '123456') self.user2 = User.objects.create_user('user4', '*****@*****.**', '123456') self.msg1 = Message(sender=self.user1, recipient=self.user2, subject='Subject Text 1', body='Body Text 1') self.msg2 = Message(sender=self.user1, recipient=self.user2, subject='Subject Text 2', body='Body Text 2') self.msg1.sender_deleted_at = datetime.datetime.now() self.msg2.recipient_deleted_at = datetime.datetime.now() self.msg1.save() self.msg2.save() def testBasic(self): self.assertEquals(Message.objects.outbox_for(self.user1).count(), 1) self.assertEquals(Message.objects.outbox_for(self.user1)[0].subject, 'Subject Text 2') self.assertEquals(Message.objects.inbox_for(self.user2).count(),1) self.assertEquals(Message.objects.inbox_for(self.user2)[0].subject, 'Subject Text 1') #undelete self.msg1.sender_deleted_at = None self.msg2.recipient_deleted_at = None self.msg1.save() self.msg2.save() self.assertEquals(Message.objects.outbox_for(self.user1).count(), 2) self.assertEquals(Message.objects.inbox_for(self.user2).count(),2)