Exemple #1
0
def checkout(request):
	quantityChanged = False
	userProfile = UserProfile.objects.get(user=request.user)
	if not request.session.get(settings.CART_KEY):
		return redirect('/cart')
	cart = request.session[settings.CART_KEY]
	if not cart:
		request.flash['message'] = 'No items in cart to checkout with.'
		return redirect('/cart', context_instance=RequestContext(request))
	lineItems = line_items_from_cart(request)
	for li in lineItems:
		li.item = Item.objects.get(pk=li.item_id)
		if li.item.onHand < li.quantity:
			quantityChanged = True
			li.quantity = li.item.onHand
			request.flash[li.item.name] = 'Quantity on hand for %s is less than ordered, order changed to the number on hand.' % li.item.name
	if quantityChanged:
		request = update_cart_with_list_line_item(request, lineItems)
		return redirect('/cart', context_instance=RequestContext(request))
	order = Order(userProfile=userProfile)
	order.save()
	for li in lineItems:
		li_save = LineItem(order=order,item=li.item, quantity=li.quantity)
		li_save.save()
		update_item = Item.objects.get(pk=li.item.id)
		update_item.onHand -= li.quantity
		update_item.save()
	request.session[settings.CART_KEY] = None
	return render_to_response('checkout.html', {'order': order}, context_instance=RequestContext(request))
Exemple #2
0
def view_cart(request):
	items = line_items_from_cart(request)
	return render_to_response('cart.html', 
							  {'items' : items},
							  context_instance=RequestContext(request))