Пример #1
0
 def post(self, request, *args, **kwargs):
     user_id = request.data.get("user_id", "")
     if not user_id:
         return Response(data={"message": "invalid user"},
                         status=status.HTTP_400_BAD_REQUEST)
     profile = get_profile(user_id)
     chats = ChatSerializer(profile.chats.all(), many=True)
     return Response({"chats": chats.data})
Пример #2
0
 def create(self, validated_data):
     users = validated_data.pop("users")
     data = validated_data.pop("datatext")
     chat = Chat(data=data)
     chat.save()
     for user in users:
         informer = get_profile(user)
         chat.participants.add(informer)
     return chat
Пример #3
0
 def post(self, request, *args, **kwargs):
     chat_id = kwargs.get('pk', '')
     if isinstance(chat_id, int):
         chat = get_current_chat(chat_id)
         profile = get_profile(request.user.profile.id)
         try:
             chat.participants.get(id=profile.id)
         except ObjectDoesNotExist:
             return Response(status=status.HTTP_400_BAD_REQUEST,
                             data={
                                 "message":
                                 'You are not a participant for this chat'
                             })
         serializer = self.serializer_class(chat)
         return Response(data={"chat": serializer.data})
     else:
         return Response(status=status.HTTP_400_BAD_REQUEST,
                         data={"message": 'invalid chat id'})
Пример #4
0
 def post(self, request, *args, **kwargs):
     username = request.data.get("username", "")
     password = request.data.get("password", "")
     print('hi')
     user = authenticate(request, username=username, password=password)
     print(user)
     if user is not None:
         profile = get_profile(user.profile.id)
         login(request, user)
         if profile:
             user_data = self.serializer_class(profile)
             token = TokenSerializer(
                 data={
                     "token": jwt_encode_handler(jwt_payload_handler(user)),
                 })
             token.is_valid()
             data = {
                 "profile": user_data.data,
                 "token": token.data['token'],
             }
             return Response(data)
     return Response(status=status.HTTP_401_UNAUTHORIZED)