Exemplo n.º 1
0
 def delete(self, request, id, list_id):
     try:
         product = Product.objects.get(id=request.data["product_id"])
     except:
         return Response({"message": "bad request: product"},
                         status=status.HTTP_400_BAD_REQUEST)
     try:
         customer = Customer.objects.get(user=id)
     except:
         return Response({"message": "bad request: customer"},
                         status=status.HTTP_400_BAD_REQUEST)
     try:
         list = ProductList.objects.get(id=list_id)
     except:
         return Response({"message": "bad request: list"},
                         status=status.HTTP_400_BAD_REQUEST)
     if request.user.id != id:
         return Response({"message": "bad token"},
                         status=status.HTTP_400_BAD_REQUEST)
     if list.customer_id != customer.user_id:
         return Response({"message": "not allowed to access"},
                         status=status.HTTP_401_UNAUTHORIZED)
     # Error handling done
     if product in list.product_set.all():
         list.product_set.remove(product)
         serializer = ProductListSerializer(list,
                                            context={'request': request})
         return Response(serializer.data, status=HTTP_202_ACCEPTED)
     else:
         serializer = ProductListSerializer(list,
                                            context={'request': request})
         return Response(serializer.data, status=HTTP_204_NO_CONTENT)
Exemplo n.º 2
0
 def post(self, request, id):
     if not self.is_user(request, id):
         return Response({"message": "bad request"},
                         status=status.HTTP_400_BAD_REQUEST)
     # error handling done above
     serializer = ProductListSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=HTTP_201_CREATED)
     return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
    def create(self, request):
        payload = request.data
        serializer = self.get_serializer(data=payload)
        serializer.is_valid(raise_exception=True)

        user = request.user
        title = serializer.validated_data.get('title')
        desc = serializer.validated_data.get('description')
        price = serializer.validated_data.get('price')
        quantity = serializer.validated_data.get('quantity')
        images = serializer.validated_data.get('images')
        category = serializer.validated_data.get('category')

        category_query = Category.objects.filter(name=category).first()

        product_params = {
            "title": title,
            "description": desc,
            "price": price,
            "quantity": quantity,
            "seller": user
        }
        product_inst = Product.objects.create(**product_params)
        product_inst.category.add(category_query)
        product_inst.save()

        for image in images:
            image_param = {"image": image}
            image_inst, created = ProductImage.objects.get_or_create(
                **image_param)
            image_inst.product.add(product_inst)
            image_inst.save()

        product_details = ProductListSerializer(product_inst, many=False).data
        return Response(product_details)
Exemplo n.º 4
0
class CartListSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    product = ProductListSerializer(read_only=True)

    class Meta:
        model = CartList
        fields = ('product', 'id', 'quantity', 'user')
Exemplo n.º 5
0
 def put(self, request, id, list_id):
     if not self.check_user(request, id, list_id):
         return Response({"message": "bad request"},
                         status=status.HTTP_400_BAD_REQUEST)
     if "is_private" in request.data and request.data["is_private"] not in [
             "true", "false"
     ]:
         return Response(
             {"is_private": ["return either 'true' or 'false'."]},
             status=status.HTTP_400_BAD_REQUEST)
     list = self.get_list(request, list_id)
     if not self.is_owner(request, id, list_id):
         return Response({"message": "not allowed to access"},
                         status=status.HTTP_401_UNAUTHORIZED)
     # error handling done above
     try:
         if "name" in request.data:
             list.name = request.data["name"]
         if "is_private" in request.data:
             list.is_private = (request.data["is_private"] == "true")
         list.save()
         serializer = ProductListSerializer(list)
         return Response(serializer.data)
     except:
         return Response({"message": "bad request"},
                         status=HTTP_400_BAD_REQUEST)
Exemplo n.º 6
0
 def get(self, request, id):
     if not self.check_private_access(request, id):
         return Response({"message": "not allowed to access"},
                         status=status.HTTP_401_UNAUTHORIZED)
     a_list = self.get_alert_list(id)
     serializer = ProductListSerializer(a_list,
                                        context={'request': request})
     return Response(serializer.data)
Exemplo n.º 7
0
 def get(self, request, id):
     if len(Customer.objects.filter(pk=id)) == 0:  # is user does not exist
         raise Http404
     product_lists = self.get_list_list(request, id)
     serializer = ProductListSerializer(product_lists,
                                        many=True,
                                        context={'request': request})
     return Response(serializer.data)
Exemplo n.º 8
0
class CartDetailSerializer(serializers.ModelSerializer):
    customer = UserSerializer(read_only=True)
    product = serializers.SerializerMethodField()
    user = Userserializer(read_only=True)
    product = ProductListSerializer(read_only=True)

    class Meta:
        model = CartList
        fields = ('id', 'customer', 'user', 'items', 'created', 'updated')
Exemplo n.º 9
0
class CartItemSerializer(serializers.ModelSerializer):
    """Serializer for the CartItem model."""

    cart = CartSerializer(read_only=True)
    product = ProductListSerializer(read_only=True)

    class Meta:
        model = CartList
        fields = ('id', 'cart', 'product', 'quantity')
Exemplo n.º 10
0
 def get(self, request, id, list_id):
     if not self.check_user(request, id, list_id):
         return Response({"message": "bad request"},
                         status=status.HTTP_400_BAD_REQUEST)
     list = self.get_list(request, list_id)
     if not self.is_owner(request, id, list_id) and list.is_private:
         return Response({"message": "not allowed to access"},
                         status=status.HTTP_401_UNAUTHORIZED)
     # error handling done above
     serializer = ProductListSerializer(list, context={'request': request})
     return Response(serializer.data)
Exemplo n.º 11
0
 def get_product(self, obj):
     product_queryset = Product.objects.filter(user=obj.id)
     product = ProductListSerializer(product_queryset, many=True).data
     return product
Exemplo n.º 12
0
class CartItemSerializer(serializers.ModelSerializer):
    product = ProductListSerializer(Product.objects.all(), many=True)

    class Meta:
        model = CartItem
        fields = '__all__'
 def get_product_details(self, obj):
     product = obj.product
     product_details = ProductListSerializer(product, many=False).data
     return product_details