本文整理汇总了Python中cart.Cart.save方法的典型用法代码示例。如果您正苦于以下问题:Python Cart.save方法的具体用法?Python Cart.save怎么用?Python Cart.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cart.Cart
的用法示例。
在下文中一共展示了Cart.save方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cart_list
# 需要导入模块: from cart import Cart [as 别名]
# 或者: from cart.Cart import save [as 别名]
def cart_list(request, tmpl, action=None, product_id=None):
cart = Cart(request)
msg_ok = msg_err = None
if action == 'del':
if cart.delete(int(product_id)):
msg_ok = _(u"Product successfully removed from cart")
else:
msg_err = _(u"Error while removing product from cart")
elif action == 'add' or action == 'update':
statusok = False
if len(request.POST)>0:
statusok = True
for k,v in request.POST.iteritems():
product_id = 0
try:
quantity = int(v)
except:
quantity = 0
if k.startswith('product_'):
product_id = int(k[8:])
if action == 'add':
statusok = statusok and cart.add(product_id, quantity)
else:
statusok = statusok and cart.update(product_id, quantity)
if k == 'donation':
statusok = statusok and quantity >= 0
if quantity >= 0:
cart.donation = quantity
if statusok:
msg_ok = _(u"Product(s) successfully added") if action == 'add' else _(u"Product(s) successfully updated")
else:
msg_err = _(u"Error while adding product(s)") if action == 'add' else _(u"Error while updating product(s)")
elif action == 'invalid':
msg_err = _(u"Unable to confirm your order, one (or more) product(s) in your cart exceed the available quantity")
elif action == 'uncheckedgcs':
msg_err = _(u"You have to read and accept the general terms and conditions of sales in order to confirm your order")
cart.save(request)
return tmpl, {
'cart': cart,
'msg_err': msg_err, 'msg_ok': msg_ok,
'usegcs': settings.CART_SETTINGS['gcsuse'],
'currency': settings.CURRENCY, 'currency_alt': settings.CURRENCY_ALT,
}
示例2: orders_list
# 需要导入模块: from cart import Cart [as 别名]
# 或者: from cart.Cart import save [as 别名]
def orders_list(request, tmpl, action=None):
msg_ok = msg_err = None
if action == 'validate':
cart = Cart(request)
if not cart.has_gcs_ckecked():
return HttpResponseRedirect('/resa/cart/uncheckedgcs/')
if not cart.is_valid():
return HttpResponseRedirect('/resa/cart/invalid/')
elif not cart.empty():
order = Order(user=request.user, creation_date=date.now(), donation = cart.donation)
order.save_confirm(cart)
cart.clear()
cart.save(request)
msg_ok = _(u"Order successfully confirmed")
pending_orders = request.user.order_set.filter(payment_date__isnull=True)
validated_orders = request.user.order_set.filter(payment_date__isnull=False)
return tmpl, {
'pending_orders': pending_orders,
'validated_orders': validated_orders,
'msg_err': msg_err, 'msg_ok': msg_ok,
'user_obj': request.user,
'currency': settings.CURRENCY, 'currency_alt': settings.CURRENCY_ALT,
}
示例3: TestCart
# 需要导入模块: from cart import Cart [as 别名]
# 或者: from cart.Cart import save [as 别名]
class TestCart(unittest.TestCase):
def setUp(self):
self.session = {}
self.product_one = NonCallableMock(product_id=1, price=1.99)
self.product_two = NonCallableMock(product_id=2, price=2.49)
self.cart = Cart(self.session)
def test_can_add_product(self):
self.cart.add_product(self.product_one, 10)
self.assertEqual(
self.cart.get_cart_item(self.product_one).quantity, 10)
def test_cannot_add_product_twice(self):
self.cart.add_product(self.product_one, 13)
self.assertRaises(
CartError, lambda: self.cart.add_product(self.product_one, 10))
def test_remove_product(self):
self.cart.add_product(self.product_one, 10)
self.cart.remove_product(self.product_one)
self.assertEqual(self.cart.num_items, 0)
self.assertRaises(
CartError, lambda: self.cart.get_cart_item(self.product_one))
def test_cannot_remove_product_not_in_cart(self):
self.assertRaises(
CartError, lambda: self.cart.remove_product(self.product_one))
def test_can_calculate_quantity_of_cart(self):
self.cart.add_product(self.product_one, 10)
self.assertEqual(self.cart.num_items, 10)
self.cart.add_product(self.product_two, 15)
self.assertEqual(self.cart.num_items, 25)
def test_quantity_of_empty_cart_is_zero(self):
self.assertEqual(self.cart.num_items, 0)
def test_can_calculate_subtotal_of_cart(self):
self.cart.add_product(self.product_one, 10)
self.assertEqual(self.cart.sub_total, 19.9)
self.cart.add_product(self.product_two, 15)
self.assertEqual(self.cart.sub_total, 57.25)
def test_subtotal_of_empty_cart_is_zero(self):
self.assertEqual(self.cart.sub_total, 0)
def test_can_update_quanity(self):
self.cart.add_product(self.product_one, 10)
self.cart.update_quantity(self.product_one, 15)
self.assertEqual(
self.cart.get_cart_item(self.product_one).quantity, 15)
def test_cannot_update_quantity_if_not_in_cart(self):
self.assertRaises(CartError, lambda:
self.cart.update_quantity(self.product_one, 10))
def test_can_save_cart(self):
self.cart.add_product(self.product_one)
self.cart.save(self.session)
self.new_cart = Cart(self.session)
self.assertEqual(self.cart.cart_items, self.new_cart.cart_items)
def test_can_reset_cart(self):
self.cart.add_product(self.product_one, 10)
self.cart.reset()
self.assertEqual(self.cart.cart_items, [])
示例4: manage_cart
# 需要导入模块: from cart import Cart [as 别名]
# 或者: from cart.Cart import save [as 别名]
def manage_cart(request, tmpl, user_id=None, action=None, product_id=None):
user = None
if user_id:
try:
user = User.objects.get(id=user_id)
except:
user = None
msg_ok = msg_err = products = cart = None
products = None
if user:
cart = Cart(request, user.id)
if request.user.is_staff:
products = Article.objects.all().order_by('order')
else:
products = Article.objects.filter(enabled=True).order_by('order')
if action == 'add':
product_id = int(request.POST.get('cart_add'))
if cart.add(product_id, 1):
msg_ok = _(u"Product successfully added to cart")
else:
msg_err = _(u"Error while adding product to cart")
elif action == 'del':
if cart.delete(int(product_id)):
msg_ok = _(u"Product successfully removed from cart")
else:
msg_err = _(u"Error while removing product from cart")
elif action == 'update':
update = True
for k,v in request.POST.iteritems():
product_id = 0
try:
quantity = int(v)
except:
quantity = 0
if k.startswith('product_'):
product_id = int(k[8:])
update = update and cart.update(product_id, quantity)
if update:
msg_ok = _(u"Product(s) successfully updated")
else:
msg_err = _(u"Error while updating product(s)")
elif action == 'validate':
valid = cart.is_valid()
if not valid and request.user.is_staff:
valid = request.POST.get('force') == '1'
if not valid:
msg_err = _(u"Unable to confirm this order, one (or more) product(s) in the cart exceed the available quantity")
else:
order = Order(user=user, creation_date=date.now())
order.save_confirm(cart)
cart.clear()
msg_ok = _(u"Order successfully confirmed")
cart.save(request)
return tmpl, {
'user_obj': user,
'products': products,
'cart': cart,
'msg_err': msg_err, 'msg_ok': msg_ok,
'is_admin': request.user.is_staff,
'currency': settings.CURRENCY, 'currency_alt': settings.CURRENCY_ALT,
}