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(recipients, "messages_reply_received", { 'message': msg, }) else: notification.send([sender], "messages_sent", { 'message': msg, }) notification.send(recipients, "messages_received", { 'message': msg, }) return message_list
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 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()
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(self.user1.sent_messages.count(), 1) self.assertEquals(self.user1.sent_messages.all() [0].subject, 'Subject Text 2') self.assertEquals(self.user2.received_messages.count(), 1) self.assertEquals(self.user2.received_messages.all() [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(self.user1.sent_messages.count(), 2) self.assertEquals(self.user2.received_messages.count(), 2)
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(self.user1.sent_messages.count(), 1) self.assertEquals(self.user1.sent_messages.all()[0].subject, 'Subject Text 2') self.assertEquals(self.user2.received_messages.count(), 1) self.assertEquals(self.user2.received_messages.all()[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(self.user1.sent_messages.count(), 2) self.assertEquals(self.user2.received_messages.count(), 2)
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)
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 _new_topic_message(request, referral_id, topic_id, object_id, redirect_to=None): client_id = object_id object = get_object_or_404( Client, pk=object_id, person__organization=request.user.get_profile().org_active) referral = Referral.objects.get( pk=referral_id, service__organization=request.user.get_profile().org_active) if not _access_check_referral_write(request, referral): return render_to_response('403.html', {'object': _("Oops! You don't have access for this service!"), }, context_instance=RequestContext(request)) messagetopic = MessageTopic.objects.get(pk=topic_id) if request.POST.get('message'): from datetime import datetime message = Message() message.sender = request.user message.sent_at = datetime.now message.body = request.POST['message'] message.topic = messagetopic message.save() from django.shortcuts import redirect return redirect(redirect_to or "/client/%s/referral/%s/messages/topic/%s" % (client_id, referral_id, topic_id)) else: return HttpResponseRedirect(redirect_to or '/client/%s/referral/%s/messages/topic/%s' % (object.id, referral.id, topic_id))
def _chat_message(request, object_id, messagetopic_id): """ This method is used to allow a user to chat. It is requested using AJAX and is used to insert a new message in the current topic. """ messagetopic = MessageTopic.objects.get(pk=messagetopic_id) message = Message() message.sender = request.user message.body = request.POST['messagebody'] message.topic = messagetopic message.save() from django.http import HttpResponse return HttpResponse("ok")
def _new_topic_message(request, referral_id, topic_id, object_id, redirect_to=None): client_id = object_id object = get_object_or_404( Client, pk=object_id, person__organization=request.user.get_profile().org_active) referral = Referral.objects.get( pk=referral_id, service__organization=request.user.get_profile().org_active) if not _access_check_referral_write(request, referral): return render_to_response( '403.html', { 'object': _("Oops! You don't have access for this service!"), }, context_instance=RequestContext(request)) messagetopic = MessageTopic.objects.get(pk=topic_id) if request.POST.get('message'): from datetime import datetime message = Message() message.sender = request.user message.sent_at = datetime.now message.body = request.POST['message'] message.topic = messagetopic message.save() from django.shortcuts import redirect return redirect(redirect_to or "/client/%s/referral/%s/messages/topic/%s" % (client_id, referral_id, topic_id)) else: return HttpResponseRedirect( redirect_to or '/client/%s/referral/%s/messages/topic/%s' % (object.id, referral.id, topic_id))