class CheckoutTestCase(TestCase): def setUp(self): self.client = Client() home_url = urlresolvers.reverse('catalog_home') self.checkout_url = urlresolvers.reverse('checkout') self.client.get(home_url) # need to create customer with a shopping cart first self.item = CartItem() product = Product.active.all()[0] self.item.product = product self.item.cart_id = self.client.session[cart.CART_ID_SESSION_KEY] self.item.quantity = 1 self.item.save() def test_checkout_page_empty_cart(self): """ empty cart should be redirected to cart page """ client = Client() cart_url = urlresolvers.reverse('show_cart') response = client.get(self.checkout_url) self.assertRedirects(response, cart_url) def test_submit_empty_form(self): """ empty form should raise error on required fields """ form = CheckoutForm() response = self.client.post(self.checkout_url, form.initial) for name, field in form.fields.iteritems(): value = form.fields[name] if not value and form.fields[name].required: error_msg = form.fields[name].error_messages['required'] self.assertFormError(response, "form", name, [error_msg])6
def setUp(self): self.client = Client() home_url = urlresolvers.reverse('catalog_home') self.checkout_url = urlresolvers.reverse('checkout') self.client.get(home_url) # need to create customer with a shopping cart first self.item = CartItem() product = Product.active.all()[0] self.item.product = product self.item.cart_id = self.client.session[cart.CART_ID_SESSION_KEY] self.item.quantity = 1 self.item.save()
def add_to_cart(request): postdata = request.POST.copy() # get product slug from post data, return blank if empty product_slug = postdata.get('product_slug','') # get quantity added, return 1 if empty quantity = postdata.get('quantity', 1) # fetch the product or return a missing page error p = get_object_or_404(Product, slug=product_slug) # get products in cart cart_products = get_cart_items(request) product_in_cart = False # check to see if item is already in cart for cart_item in cart_products: if cart_item.product.id == p.id: # update the quantity if found cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: # create and save a new cart item ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()