Beispiel #1
0
    def post(self, request):
        try:
            token = AccessToken(request.data["access"])
            token.check_exp()

        except Exception as e:
            return Response(invalidToken)

        user_id = TokenUser(token).id
        user = User.objects.filter(id=user_id).first()

        setOfClothes = SetOfClothes.objects.all().filter(users=user)

        if not setOfClothes:
            return Response({"response": "user have not set of clothes"})

        listClothes = []
        # print(setOfClothes)
        for i in setOfClothes:
            clothes = Clothes.objects.all().filter(set_Of_Clothes=i.id)

            serializer = ClothesSerializer(clothes, many=True)
            # listClothes.append({
            #     f'set {i.id}': serializer.data
            # })
            listClothes.append({f'{i.name_Set_Of_Clothes}': serializer.data})

        return Response(listClothes)
Beispiel #2
0
    def put(self, request):
        try:
            token = AccessToken(request.data[0]["access"])
            token.check_exp()

        except Exception as e:
            return Response(invalidToken)

        if not existClothes(request):
            return Response(status=status.HTTP_400_BAD_REQUEST)

        # if existCloneSetOfClothes(request):
        #     return Response(status=status.HTTP_400_BAD_REQUEST)

        # get user in token
        user_id = TokenUser(token).id
        user = User.objects.filter(id=user_id).first()

        # create a new set of clothes and associate with a user
        setOfClothes = SetOfClothes.objects.create(
            users=user,
            name_Set_Of_Clothes=request.data[1]
            ["nameSetOfClothes"])  #bulk=False

        for i in range(2, len(request.data)):
            # print(str(request.data[i]))
            clothes = Clothes.objects.get_or_create(
                name_Clothes=request.data[i]["nameClothes"],
                type_Clothes=request.data[i]["typeClothes"],
                description=request.data[i]["description"],
                price=request.data[i]["price"],
                link_Image=request.data[i]["linkImage"],
                link_Source=request.data[i]["linkSource"])
            id_clothes = Clothes.objects.get(
                link_Source=request.data[i]["linkSource"]).id
            setOfClothes.clothes_set.add(id_clothes)

        return Response({"nameSetOfClothes": setOfClothes.name_Set_Of_Clothes})
Beispiel #3
0
    def delete(self, request):
        try:
            token = AccessToken(request.data["access"])
            token.check_exp()

        except Exception as e:
            return Response(invalidToken)

        # get set of clothes
        setOfClothes = SetOfClothes.objects.filter(
            name_Set_Of_Clothes=request.data["nameSetOfClothes"]).first()

        if setOfClothes is None:
            return Response(status=status.HTTP_400_BAD_REQUEST)

        # user_id = TokenUser(token).id
        # user = User.objects.filter(id=user_id).first()

        SetOfClothes.objects.filter(
            name_Set_Of_Clothes=request.data["nameSetOfClothes"]).delete()

        # Допили удаление
        # return Response(status=status.HTTP_205_RESET_CONTENT)
        return Response({"status": "successful"})