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)
def message(self, request, channel_id=None): try: channel = ChannelService.get_channel_from_id(channel_id) except Channel.DoesNotExist: return Response({u'detail': u'channel不存在'}, status=status.HTTP_404_NOT_FOUND) serializer = ChatSerializer(data=request.data) serializer.is_valid(raise_exception=True) content = serializer.validated_data.get('content', '') file = serializer.validated_data.get('file', None) type = serializer.validated_data['type'] message = channel.send_message(request.user, type, content, file) return Response(MessageSerializer(message).data, status=status.HTTP_200_OK)
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)
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
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)
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)