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
This commit is contained in:
helmutm 2009-03-15 09:37:00 +00:00
parent 091cf37185
commit b0a545d16f
3 changed files with 53 additions and 7 deletions

View file

@ -88,6 +88,27 @@ A cart is just a collection of order items belonging to a certain customer
>>> orderItems.getCart(c001)
[<OrderItem [2, 1, 7, '... ...', '???']: {'shop': 0, 'quantity': 3}>]
>>> item1 = orderItems.getCart(c001, shop=shop1, product=p001)[0]
>>> item1
<OrderItem [2, 1, 7, '... ...', '???']: {'shop': 0, 'quantity': 3}>
>>> orderItems.add(p003, c001, shop=shop1, quantity=1)
<OrderItem [4, 2, 7, '... ...', '???']: {'shop': 0, '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)
<OrderItem [4, 2, 7, '... ...', '???']: {'shop': 0, 'quantity': 2}>
>>> 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)
[<OrderItem [2, 1, 7, '... ...', 11]: {'shop': 0, 'quantity': 3}>]
[<OrderItem [4, 2, 7, '... ...', 11]: {'shop': 0, 'quantity': 2}>]

View file

@ -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):

View file

@ -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