예제 #1
0
    def post(self, request, *args, **kwargs):
        """Dispatch message to start a new conversation"""
        recipient_username = request.POST.get('recipient') or 'admin'
        recipient = User.objects.get(username=recipient_username)
        payload = {
            'subject': request.POST.get('subject'),
            'sender': User.objects.get(username=request.user.username),
            'body': request.POST.get('body'),
            'recipient': recipient,
        }  # create new message payload

        mesg = Message(**payload)
        mesg.sent_at = timezone.now()
        mesg.save()

        alert.add_message(
            request, alert.SUCCESS,
            'New conversation started with {}.'.format(recipient.username))

        return redirect(reverse('message', kwargs={'m_id': mesg.id}))
예제 #2
0
    def post(self, request, m_id):
        """Dispatch a reply to a conversation"""
        recipient_username = request.POST.get('recipient') or 'admin'
        recipient = User.objects.get(username=recipient_username)
        parent_msg_id = request.POST.get('parent_msg')

        message = Message.objects.get(id=int(parent_msg_id))
        message.replied_at = timezone.now()
        message.save()  # update replied at
        parent_msg_id = Message.objects.get(id=m_id)

        payload = {
            'sender': User.objects.get(username=request.user.username),
            'parent_msg': message,
            'body': request.POST.get('body'),
            'recipient': recipient,
        }  # create reply payload
        reply = Message(**payload)
        reply.sent_at = timezone.now()
        reply.save()

        alert.add_message(request, alert.SUCCESS, 'Message sent successfully.')

        return redirect(reverse('message', kwargs={'m_id': message.id}))