Esempio n. 1
0
def update_cart(request, item_id):
    if request.method == "POST":
        cart = utils.get_or_create_cart(request)
        item = get_object_or_404(CartItem, pk=item_id)

        if item not in cart.cartitem_set.all():
            return HttpResponse("Bad request", status=400)

        form = CartItemForm(request.POST, instance=item)
        if form.is_valid():
            form.save()

        return redirect("kishore_cart")
    else:
        return HttpResponse("Bad request", status=400)
Esempio n. 2
0
def add_to_cart(request):
    if request.method == "POST":
        cart = utils.get_or_create_cart(request)
        form = CartItemForm(request.POST)
        if form.is_valid():
            item = form.save(commit=False)

            cart_items = cart.cartitem_set.filter(product=item.product)
            # check to see we don't already have this product in the cart
            if len(cart_items) > 0:
                item_to_update = cart_items[0]
                item_to_update.quantity = item_to_update.quantity + item.quantity
                item_to_update.save()
            else:
                item.cart = cart
                item.save()

        return redirect("kishore_cart")
    else:
        return HttpResponse("Bad request", status=400)