Exemplo n.º 1
0
def chat_details(request, value):

    try:
        chat = Chat.objects.get(id=value)
    except Chat.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = ChatSerializer(chat)
        return JsonResponse(serializer.data, safe=False, status=200)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = ChatSerializer(chat, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=200)
        return JsonResponse(serializer.errors, status=204)

    elif request.method == 'DELETE':
        try:
            chat.delete()
            return HttpResponse(status=200)
        except:
            return HttpResponse(status=409)
Exemplo n.º 2
0
 def create_chat(self, sender_id, thread_id, text):
     serializer = ChatSerializer(data={
         'sender': sender_id,
         'thread': thread_id,
         'text': text
     },
                                 context={'sender': self.sender})
     if serializer.is_valid():
         serializer.save()
         # return json.dumps({"messsage" : serializer.data})
         return json.dumps(serializer.data)
         # return text
     return serializer.errors
Exemplo n.º 3
0
def chat_list(request):
    if request.method == 'GET':
        chats = Chat.objects.all()
        serializer = ChatSerializer(chats, many=True)
        return JsonResponse(serializer.data, safe=False, status=200)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = ChatSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=204)
Exemplo n.º 4
0
    def chats(self, request, pk=None):
        """
        /api/users/<id>/chats/
            GET - Returns a list of chats associated with the user
        /api/users/<id>/chats/<id>
            POST - Creates a new chat with the users sent in the body
        """

        user = self.get_object()
        if request.method == 'GET':
            chats = Chat.objects.filter(users=user)
            context = {'request': request}
            serializer = ChatSerializer(chats, many=True, context=context)
            return Response(serializer.data)
        if request.method == 'POST':
            serializer = ChatSerializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(serializer.data, status=201)
Exemplo n.º 5
0
    def post(self, request):
        data = {**request.data, **{'sender': request.user.user_token}}
        serializer = ChatSerializer(data=data)

        if serializer.is_valid():
            chat = serializer.save()
            message = chat.sender.username + ': ' + chat.message
            send_push_message(token=chat.receiver.device_token,
                              message=message,
                              notification_type='message-received',
                              data=ChatSerializer(chat).data)
            return Response(status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)