def post(self, request, slug): cart = Cart(request) product = Product.objects.get(slug=slug) quantity = request.POST.get('quantity') options = ProductOptions.objects.filter( id__in=request.POST.getlist('options')) cart.add(product, options, quantity) return HttpResponse(status=200)
def cart_add(request, product_id): cart = Cart(request) product = get_object_or_404(Product, id=product_id) form = CartAddProductForm(request.POST) if form.is_valid(): cd = form.cleaned_data cart.add(product=product, quantity=cd['quantity'], update_quantity=cd['update']) return redirect('cart_detail')
def add_cart(request, slug): cart = Cart(request) item = get_object_or_404(Item, slug=slug) form = CartAddProductForm(request.POST) if form.is_valid(): clean = form.cleaned_data cart.add(item=item, quantity=clean['quantity'], update_quantity=clean['update']) return redirect('cart_detail')
def cart_add(request, product_id): cart = Cart(request) product = get_object_or_404(Product, id=product_id) form = CartAddProductForm(request.POST) if form.is_valid(): cd = form.cleaned_data cart.add(product=product, quantity=cd['quantity'], update_quantity=cd['update']) messages.info(request, _("You have added {} to your cart.").format(product)) return redirect('shop:cart_detail')
def registration_complete(request, slug): pk = request.session.get("reg_id") registry = Registry.objects.get(pk=pk) event = registry.event order = Cart(request) meal_order_formset = None msgs = [] if event.offers_meals: meals = event.meals.all() meal_order_formset = ServiceOrderFormSet( request.POST or None, initial=[{"id":meal.id, "unit_price":meal.price, "name":meal.description} for meal in meals] ) if request.method == "POST": if meal_order_formset.is_valid(): meals_by_id = dict([[meal.id, meal] for meal in meals]) for form in meal_order_formset: quantity = int(form.cleaned_data['quantity']) if quantity > 0: meal_id = int(form.cleaned_data['id']) meal = meals_by_id[meal_id] try: order.add(meal, meal.price, quantity, meal.description) except OrderAlreadyCheckedout: return redirect("payment", order.cart.id) return redirect("checkout") return render(request, "evreg/registration_completed.html", {"registry": registry, "event": event, "meal_order_formset": meal_order_formset, "msgs": msgs, "order": order })
def test_order_from_cart(self): """ Tests that 1 + 1 always equals 2. """ self.assertEqual(1 + 1, 2) client = Client.objects.get(pk=1) self.assertEqual(client.email, '*****@*****.**') c = Cart() c.add((12, 37, 2)) c.add((1, None, 1)) l = cart_list(c) order = order_from_cart(c, client, 'paypal') self.assertEqual(len(order.line_set.all()), 3) # this test fails because not shipping costs ship_cost = Decimal(calc_shipping_costs(c, '08695', cart_total(l))) self.assertEqual(order.total(), cart_total(l) + ship_cost ) self.assertEqual(order.line_set.all()[2].types, 'ship' ) self.assertEqual(order.pay_type, 'paypal' )
def test_order_from_cart(self): """ Tests that 1 + 1 always equals 2. """ self.assertEqual(1 + 1, 2) client = Client.objects.get(pk=1) self.assertEqual(client.email, '*****@*****.**') c = Cart() c.add((12, 37, 2)) c.add((1, None, 1)) l = cart_list(c) order = order_from_cart(c, client, 'paypal') self.assertEqual(len(order.line_set.all()), 3) # this test fails because not shipping costs ship_cost = Decimal(calc_shipping_costs(c, '08695', cart_total(l))) self.assertEqual(order.total(), cart_total(l) + ship_cost) self.assertEqual(order.line_set.all()[2].types, 'ship') self.assertEqual(order.pay_type, 'paypal')
def test_cart_list(self): c = Cart() # add to cart el producte 12, quantitat 1 c.add((12, None, 1)) l = cart_list(c) # afegeix b el producte self.assertEqual( 12, l[0][0].pk ) # retorna la quantitat de forma correcta self.assertEqual( 1, float(l[0][2]) ) # retorna el preu correctament self.assertEqual( 24.9, float(l[0][3]) ) # probem amb dos lines amb el mateix producte, ha d'afegir descmpte per quantitat c.add((12, None, 1)) l = cart_list(c) self.assertEqual( 24.9, float(l[0][3]) ) self.assertEqual( 44.9/2, float(l[0][4]) ) # testem que el total d'items de c funciona self.assertEqual( c.total_items(), 2 ) c.add((12, None, 3)) l = cart_list(c) self.assertEqual( c.total_items(), 5 ) self.assertEqual( c.total(), 3 ) self.assertEqual( 79.9/4, float(l[2][4]) ) self.assertEqual( round((79.9/4)*3, 2), round(float(l[2][5]), 2) )
def test_cart_weight(self): c = Cart() # add to cart el producte 12, quantitat 1 c.add((12, None, 1)) weight = cart_weight(c) self.assertEqual(weight, 960) c.add((12, None, 2)) self.assertEqual(cart_weight(c), 960*3) c.add((11, None, 1)) self.assertEqual(cart_weight(c), (960*3)+(2800))
from shop.cart import Cart from drawn import Drawn from random import choice Drawn('http://www.wp.pl') c = Cart() c.add(Drawn('http://www.amazon.co.uk/Doctor-Who-50th-Anniversary-Special/dp/B00F37VHX4/ref=pd_cp_d_h__0')) c.add(Drawn('http://www.amazon.co.uk/Star-Wars-Original-Trilogy-Episodes/dp/B00E0NHP6K/ref=br_lf_m_1000772753_1_2_img?ie=UTF8&s=dvd&pf_rd_p=453915507&pf_rd_s=center-2&pf_rd_t=1401&pf_rd_i=1000772753&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_r=0S4X8JSW469NW7P9RQ7B')) c.add(Drawn('http://www.amazon.com/Star-Trek-VIII-First-Contact/dp/B000IZ8SD8/ref=pd_sim_mov_aiv_7')) #print c.elements[0].item.title for element in c.elements: print element.item.title, element.item.price print "Wybralem dla Ciebie" el = choice(c.elements) print el.item.title