def minus_quantity(request): cart = Cart(request.session) product = Submenu.objects.get(id=request.GET.get('dish_id')) quantity = request.GET.get('quantity') quantity = int(quantity) cart.set_quantity(product, quantity - 1) return HttpResponse("Quantity decreased")
def update(request): if request.is_ajax(): if request.method == 'POST': cart = Cart(request.session) producto = Product.objects.get(id=request.POST.get('id')) new_cantidad = request.POST.get('valor') cart.set_quantity(producto,quantity=new_cantidad) return HttpResponse('Actualizado')
def show(request): cart = Cart(request.session) if request.method == 'POST': print 'mpike mesa sto post' isbn = request.POST.get("book_isbn",'') quant = request.POST.get("book_new_quant",'') product = Book.objects.get(isbn=isbn) cart.set_quantity(product, quantity=quant) return render(request, 'shopping/show-cart.html')
def show(request): cart = Cart(request.session) if request.method == 'POST': quantitylist = request.POST.getlist('quantity[]') for idx, item in enumerate(cart.products): item = Item.objects.get(id=item.id) cart.set_quantity(item, quantitylist[idx]) return redirect(reverse('checkout:page')) return render(request, 'shopping/show-cart.html')
def update_cart(request): if request.method == 'POST': if request.is_ajax(): cart = Cart(request.session) quantity = request.POST.get('quantity') goods = Goods.objects.get(id=request.POST.get('good')) cart.set_quantity(goods, quantity) return JsonResponse( {'message': 'update {} for {}'.format(goods.name, quantity)})
def decrementar_quantidade(request): produto_id = request.POST['produto'] quantidade = request.POST['quantidade'] carrinho = Cart(request.session) produto = Produto.objects.get(pk=produto_id) carrinho.set_quantity(produto, quantity=quantidade) template = loader.get_template('loja/carrinho.html') context = RequestContext(request) return HttpResponse(template.render(context))
def cart_update(request): if request.is_ajax() and request.POST and 'data' in request.POST: cart = Cart(request.session) json_data = json.loads(request.POST.get('data')) for i in json_data: product = models.Part.objects.get(slug=i['part']) cart.set_quantity(product, quantity=i['quantity']) return HttpResponse("Successfully updated cart") else: return HttpResponse(status=400, content="Error updating cart")
def add(request): cart = Cart(request.session) pk = request.POST.get('pk') quantity = request.POST.get('quantity') if quantity == '': message = ' 수량을 선택해주세요.' ctx = { 'message': message, } return JsonResponse(ctx) else: product = Product.objects.get(id=pk) if not product.category == 'EXTRA': cart.add(product, price=product.price, quantity=quantity) total_count = cart.count message = ' 상품을 장바구니에 담았습니다.' ctx = { 'message': message, 'total_count': total_count, } return JsonResponse(ctx) elif product.title == '13oz컵 • 홀더 • 뚜겅 세트': cart.add(product, price=product.price, quantity=quantity) total_count = cart.count message = ' 상품을 장바구니에 담았습니다.' ctx = { 'message': message, 'total_count': total_count, } return JsonResponse(ctx) else: cart.add(product, price=product.price, quantity=quantity) for cart_item in cart.items: if cart_item.product.title == product.title: if cart_item.quantity > 5: cart.set_quantity(product, quantity=5) total_count = cart.count message = ' 상품은 최대 5개까지 구매하실 수 있습니다.' ctx = { 'message': message, 'total_count': total_count, } return JsonResponse(ctx) else: total_count = cart.count message = ' 상품을 장바구니에 담았습니다.' ctx = { 'message': message, 'total_count': total_count, } return JsonResponse(ctx)
def post(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) cart = Cart(request.session) product = get_object_or_404(Product, id=serializer.data['product_id']) cart.set_quantity(product, quantity=serializer.data['product_quantity']) response_data = { 'detail': 'Product was updated.' } response_status = status.HTTP_201_CREATED return Response(response_data, status=response_status)
class CartTests(TestCase): def setUp(self): engine = import_module(settings.SESSION_ENGINE) self.session = engine.SessionStore() self.session.save() self.client.cookies[ settings.SESSION_COOKIE_NAME] = self.session.session_key self.factory = RequestFactory() self.cart = Cart(self.session) self.deer = Product.objects.create( name='deer', price=10.0, custom_id=1, ) self.moose = Product.objects.create( name='moose', price=20.0, custom_id=2, ) self.ticket = Ticket.objects.create( name='Clint Mansell', price=25.0, custom_id=6, ) def show_response(self): """ Utility method to return a response from the `show` view. """ request = self.factory.get(reverse('carton-tests-show')) request.session = self.session return show(request) def test_product_is_added(self): """ Can a product be added to the cart? """ self.cart.add(self.deer, 10.00, 1) self.session.save() self.assertContains(self.show_response(), '1 deer for $10.0') def test_multiple_products_are_added(self): """ Can multiple products be added to the cart? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.moose, 20.00, 1) self.session.save() response = self.show_response() self.assertContains(response, '1 deer for $10.0') self.assertContains(response, '1 moose for $20.0') def test_stale_item_is_removed_from_cart(self): """ Are items which are not in the database kept out of the cart? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.moose, 20.00, 1) self.session.save() response = self.show_response() self.assertContains(response, 'deer') self.assertContains(response, 'moose') self.deer.delete() response = self.show_response() self.assertNotContains(response, 'deer') self.assertContains(response, 'moose') def test_quantity_increases(self): """ Do multiple calls to `add` increase the quantity in the cart? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.deer, 10.00, 2) self.session.save() self.assertContains(self.show_response(), '3 deer') def test_items_are_counted_properly(self): """ Are items in the cart counted correctly? """ self.cart.add(self.deer, 10.00, 2) self.cart.add(self.moose, 20.00, 1) self.session.save() response = self.show_response() self.assertContains(response, 'items count: 3') self.assertContains(response, 'unique count: 2') def test_products_are_removed_all_together(self): """ Can all products of a single type be removed? """ self.cart.add(self.deer, 10.00, 3) self.cart.add(self.moose, 20.00, 1) self.session.save() self.cart.remove(self.deer) response = self.show_response() self.assertNotContains(response, 'deer') self.assertContains(response, 'moose') def test_single_product_is_removed(self): """ Can a single instance of a product be removed? """ self.cart.add(self.deer, 10.00, 3) self.session.save() self.cart.remove_single(self.deer) self.assertContains(self.show_response(), '2 deer') def test_quantity_is_overwritten(self): """ Can an items quantity be changed? """ self.cart.add(self.deer, 10.00, 3) self.session.save() self.cart.set_quantity(self.deer, 4) self.assertContains(self.show_response(), '4 deer') def test_cart_items_are_cleared(self): """ Can a cart be entirely cleared? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.moose, 20.00, 1) self.cart.clear() self.session.save() response = self.show_response() self.assertNotContains(response, 'deer') self.assertNotContains(response, 'moose') @override_settings(CART_PRODUCT_LOOKUP={'price__gt': 1}) def test_custom_product_filter_are_applied(self): """ We modify the cart queryset to exclude some products. We should not be able to add excluded products to the cart. """ exclude = Product.objects.create( name='EXCLUDED', price=0.99, custom_id=100, ) self.cart.add(self.deer, 10.00, 1) self.cart.add(exclude, exclude.price, 1) self.session.save() response = self.show_response() self.assertNotContains(response, 'EXCLUDE') self.assertContains(response, 'deer') def test_get_model_product_path(self): """ Is a string with the full, correct import path returned for a model? """ self.assertEqual( self.cart.get_product_model_path(self.deer), 'carton.tests.models.Product', ) def test_different_product_models(self): """ Does the cart work with different models? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.ticket, 25.00, 1) self.session.save() response = self.show_response() self.assertContains(response, '1 deer for $10.0') self.assertContains(response, '1 Clint Mansell for $25.0')
class CartTests(TestCase): def setUp(self): engine = import_module(settings.SESSION_ENGINE) self.session = engine.SessionStore() self.session.save() self.client.cookies[settings.SESSION_COOKIE_NAME] = self.session.session_key self.factory = RequestFactory() self.cart = Cart(self.session) self.deer = Product.objects.create( name='deer', price=10.0, custom_id=1, ) self.moose = Product.objects.create( name='moose', price=20.0, custom_id=2, ) self.ticket = Ticket.objects.create( name='Clint Mansell', price=25.0, custom_id=6, ) def show_response(self): """ Utility method to return a response from the `show` view. """ request = self.factory.get(reverse('carton-tests-show')) request.session = self.session return show(request) def test_product_is_added(self): """ Can a product be added to the cart? """ self.cart.add(self.deer, 10.00, 1) self.session.save() self.assertContains(self.show_response(), '1 deer for $10.0') def test_multiple_products_are_added(self): """ Can multiple products be added to the cart? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.moose, 20.00, 1) self.session.save() response = self.show_response() self.assertContains(response, '1 deer for $10.0') self.assertContains(response, '1 moose for $20.0') def test_stale_item_is_removed_from_cart(self): """ Are items which are not in the database kept out of the cart? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.moose, 20.00, 1) self.session.save() response = self.show_response() self.assertContains(response, 'deer') self.assertContains(response, 'moose') self.deer.delete() response = self.show_response() self.assertNotContains(response, 'deer') self.assertContains(response, 'moose') def test_quantity_increases(self): """ Do multiple calls to `add` increase the quantity in the cart? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.deer, 10.00, 2) self.session.save() self.assertContains(self.show_response(), '3 deer') def test_items_are_counted_properly(self): """ Are items in the cart counted correctly? """ self.cart.add(self.deer, 10.00, 2) self.cart.add(self.moose, 20.00, 1) self.session.save() response = self.show_response() self.assertContains(response, 'items count: 3') self.assertContains(response, 'unique count: 2') def test_products_are_removed_all_together(self): """ Can all products of a single type be removed? """ self.cart.add(self.deer, 10.00, 3) self.cart.add(self.moose, 20.00, 1) self.session.save() self.cart.remove(self.deer) response = self.show_response() self.assertNotContains(response, 'deer') self.assertContains(response, 'moose') def test_single_product_is_removed(self): """ Can a single instance of a product be removed? """ self.cart.add(self.deer, 10.00, 3) self.session.save() self.cart.remove_single(self.deer) self.assertContains(self.show_response(), '2 deer') def test_quantity_is_overwritten(self): """ Can an items quantity be changed? """ self.cart.add(self.deer, 10.00, 3) self.session.save() self.cart.set_quantity(self.deer, 4) self.assertContains(self.show_response(), '4 deer') def test_cart_items_are_cleared(self): """ Can a cart be entirely cleared? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.moose, 20.00, 1) self.cart.clear() self.session.save() response = self.show_response() self.assertNotContains(response, 'deer') self.assertNotContains(response, 'moose') @override_settings(CART_PRODUCT_LOOKUP={'price__gt': 1}) def test_custom_product_filter_are_applied(self): """ We modify the cart queryset to exclude some products. We should not be able to add excluded products to the cart. """ exclude = Product.objects.create( name='EXCLUDED', price=0.99, custom_id=100, ) self.cart.add(self.deer, 10.00, 1) self.cart.add(exclude, exclude.price, 1) self.session.save() response = self.show_response() self.assertNotContains(response, 'EXCLUDE') self.assertContains(response, 'deer') def test_get_model_product_path(self): """ Is a string with the full, correct import path returned for a model? """ self.assertEqual( self.cart.get_product_model_path(self.deer), 'carton.tests.models.Product', ) def test_different_product_models(self): """ Does the cart work with different models? """ self.cart.add(self.deer, 10.00, 1) self.cart.add(self.ticket, 25.00, 1) self.session.save() response = self.show_response() self.assertContains(response, '1 deer for $10.0') self.assertContains(response, '1 Clint Mansell for $25.0')
def set_quantity(request): cart = Cart(request.session) product = Product.objects.get(pk=request.POST.get('product_id')) quantity = request.POST.get('quantity') cart.set_quantity(product, quantity) return HttpResponse()
def set_quantity(request): cart = Cart(request.session) product = products[int(request.POST.get('product_id'))] quantity = request.POST.get('quantity') cart.set_quantity(product, quantity) return HttpResponse()