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])
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): """ function that takes a POST request and adds a product instance to the current customer's shopping cart """ 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()
def add_to_cart(request): #get the post data from the request and push to dictionary 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 found cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: #create and save a new cart item item = CartItem() item.product = p item.quantity = quantity item.cart_id = _cart_id(request) 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()
def add_to_cart(request): """ function that takes a POST request and adds a product instance to the current customer's shopping cart """ 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()
class CheckoutTestCase(TestCase): """ tests checkout form page functionality """ 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_checkout_page(self): """ with at least one cart item, request for checkout page URL is successful """ response = self.client.get(self.checkout_url) self.assertEqual(response.status_code, httplib.OK) self.assertContains(response, "Checkout") url_entry = urlresolvers.resolve(self.checkout_url) template_name = url_entry[2]['template_name'] self.assertTemplateUsed(response, template_name) def test_submit_empty_form(self): """ empty order form raises 'required' error message for required order form 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])
def add_to_cart(request): postdata = request.POST.copy() product_slug = postdata.get('product_slug','') quantity = postdata.get('quantity',1) p = get_object_or_404(Product, slug=product_slug) cart_products = get_cart_items(request) product_in_cart = False for cart_item in cart_products: if cart_item.product.id == p.id: cart_item.augment_quantity(quantity) product_in_cart = True if not product_in_cart: ci = CartItem() ci.product = p ci.quantity = quantity ci.cart_id = _cart_id(request) ci.save()