Ejemplo n.º 1
0
def update_cart(request):
    order = Order()
    if request.method == 'POST':
        cart = request.session.get('cart', list())
        if len(cart) == 0:
            return HttpResponseRedirect(reverse(update_cart))
        sub_total = order.calc_sub_total(cart)
        shipping_cost = order.calc_ship_cost(cart)
        total = order.calc_total(cart)
        formset = OrderItemFormSet(data=request.POST, init_instances=cart)
        if formset.is_valid():
            cart = list()
            for form in formset.forms:
                try:                
                    if not form.cleaned_data['DELETE']:
                        order_item = form.instance
                        order_item.quantity = form.cleaned_data['quantity']
                        if order_item.quantity > 0:
                            cart.append(order_item)
                except AttributeError:
                    pass
            request.session['cart'] = cart
            return HttpResponseRedirect(reverse(update_cart))
    else:
        cart = request.session.get('cart', list())
        sub_total = order.calc_sub_total(cart)
        shipping_cost = order.calc_ship_cost(cart)
        total = order.calc_total(cart)
        formset = OrderItemFormSet(init_instances=cart)
    return render_to_response('simple_orders/cart.html', {'shipping_cost': shipping_cost, 'total': total, 'cart_formset': formset, 'sub_total': sub_total}, context_instance = RequestContext(request))
Ejemplo n.º 2
0
def checkout(request):
    order = Order()
    if request.method == 'POST':
        cart = request.session.get('cart', list())
        if len(cart) == 0:
            return HttpResponseRedirect(reverse(checkout))
        form = OrderForm(request.POST)
        order_items = request.session['cart']
        shipping_cost = order.calc_ship_cost(request.session['cart'])
        sub_total = order.calc_sub_total(cart)
        total = order.calc_total(request.session['cart'])
        if form.is_valid():
            if request.user.is_authenticated():
                order = form.save(commit=False)
                order.user = request.user
                order.save()
            else:
                order = form.save()
            for order_item in request.session['cart']:
                order_item.order = order
                order_item.save()
            request.session['cart'] = list()
            request.session['order'] = order
            return HttpResponseRedirect(reverse(order_confirm))
    else:
        cart = request.session.get('cart', list())
        form = OrderForm()
        order_items = cart
        shipping_cost = order.calc_ship_cost(cart)
        sub_total = order.calc_sub_total(cart)
        total = order.calc_total(cart)
    return render_to_response('simple_orders/checkout.html', {'order_form': form, 'order_items': order_items, 'shipping_cost': shipping_cost, 'total': total, 'sub_total': sub_total}, context_instance = RequestContext(request))