From b0a545d16f9f7573a6f82a77ad1f6a2beb106bd6 Mon Sep 17 00:00:00 2001 From: helmutm Date: Sun, 15 Mar 2009 09:37:00 +0000 Subject: [PATCH] allow modification and removal of cart or order items git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3275 fd906abe-77d9-0310-91a1-e0d9ade77398 --- commerce/README.txt | 23 ++++++++++++++++++++++- commerce/interfaces.py | 14 +++++++++++++- commerce/order.py | 23 ++++++++++++++++++----- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/commerce/README.txt b/commerce/README.txt index 2850ae8..37d3dd1 100644 --- a/commerce/README.txt +++ b/commerce/README.txt @@ -88,6 +88,27 @@ A cart is just a collection of order items belonging to a certain customer >>> orderItems.getCart(c001) [] + >>> item1 = orderItems.getCart(c001, shop=shop1, product=p001)[0] + >>> item1 + + + >>> orderItems.add(p003, c001, shop=shop1, quantity=1) + + + >>> len(orderItems.getCart(c001)) + 2 + +If we add the same product again to the cart no new item is created but +the quantity is added to the existing item. + + >>> orderItems.add(p003, c001, shop=shop1, quantity=1) + + >>> len(orderItems.getCart(c001)) + 2 + + >>> item1.remove() + >>> len(orderItems.getCart(c001)) + 1 Orders ------ @@ -105,4 +126,4 @@ retrieving the order items. >>> orderItems.getCart(c001) [] >>> orderItems.getCart(c001, ord001) - [] + [] diff --git a/commerce/interfaces.py b/commerce/interfaces.py index b678eb4..c31a223 100644 --- a/commerce/interfaces.py +++ b/commerce/interfaces.py @@ -345,6 +345,15 @@ class IOrderItem(ITrack): u'items ordered.') fullPrice = Attribute(u'The full price for the quantity ordered.') + def remove(): + """ Remove the order item from the order or cart. + """ + + def modify(quantity, **kw): + """ Change the quantity of the order item (and optionally other + attributes). + """ + def setOrder(order): """ Assign the order given to the order item. """ @@ -367,9 +376,12 @@ class IOrderItems(Interface): product, party, order, run, timeFrom, timeTo. """ - def add(product, party, shop, order='???', run=0, **kw): + def add(product, party, shop, order='???', run=0, quantity=1, **kw): """ Create and register an order item; return it. Additional properties may be specified via keyword arguments. + + If the order item is already present do not create a new one + but add the quantity. """ def getCart(party=None, order='???', shop=None, run=0, **kw): diff --git a/commerce/order.py b/commerce/order.py index c26c2b9..e10e58f 100644 --- a/commerce/order.py +++ b/commerce/order.py @@ -74,6 +74,13 @@ class OrderItem(Track): return (tp, id) return ref + def remove(self): + self.getParent().context.removeTrack(self) + + def modify(self, quantity, **kw): + self.data['quantity'] = quantity + return self + def setOrder(self, order): parent = self.getParent() self.order = parent.getUid(order) @@ -109,11 +116,16 @@ class OrderItems(object): def add(self, product, party, shop, order='???', run=0, **kw): kw['shop'] = self.getUid(shop) - trackId = self.context.saveUserTrack(self.getUid(product), run, - self.getUid(party), kw) - track = self[trackId] - track.order = self.getUid(order) - self.context.indexTrack(0, track, 'order') + existing = self.getCart(party, order, shop, run, product=product) + if existing: + track = existing[-1] + track.modify(track.quantity + kw.get('quantity', 1)) + else: + trackId = self.context.saveUserTrack(self.getUid(product), run, + self.getUid(party), kw) + track = self[trackId] + track.order = self.getUid(order) + self.context.indexTrack(0, track, 'order') return track def getCart(self, party=None, order='???', shop=None, run=None, **kw): @@ -122,6 +134,7 @@ class OrderItems(object): result = self.query(party=party, order=order, **kw) if shop is None: return list(result) + shop = self.getUid(shop) return [item for item in result if item.shop == shop] # utility methods