Exemplo n.º 1
0
 def put(self, request, pk, format=None):
     user = self.get_object(pk)
     serializer = CartSerializer(user, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def put(self, request, pk, format=None):
     cart = self.get_object(pk)
     serializer = CartSerializer(cart, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
def update_cart_item(data, cart):
    cart_serializer = CartSerializer(data=data, instance=cart)
    if cart_serializer.is_valid():
        cart_serializer.save()
        return cart_serializer.data
    else:
        raise exceptions_utils.ValidationException(cart_serializer.errors,
                                                   status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
def add_item_to_cart(data):
    cart_serializer = CartSerializer(data=data)
    if cart_serializer.is_valid():
        cart_item = cart_serializer.save()
        keys = ['id', 'user', 'item', 'quantity',
                'price']  # data that we want to return as JSON response
        custom_response = {
            k: v
            for k, v in cart_serializer.data.iteritems() if k in keys
        }
        return custom_response
    else:
        raise exceptions_utils.ValidationException(cart_serializer.errors,
                                                   status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
def cart_detail(request, pk):
    """
            **Get all the product_categories data- Ignore**

            > GET

            Returns the product_categories data.

            * Possible HTTP status codes and JSON response:

                * `HTTP_200_OK` - Returns the products data:

                        {
                          "id": Integer,
                          "name": String,
                          "description": String,
                          "image": Url,
                          "is_available": Boolean
                        }

                * `HTTP_500_INTERNAL_SERVER_ERROR` - Internal server error

                :param pk:
                :param request:
            """
    data = request.data
    try:
        user = validations_utils.user_validation(
            pk)  # Validates if user exists or not.
        token_user_id = validations_utils.user_token_validation(
            request.auth.user_id, pk)  # Validates user's Token authentication.
    except ValidationException as e:  # Generic exception
        return Response(e.errors, status=e.status)

    if request.method == 'GET':
        if Cart.objects.filter(user_id=user.id).exists(
        ):  # Checks if product_category exists with given id.
            cart_items = Cart.objects.filter(user_id=user.id)
        else:
            return Response(messages.EMPTY_CART,
                            status=status.HTTP_404_NOT_FOUND)
        if cart_items:
            cart_serializer = CartSerializer(cart_items, many=True)
            cart_data = cart_serializer.data
            data = []
            for obj in cart_data:
                x = utils.get_item_id(obj)
                item = validations_utils.item_validation(int(x))
                obj['name'] = item.name
                data.append(obj)
            return Response(data, status=status.HTTP_200_OK)
        else:
            return Response(messages.EMPTY_CART,
                            status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 6
0
    def post(self):
        """Принимает продукт и его кол-во, после чего добавляет товар в корзину."""

        cart = Cart(session)
        data = request.get_json()

        try:
            cart_item = CartSerializer(**data)
        except ValidationError as err:
            return Response(status=400, response=err.json(), mimetype='application/json')

        cart.add_item(cart_item.product, cart_item.quantity)

        return Response(status=201, response=dumps(cart.cart))
Exemplo n.º 7
0
 def post(self, request, format=None):
     serializer = CartSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 8
0
 def cart(self, request, pk=None):
     user = User.objects.get(pk=pk)
     queryset = Cart.objects.get(user=user)
     serializer = CartSerializer(queryset)
     return Response(serializer.data)
Exemplo n.º 9
0
 def get(self, request):
     users = Cart.objects.all()
     print(users)
     serialized_account = CartSerializer(users, many=True)
     return Response(serialized_account.data)
Exemplo n.º 10
0
 def get(self, request, pk, format=None):
     user = self.get_object(pk)
     serializer = CartSerializer(user)
     return Response(serializer.data)
Exemplo n.º 11
0
 def post(self, request):
     data = JSONParser().parse(request)
     serializer = CartSerializer(data=data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=201)
Exemplo n.º 12
0
 def get(self, request, pk, format=None):
     cart = self.get_object(pk)
     cart = CartSerializer(cart)
     return Response(cart.data)
Exemplo n.º 13
0
 def post(self, request, format=None):
     serializer = CartSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 14
0
 def get(self, request, format=None):
     carts = NCartInfo.objects.all()
     serializer = CartSerializer(carts, many=True)
     return Response(serializer.data)