From 746df67c90640afb683b2561488cc56898614185 Mon Sep 17 00:00:00 2001 From: "Nimrod S. Kerrett" Date: Thu, 17 May 2012 22:12:34 +0700 Subject: [PATCH 1/3] update() logic Seems like update() did nothing before. [?!?] Now it adds the item if not found, removes it if quantity is 0, and tries to act sanely when something goes wrong :) --- cart/cart.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cart/cart.py b/cart/cart.py index ff08dc1..5761615 100644 --- a/cart/cart.py +++ b/cart/cart.py @@ -66,9 +66,18 @@ def update(self, product, quantity, unit_price=None): cart=self.cart, product=product, ) + if quantity: + item.quantity = quantity + if unit_price: + item.unit_price = unit_price + else: + item.delete() except models.Item.DoesNotExist: - raise ItemDoesNotExist - + if unit_price: + self.add(product, unit_price, quantity) + else: + raise ItemDoesNotExist + def count(self): result = 0 for item in self.cart.item_set.all(): From 06c079e6d53e7595401d52eee7fb27eaf06520d7 Mon Sep 17 00:00:00 2001 From: "Nimrod S. Kerrett" Date: Fri, 18 May 2012 01:44:05 +0700 Subject: [PATCH 2/3] Oops, forgot the save :s --- cart/cart.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cart/cart.py b/cart/cart.py index 5761615..fcfedbf 100644 --- a/cart/cart.py +++ b/cart/cart.py @@ -70,6 +70,7 @@ def update(self, product, quantity, unit_price=None): item.quantity = quantity if unit_price: item.unit_price = unit_price + item.save() else: item.delete() except models.Item.DoesNotExist: From 547ea66d8dce79841a595576c0766d53c021ea6e Mon Sep 17 00:00:00 2001 From: "Nimrod S. Kerrett" Date: Fri, 25 May 2012 03:26:22 +0700 Subject: [PATCH 3/3] Ignore 0->0 updates (don't add empty item) Turns our users actually do this in real life :) --- cart/cart.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cart/cart.py b/cart/cart.py index fcfedbf..b321113 100644 --- a/cart/cart.py +++ b/cart/cart.py @@ -75,7 +75,8 @@ def update(self, product, quantity, unit_price=None): item.delete() except models.Item.DoesNotExist: if unit_price: - self.add(product, unit_price, quantity) + if quantity: # Maybe user updated 0 to 0. Happens + self.add(product, unit_price, quantity) else: raise ItemDoesNotExist