本文整理汇总了Python中cart.Cart.add_item方法的典型用法代码示例。如果您正苦于以下问题:Python Cart.add_item方法的具体用法?Python Cart.add_item怎么用?Python Cart.add_item使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cart.Cart
的用法示例。
在下文中一共展示了Cart.add_item方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestRemoveFromCart
# 需要导入模块: from cart import Cart [as 别名]
# 或者: from cart.Cart import add_item [as 别名]
class TestRemoveFromCart(unittest.TestCase):
def setUp(self):
self.products = []
self.cart = Cart()
for i in range(1,10):
product = Product(product_id=i, key_name="test_product_"+str(i),price=round(random.uniform(1,10),2),tax_percent=18.00)
product.put()
product.add_stock("test_product_"+str(i),10)
self.products.append("test_product_"+str(i))
p = Product.get_by_key_name("test_product_"+str(i))
self.cart.add_item(CartItem(p), random.randint(1,10))
random.shuffle(self.products)
def test_totals_with_delivery(self):
print '----------------- TEST RfB TOTALS --------------------'
self.cart.delivery = {'price':2.5,'tax':15.0,'name':'testDelivery'}
self.cart.recalc()
delivery_gross = self.cart.delivery['price'] + self.cart.delivery['price'] * self.cart.delivery['tax']/100
delivery_tax = self.cart.delivery['price'] * self.cart.delivery['tax'] / 100
delivery_net = self.cart.delivery['price']
print 'DELIVERY ','price:', self.cart.delivery['price'], 'plus tax:', delivery_gross, 'tax percent:', self.cart.delivery['tax'],'price tax:', delivery_tax
sub_total_tax_items = self.cart.sub_total_tax_items #TAX Items
sub_total_price = self.cart.sub_total_price #GROSS Items
total = self.cart.total #GROSS Items + GROSS Delivery
total_without_tax = self.cart.total_without_tax #NET Items + NET Delivery
total_tax = self.cart.total_tax #TAX items + TAX Delivery
for i in range(3):
qty = random.randint(1,10)
qty=1
p = Product.get_by_key_name(self.products[i])
print self.products[i],'price:', p.price, 'plus tax:', p.price + p.price * p.tax_percent / 100,'tax percent: ',p.tax_percent, 'price tax:',p.price * p.tax_percent / 100, 'qty:',qty
sub_total_tax_items -= (p.price * p.tax_percent / 100)*qty
sub_total_price -= (p.price + p.price * p.tax_percent / 100)*qty
total -= (p.price + p.price * p.tax_percent / 100)*qty
total_without_tax -= (p.price)*qty
total_tax -= (p.price * p.tax_percent / 100)*qty
self.cart.remove_item(CartItem(p),qty)
print 'sub_total_tax_items: ', sub_total_tax_items, ' CART sub_total_tax_items: ',self.cart.sub_total_tax_items
self.assertAlmostEqual(sub_total_tax_items, self.cart.sub_total_tax_items,7, 'Error calculating sub_total_tax_items')
print 'sub_total_price: ', sub_total_price, ' CART sub_total_price: ',self.cart.sub_total_price
self.assertEqual(round(sub_total_price,2), round(self.cart.sub_total_price,2), 'Error calculating sub_total_price')
print 'total: ', total, ' CART total: ',self.cart.total
self.assertEqual(round(total,2), round(self.cart.total,2), 'Error calculating total')
print 'total_without_tax: ', total_without_tax, ' CART total_without_tax: ',self.cart.total_without_tax
self.assertEqual(round(total_without_tax,2), round(self.cart.total_without_tax,2), 'Error calculating total_without_tax')
print 'total_tax: ', total_tax, ' CART total_tax: ',self.cart.total_tax
self.assertEqual(round(total_tax,2), round(self.cart.total_tax,2), 'Error calculating total_tax')
print '----------------- END TEST -----------------------'
示例2: CartTest
# 需要导入模块: from cart import Cart [as 别名]
# 或者: from cart.Cart import add_item [as 别名]
class CartTest(unittest.TestCase):
def setUp(self):
self.cart = Cart()
self.inv = Inventory()
def tearDown(self):
self.inv.close()
def test_add_items_to_basket(self):
# Test adding item, make sure it's in cart and inventory updates to reflect that
old_qty= self.inv.view_qty('item050')
self.cart.add_item('item050', 10)
self.assertEqual(self.cart.basket('item050'), 10)
self.assertEqual(self.inv.view_qty('item050'), old_qty-10)
self.inv.update_qty('item050', 500)
# Test adding more to cart than inventory contains
self.cart._basket['item050'] = 0 # Temporary kluge
self.cart.add_item('item050', 600)
self.assertEqual(self.cart.basket('item050'), 500)
self.assertEqual(self.inv.view_qty('item050'), 0)
self.inv.update_qty('item050', 500)
def test_remove_items_from_basket(self):
self.cart.add_item('item050', 10)
self.cart.remove_item('item050',5)
self.assertEqual(self.cart.basket('item050'), 5)
self.assertEqual(self.inv.view_qty('item050'), 495)
def test_edit_items(self):
# Change to a quantity greater than you already have
self.cart.edit_item('item050', 50)
self.cart.edit_item('item050', 70)
self.assertEqual(self.cart.basket('item050'), 70)
self.assertEqual(self.inv.view_qty('item050'), 430)
self.cart.remove_item('item050', 70)
# Add more than are in inventory
self.cart.edit_item('item050', 1000)
self.assertEqual(self.inv.view_qty('item050'), 0)
self.assertEqual(self.cart.basket('item050'), 500)
self.cart.remove_item('item050', 500)
# Change to a quantity less than what you already have
self.cart.add_item('item050', 100)
self.cart.edit_item('item050', 50)
self.assertEqual(self.inv.view_qty('item050'), 450)
self.assertEqual(self.cart.basket('item050'), 50)
self.cart.remove_item('item050', 50)
def test_items_total(self):
self.cart.add_item('item100', 100)
cost = 100 * self.inv.view_price('item100')
self.assertEqual(self.cart.items_total(), cost)
self.cart.remove_item('item100', 100)
def test_list_items(self):
self.cart.add_item('item100', 100)
# Test listing sorted by item name
for _tuple in self.cart.list_items('item'):
self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
# Test listing sorted by qty
for _tuple in self.cart.list_items('price'):
self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
# Test listing sorted by price
self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100')))
self.cart.remove_item('item100', 100)
示例3: TestAddToCart
# 需要导入模块: from cart import Cart [as 别名]
# 或者: from cart.Cart import add_item [as 别名]
class TestAddToCart(unittest.TestCase):
def setUp(self):
self.products = []
for i in range(1,10):
product = Product(product_id=i, key_name="test_product_"+str(i),price=round(random.uniform(1,10),2),tax_percent=18.00)
product.put()
product.add_stock("test_product_"+str(i),10)
self.products.append("test_product_"+str(i))
self.cart = Cart()
random.shuffle(self.products)
def test_price_cart(self):
#3 random products in cart
value = 0.0
for i in range(3):
p = Product.get_by_key_name(self.products[i])
#print p.price,
value += p.price
self.cart.add_item(CartItem(p),1)
#print value, self.cart.total_without_tax, self.cart.sub_total_tax, self.cart.sub_total_price, self.cart.total
self.assertEqual(round(value,2), round(self.cart.total_without_tax,2), 'price not matching')
def test_price_tax_cart(self):
value_tax_included = 0.0
for i in range(3):
p = Product.get_by_key_name(self.products[i])
cart_item = CartItem(p)
qty = random.randint(1,10)
#qty = 1
value_tax_included += cart_item.final_price*qty
self.cart.add_item(cart_item, qty)
#print value_tax_included, self.cart.total_without_tax, self.cart.sub_total_tax, self.cart.sub_total_price, self.cart.total
self.assertEqual(round(value_tax_included,2), round(self.cart.sub_total_price,2), 'taxes not being calculated well')
def test_reserved_stock(self):
#3 random products in cart
value = 0.0
result = 0
items_no = 0
for i in range(3):
qty = random.randint(1,10)
print 'product: ', self.products[i], ' to reserve: ',qty,
if Product.reserve(self.products[i],qty):
p = Product.get_by_key_name(self.products[i])
#print p.stock, p.reserved
self.cart.add_item(CartItem(p),qty)
result += qty
items_no +=1
#p = Product.get_by_key_name(self.products[i])
print self.cart.total_different_items, items_no
self.assertEqual(self.cart.total_different_items, items_no, 'Error in total different items')
self.assertEqual(self.cart.total_items, result, 'Error in total_items')
def test_totals_with_delivery(self):
print '----------------- TEST AtB TOTALS --------------------'
self.cart.delivery = {'price':2.5,'tax':15.0,'name':'testDelivery'}
delivery_gross = self.cart.delivery['price'] + self.cart.delivery['price'] * self.cart.delivery['tax']/100
delivery_tax = self.cart.delivery['price'] * self.cart.delivery['tax'] / 100
delivery_net = self.cart.delivery['price']
print 'DELIVERY ','price:', self.cart.delivery['price'], 'plus tax:', delivery_gross, 'tax percent:', self.cart.delivery['tax'],'price tax:', delivery_tax
sub_total_tax_items = 0 #TAX Items
sub_total_price = 0 #GROSS Items
total = delivery_gross #GROSS Items + GROSS Delivery
total_without_tax = delivery_net #NET Items + NET Delivery
total_tax = delivery_tax #TAX items + TAX Delivery
for i in range(3):
qty = random.randint(1,10)
p = Product.get_by_key_name(self.products[i])
print self.products[i],'price:', p.price, 'plus tax:', p.price + p.price * p.tax_percent / 100,'tax percent: ',p.tax_percent, 'price tax:',p.price * p.tax_percent / 100, 'qty:',qty
sub_total_tax_items += (p.price * p.tax_percent / 100)*qty
sub_total_price += (p.price + p.price * p.tax_percent / 100)*qty
total += (p.price + p.price * p.tax_percent / 100)*qty
total_without_tax += (p.price)*qty
total_tax += (p.price * p.tax_percent / 100)*qty
self.cart.add_item(CartItem(p),qty)
print 'sub_total_tax_items: ', sub_total_tax_items, ' CART sub_total_tax_items: ',self.cart.sub_total_tax_items
self.assertEqual(round(sub_total_tax_items,2), round(self.cart.sub_total_tax_items,2), 'Error calculating sub_total_tax_items')
print 'sub_total_price: ', sub_total_price, ' CART sub_total_price: ',self.cart.sub_total_price
self.assertEqual(round(sub_total_price,2), round(self.cart.sub_total_price,2), 'Error calculating sub_total_price')
print 'total: ', total, ' CART total: ',self.cart.total
self.assertEqual(round(total,2), round(self.cart.total,2), 'Error calculating total')
print 'total_without_tax: ', total_without_tax, ' CART total_without_tax: ',self.cart.total_without_tax
self.assertEqual(round(total_without_tax,2), round(self.cart.total_without_tax,2), 'Error calculating total_without_tax')
print 'total_tax: ', total_tax, ' CART total_tax: ',self.cart.total_tax
self.assertEqual(round(total_tax,2), round(self.cart.total_tax,2), 'Error calculating total_tax')
#.........这里部分代码省略.........
示例4: get
# 需要导入模块: from cart import Cart [as 别名]
# 或者: from cart.Cart import add_item [as 别名]
def get(self, subdomain=None):
#args
item = self.request.get('item','-')
qty = int(self.request.get('qty',1))
'''NAMESPACE CHANGE (memcache and datastore)'''
namespace = namespace_manager.get_namespace()
namespace_manager.set_namespace(subdomain)
'''Name of the memcache'''
logging.info(str(self.session))
#cookie_value = self.request.cookies.get('shopapp-cart')
#logging.info(str(cookie_value))
'''logging.info(cookie_value)
if not cookie_value:
#random number
import random, string
cookie_value = ''.join(random.choice(string.ascii_uppercase + '123456789') for i in xrange(10))
self.response.set_cookie(key='shopapp-cart', value=cookie_value,max_age=900)
logging.info(str(self.request.cookies.get('shopapp-cart')))'''
#self.response.set_cookie(key='guille', value='el mejor',max_age=900)
self.session = self.session_store.get_session(name='shopapp',max_age=None,backend='memcache')
logging.info(str(self.session))
#15 mins session (900 secs), if a previous session does not exist it creates a new one
#self.session = self.session_store.get_session(name='mc_session',max_age=900,backend='memcache')
#logging.info(cookie_value)
try:
cart = self.session['cart']
logging.info('cart found in memcache :)')
except:
cart = Cart()
logging.info('cart not found in memcache :(')
#return self.response.write(str(self.session['cart'].__dict__))
'''import random
for i in range(1,10):
product = Product(product_id=i, key_name="test_product_"+str(i),price=round(random.uniform(1,10),2),tax_percent=18.00)
product.put()
product.add_stock("test_product_"+str(i),10)'''
product = Product.get_by_key_name(item)
if not product: return self.response.write('The product does not exist')
elif product.reserve(qty):
'''Checked stock'''
cart.add_item(CartItem(product),qty)
else:
self.response.write('Not enough products in the warehouse')
self.session['cart'] = cart
self.session_store.save_sessions(self.response)
'''NAMESPACE CHANGE'''
namespace_manager.set_namespace(namespace)
return self.response.write(str(cart))