Exemplo n.º 1
0
def cart(request, template="shop/cart.html"):
    """
    Display cart and handle removing items from the cart.
    """
    cart_formset = CartItemFormSet(instance=request.cart)
    discount_form = DiscountForm(request, request.POST or None)
    if request.method == "POST":
        valid = True
        if request.POST.get("update_cart"):
            valid = request.cart.has_items()
            if not valid:
                # Session timed out.
                info(request, _("Your cart has expired"))
            else:
                cart_formset = CartItemFormSet(request.POST,
                                               instance=request.cart)
                valid = cart_formset.is_valid()
                if valid:
                    cart_formset.save()
                    recalculate_discount(request)
                    info(request, _("Cart updated"))
        else:
            valid = discount_form.is_valid()
            if valid:
                discount_form.set_discount()
        if valid:
            return redirect("shop_cart")
    context = {"cart_formset": cart_formset}
    settings.use_editable()
    if (settings.SHOP_DISCOUNT_FIELD_IN_CART and
        DiscountCode.objects.active().count() > 0):
        context["discount_form"] = discount_form
    return render(request, template, context)
Exemplo n.º 2
0
def update_cart(request):
	if request.is_ajax() and request.method == "POST":
		
		sub = {}
		grand = 0
		form = request.POST
		
		cart_formset = CartItemFormSet(request.POST, instance=request.cart)
		cart = cart_formset[0].instance.cart

		
		valid = cart_formset.is_valid()
		
		if valid:
			cart_formset.save()
			sub = [float(f.instance.total_price) for f in cart_formset]
			subtotal = float(cart.total_price())
			discount = get_discount_on_update(request)
			grand = "{0:.2f}".format(subtotal - discount)
			total_qty = int(cart.total_quantity())
			if total_qty == 0: set_shipping(request, "Shipping", 0)
			return HttpResponse(json.dumps({'sub':sub, 'subtotal' : subtotal, 'grand':grand, 'total_qty':total_qty, 'discount_total':discount}), content_type='application/json')
		else:
			error = cart_formset.errors[0]['quantity'][0]
			cart_formset = CartItemFormSet(instance=request.cart)
			messages.info(request, _(error))
			#cart_formset._errors = errors
			return HttpResponse(json.dumps({'error': error}), content_type='application/json')
	else:
		return HttpResponse('Sth went wrong.')
Exemplo n.º 3
0
def cart(request, template="shop/cart.html"):
    """
    Display cart and handle removing items from the cart.
    """
    cart_formset = CartItemFormSet(instance=request.cart)
    discount_form = DiscountForm(request, request.POST or None)
    if request.method == "POST":
        valid = True
        if request.POST.get("update_cart"):
            valid = request.cart.has_items()
            if not valid:
                # Session timed out.
                info(request, _("Your cart has expired"))
            else:
                cart_formset = CartItemFormSet(request.POST,
                                               instance=request.cart)
                valid = cart_formset.is_valid()
                if valid:
                    cart_formset.save()
                    recalculate_cart(request)
                    info(request, _("Cart updated"))
                else:
                    # Reset the cart formset so that the cart
                    # always indicates the correct quantities.
                    # The user is shown their invalid quantity
                    # via the error message, which we need to
                    # copy over to the new formset here.
                    errors = cart_formset._errors
                    cart_formset = CartItemFormSet(instance=request.cart)
                    cart_formset._errors = errors
        else:
            valid = discount_form.is_valid()
            if valid:
                discount_form.set_discount()
            # Potentially need to set shipping if a discount code
            # was previously entered with free shipping, and then
            # another was entered (replacing the old) without
            # free shipping, *and* the user has already progressed
            # to the final checkout step, which they'd go straight
            # to when returning to checkout, bypassing billing and
            # shipping details step where shipping is normally set.
            recalculate_cart(request)
        if valid:
            return redirect("shop_cart")
    context = {"cart_formset": cart_formset}
    settings.use_editable()
    if (settings.SHOP_DISCOUNT_FIELD_IN_CART
            and DiscountCode.objects.active().exists()):
        context["discount_form"] = discount_form
    return render(request, template, context)
Exemplo n.º 4
0
def cart(request, template="shop/cart.html"):
    """
    Display cart and handle removing items from the cart.
    """
    cart_formset = CartItemFormSet(instance=request.cart)
    discount_form = DiscountForm(request, request.POST or None)
    if request.method == "POST":
        valid = True
        if request.POST.get("update_cart"):
            valid = request.cart.has_items()
            if not valid:
                # Session timed out.
                info(request, _("Your cart has expired"))
            else:
                cart_formset = CartItemFormSet(request.POST,
                                               instance=request.cart)
                valid = cart_formset.is_valid()
                if valid:
                    cart_formset.save()
                    recalculate_discount(request)
                    info(request, _("Cart updated"))
        else:
            valid = discount_form.is_valid()
            if valid:
                discount_form.set_discount()
        if valid:
            return redirect("shop_cart")
    context = {"cart_formset": cart_formset}
    settings.use_editable()
    if (settings.SHOP_DISCOUNT_FIELD_IN_CART and
        DiscountCode.objects.active().count() > 0):
        context["discount_form"] = discount_form
    return render(request, template, context)
Exemplo n.º 5
0
def cart(request, template="shop/cart.html"):
    """
    Display cart and handle removing items from the cart.
    """
    cart_formset = CartItemFormSet(instance=request.cart)
    discount_form = DiscountForm(request, request.POST or None)
    if request.method == "POST":
        valid = True
        if request.POST.get("update_cart"):
            valid = request.cart.has_items()
            if not valid:
                # Session timed out.
                info(request, _("Your cart has expired"))
            else:
                cart_formset = CartItemFormSet(request.POST,
                                               instance=request.cart)
                valid = cart_formset.is_valid()
                if valid:
                    cart_formset.save()
                    recalculate_discount(request)
                    info(request, _("Cart updated"))
                else:
                    # Reset the cart formset so that the cart
                    # always indicates the correct quantities.
                    # The user is shown their invalid quantity
                    # via the error message, which we need to
                    # copy over to the new formset here.
                    errors = cart_formset._errors
                    cart_formset = CartItemFormSet(instance=request.cart)
                    cart_formset._errors = errors
        else:
            valid = discount_form.is_valid()
            if valid:
                discount_form.set_discount()
        if valid:
            return redirect("shop_cart")
    context = {"cart_formset": cart_formset}
    settings.use_editable()
    if (settings.SHOP_DISCOUNT_FIELD_IN_CART
            and DiscountCode.objects.active().count() > 0):
        context["discount_form"] = discount_form
    return render(request, template, context)
Exemplo n.º 6
0
def cart(request, template="shop/cart.html"):
    """
    Display cart and handle removing items from the cart.
    """
    cart_formset = CartItemFormSet(instance=request.cart)
    discount_form = DiscountForm(request, request.POST or None)
    if request.method == "POST":
        valid = True
        if request.POST.get("update_cart"):
            valid = request.cart.has_items()
            if not valid:
                # Session timed out.
                info(request, _("Your cart has expired"))
            else:
                cart_formset = CartItemFormSet(request.POST,
                                               instance=request.cart)
                valid = cart_formset.is_valid()
                if valid:
                    cart_formset.save()
                    recalculate_cart(request)
                    info(request, _("Cart updated"))
                else:
                    # Reset the cart formset so that the cart
                    # always indicates the correct quantities.
                    # The user is shown their invalid quantity
                    # via the error message, which we need to
                    # copy over to the new formset here.
                    errors = cart_formset._errors
                    cart_formset = CartItemFormSet(instance=request.cart)
                    cart_formset._errors = errors
        else:
            valid = discount_form.is_valid()
            if valid:
                discount_form.set_discount()
            # Potentially need to set shipping if a discount code
            # was previously entered with free shipping, and then
            # another was entered (replacing the old) without
            # free shipping, *and* the user has already progressed
            # to the final checkout step, which they'd go straight
            # to when returning to checkout, bypassing billing and
            # shipping details step where shipping is normally set.
            recalculate_cart(request)
        if valid:
            return redirect("shop_cart")
    context = {"cart_formset": cart_formset}
    settings.use_editable()
    if (settings.SHOP_DISCOUNT_FIELD_IN_CART and
        DiscountCode.objects.active().exists()):
        context["discount_form"] = discount_form
    return render(request, template, context)
Exemplo n.º 7
0
def cart(request, template="shop/cart.html"):
    """
    Display cart and handle removing items from the cart.
    """
    cart_formset = CartItemFormSet(instance=request.cart)
    discount_form = DiscountForm(request, request.POST or None)
    if request.method == "POST":
        valid = True
        if request.POST.get("update_cart"):
            valid = request.cart.has_items()
            if not valid:
                # Session timed out.
                info(request, _("Your cart has expired"))
            else:
                cart_formset = CartItemFormSet(request.POST,
                                               instance=request.cart)
                valid = cart_formset.is_valid()
                if valid:
                    cart_formset.save()
                    recalculate_cart(request)
                    info(request, _("Cart updated"))
                else:
                    # Reset the cart formset so that the cart
                    # always indicates the correct quantities.
                    # The user is shown their invalid quantity
                    # via the error message, which we need to
                    # copy over to the new formset here.
                    errors = cart_formset._errors
                    cart_formset = CartItemFormSet(instance=request.cart)
                    cart_formset._errors = errors
        else:
            valid = discount_form.is_valid()
            if valid:
                discount_form.set_discount()
        if valid:
            return redirect("shop_cart")
    context = {"cart_formset": cart_formset}
    settings.use_editable()
    no_discounts = not DiscountCode.objects.active().exists()
    discount_applied = "discount_code" in request.session
    discount_in_cart = settings.SHOP_DISCOUNT_FIELD_IN_CART
    if not no_discounts and not discount_applied and discount_in_cart:
        context["discount_form"] = discount_form
    return render(request, template, context)
Exemplo n.º 8
0
def update_cart(request):
    if request.is_ajax() and request.method == "POST":

        sub = {}
        grand = 0
        form = request.POST

        cart_formset = CartItemFormSet(request.POST, instance=request.cart)
        cart = cart_formset[0].instance.cart

        valid = cart_formset.is_valid()

        if valid:
            cart_formset.save()
            sub = [float(f.instance.total_price) for f in cart_formset]
            subtotal = float(cart.total_price())
            discount = get_discount_on_update(request)
            grand = "{0:.2f}".format(subtotal - discount)
            total_qty = int(cart.total_quantity())
            if total_qty == 0: set_shipping(request, "Shipping", 0)
            return HttpResponse(json.dumps({
                'sub': sub,
                'subtotal': subtotal,
                'grand': grand,
                'total_qty': total_qty,
                'discount_total': discount
            }),
                                content_type='application/json')
        else:
            error = cart_formset.errors[0]['quantity'][0]
            cart_formset = CartItemFormSet(instance=request.cart)
            messages.info(request, _(error))
            #cart_formset._errors = errors
            return HttpResponse(json.dumps({'error': error}),
                                content_type='application/json')
    else:
        return HttpResponse('Sth went wrong.')
Exemplo n.º 9
0
def cart(request, template="shop/cart.html"):
    """
    Display cart and handle removing items from the cart.
    """
    if 'delivery min' in request.session:
        delivery_min = request.session['delivery min']
    else:
        delivery_min = []

    if 'store slug' in request.session:
	store_slug = request.session['store slug']
    else:
	store_slug = '/shop/'

    address = request.session['address']
    current_store = []
    if 'cart loaded' in request.session:
        current_store = request.session['stores'][0]

    cart_formset = CartItemFormSet(instance=request.cart)
    discount_form = DiscountForm(request, request.POST or None)

    tax_handler(request, None)

    if request.method == 'POST':
        tipform = TipForm(request.POST)
        if tipform.is_valid():
            tip_percent = tipform.cleaned_data['tip']
	    tip = 0.01*float(tip_percent)*float(request.cart.total_price())

            billship_handler(request, tip)
#    	    tax_handler(request, None)
            request.session['tip fixed'] = True
    else:
        tipform = TipForm()

    if 'shop_checkout' in request.POST:
                return redirect('/shop/checkout')
    elif request.method == "POST":
        valid = True
        if request.POST.get("update_cart"):
            valid = request.cart.has_items()
            if not valid:
                # Session timed out.
                if 'stores' in request.session:
                    del request.session['stores']
                if 'cart loaded' in request.session:
                    del request.session['cart loaded']
                info(request, _("Your cart has expired"))
		return redirect('/shop/')
            else:
                cart_formset = CartItemFormSet(request.POST,
                                               instance=request.cart)
                valid = cart_formset.is_valid()
                if valid:
                    cart_formset.save()
                    recalculate_cart(request)
                    info(request, _("Cart updated"))
                else:
                    # Reset the cart formset so that the cart
                    # always indicates the correct quantities.
                    # The user is shown their invalid quantity
                    # via the error message, which we need to
                    # copy over to the new formset here.
                    errors = cart_formset._errors
                    cart_formset = CartItemFormSet(instance=request.cart)
                    cart_formset._errors = errors
        else:
            valid = discount_form.is_valid()
            if valid:
                discount_form.set_discount()

        if valid:
            total_quantity, number_forms, number_items_removed = 0, 0, 0
            for form in cart_formset:
                number_forms += 1
                if form.is_valid():
                    was_item_removed = form.cleaned_data["DELETE"]
                    if was_item_removed:
                        number_items_removed += 1
                    total_quantity += form.cleaned_data["quantity"]
            if number_forms==number_items_removed:
                if 'stores' in request.session:
                    del request.session['stores']
                if 'cart loaded' in request.session:
                    del request.session['cart loaded']
		return redirect('/shop/')
            elif total_quantity==0:
                if 'stores' in request.session:
                    del request.session['stores']
                if 'cart loaded' in request.session:
                    del request.session['cart loaded']
#            return redirect("shop_cart")
	    	return redirect('/shop/')

    ten_percent = 0.1*float(request.cart.total_price())
    suggested_tip = '%.2f' % float((ten_percent>2.0)*ten_percent+(ten_percent <=2.0)*2.0)
    if 'tip fixed' in request.session:
        tip_fixed = True
    else:
        tip_fixed = False
    context = {"cart_formset": cart_formset, "delivery_min": delivery_min, "tipform": tipform,
               "suggested_tip": suggested_tip, "tip_fixed": tip_fixed, "store_slug": store_slug,
	       "address": address, "stores": current_store}
    settings.use_editable()
    if (settings.SHOP_DISCOUNT_FIELD_IN_CART and
        DiscountCode.objects.active().count() > 0):
        context["discount_form"] = discount_form
    return render(request, template, context)