Slide 30
Slide 30 text
def
update_quantity(cart_id,
sku,
old_qty,
new_qty):
now
=
datetime.utcnow()
delta_qty
=
new_qty
-‐
old_qty
#
Make
sure
the
cart
is
still
active
and
add
the
line
item
result
=
db.cart.update(
{'_id':
cart_id,
'status':
'active',
'items.sku':
sku
},
{'$set':
{
'last_modified':
now,
'items.$.qty':
new_qty
},
},
safe=True)
if
not
result['updatedExisting']:
raise
CartInactive()
#
Update
the
inventory
result
=
db.inventory.update(
{'_id':sku,
'carted.cart_id':
cart_id,
'qty':
{'$gte':
delta_qty}
},
{'$inc':
{'qty':
-‐delta_qty
},
'$set':
{
'carted.$.qty':
new_qty,
'timestamp':
now
}
},
safe=True)
if
not
result['updatedExisting']:
#
Roll
back
our
cart
update
db.cart.update(
{'_id':
cart_id,
'items.sku':
sku
},
{'$set':
{
'items.$.qty':
old_qty
}
})
raise
InadequateInventory()
Modifying
quantities