本文整理汇总了Python中cart.cart.Cart类的典型用法代码示例。如果您正苦于以下问题:Python Cart类的具体用法?Python Cart怎么用?Python Cart使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Cart类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: order_create
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
if cart.coupon:
order.coupon = cart.coupon
order.discount = cart.coupon.discount
order.save()
for item in cart:
OrderItem.objects.create(order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity'])
# clear the cart
cart.clear()
# launch asynchronous task
order_created.delay(order.id)
# set the order in the session
request.session['order_id'] = order.id
# redirect to the payment
return redirect(reverse('payment:process'))
else:
form = OrderCreateForm()
return render(request, 'orders/order/create.html', {'cart': cart,
'form': form})
示例2: TestShow
class TestShow(TestCase):
def setUp(self):
rf = RequestFactory()
request = rf.get(reverse('cart-show'))
add_session_to_request(request)
self.cart = Cart(request)
product = ProductFactory()
self.cart.add(product, product.price, 1)
self.client = Client()
engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore()
store.save()
self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
def test_get(self):
# store cart id in session
session = self.client.session
session['CART-ID'] = self.cart.cart.pk
session.save()
resp = self.client.get(reverse('cart-show'))
self.assertEqual(resp.status_code, 200)
self.assertTemplateUsed(resp, 'check/cart.html')
def test_post(self):
data = {}
for item in self.cart:
data['%s-quantity' % item.product.pk] = 2
# store cart id in session
session = self.client.session
session['CART-ID'] = self.cart.cart.pk
session.save()
resp = self.client.post(reverse('cart-show'), data=data)
self.assertRedirects(resp, reverse('cart-check-in'), status_code=302, target_status_code=200)
示例3: order_create
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
if cart.coupon:
order.coupon = cart.coupon
order.discount = cart.coupon.discount
order.save()
for item in cart:
OrderItem.objects.create(order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity'])
# Usunięcie zawartości koszyka na zakupy.
cart.clear()
# Uruchomienie zadania asynchronicznego.
order_created.delay(order.id) # Umieszczenie zamówienia w sesji.
request.session['order_id'] = order.id # Przekierowanie do płatności.
return redirect(reverse('payment:process'))
else:
form = OrderCreateForm()
return render(request,
'orders/order/create.html',
{'cart': cart, 'form': form})
示例4: add_sandwich
def add_sandwich(request, base):
cart = Cart(request.session)
sand_base = get_object_or_404(Base, pk=base)
form = SandwichForm(request.POST, initial={'base': base})
form2 = QuantityForm(request.POST)
if form.is_valid() and form2.is_valid():
bread = form.cleaned_data['bread']
toppings = form.cleaned_data['toppings']
condiments = form.cleaned_data['condiments']
cheeses = form.cleaned_data['cheeses']
side= form.cleaned_data['side']
quantity = form2.cleaned_data['quantity']
sandwich = Sandwich()
sandwich.base= sand_base
sandwich.bread= bread
sandwich.save()
sandwich.side= side
sandwich.toppings= toppings
sandwich.condiments= condiments
sandwich.cheeses= cheeses
sandwich.save()
product = sandwich
cart.add(product, price=sandwich.price, quantity=quantity)
return HttpResponseRedirect(reverse('cart:show_cart'))
return render(request, 'sandwich_builder/sandwich_constructor.html',
{'base': sand_base, 'form':form})
示例5: order_create
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save()
for item in cart:
OrderItem.objects.create(
order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity']
)
# clear the cart
cart.clear()
# launch asynchronous task
order_created.delay(order.id)
return render(
request,
'orders/order/created.html',
{'order': order}
)
else:
form = OrderCreateForm()
return render(
request,
'orders/order/create.html',
{
'cart': cart,
'form': form
}
)
示例6: order_create
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
if cart.coupon:
order.coupon = cart.coupon
order.discount = cart.coupon.discount
order.save()
try:
for item in cart:
OrderItem.objects.create(order=order,
product=item['product'],
price=item['price'],
qty=item['qty'])
except Exception as exp:
return HttpResponseBadRequest(str(exp))
# clear the cart
cart.clear()
# send email asynchronously
order_created.delay(order.id)
# set the order in the session
request.session['order_id'] = order.id
# redirect for payment
return redirect(reverse('payment:process'))
else:
form = OrderCreateForm()
return render(request,
'orders/order/create.html',
{'form': form, 'cart': cart})
示例7: order_create
def order_create(request):
'''get the product in cart of session and generate an order'''
cart=Cart(request) #get the cart information as a dictionary
if request.method=='POST':
form=OrderCreateForm(request.POST) #get the user input information
if form.is_valid():
order=form.save() #save the input information into database
for item in cart: #save the products in cart into database
OrderItem.objects.create(order=order,product=item['product'],price=item['price'],quantity=item['quantity'])
# Clear the cart information in session
cart.clear()
# Launch asynchronous task
# Call delay() method of hte task to execute it asynchronously. The task will be added to the queue and will be executed by a worker as soon as possible.
order_created.delay(order.id)
#set the order in the session
request.session['order_id']=order.id
#return render(request, 'orders/order/created.html',{'order':order})
#redirect to the payment
return redirect(reverse('payment:process'))
else:
form=OrderCreateForm()
return render(request, 'orders/order/create.html',{'cart':cart,'form':form})
示例8: order_create
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
if cart.coupon:
order.coupon = cart.coupon
order.discount = cart.coupon.discount
order.save()
for item in cart:
OrderItem.objects.create(order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity'])
# clear the cart
cart.clear()
# launch asynchronous task
order_created.delay(order.id)
# set the order in the session
request.session['order_id'] = order.id
# Increment the score of each product id contained in the order.
# The score represents the times another product has been bought
# together with the given product.
Recommender().products_bought(item['product'] for item in cart)
# redirect ot the payment
return redirect(reverse('payment:process'))
else:
form = OrderCreateForm()
return render(request,
'orders/order/create.html',
{'cart': cart, 'form': form})
示例9: remove_from_cart
def remove_from_cart(request):
product_id = request.POST.get('product_id')[0]
print(product_id)
product = Product.objects.get(pk=product_id)
cart = Cart(request)
cart.remove(product)
return HttpResponse(cart.summary())
示例10: order_create
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
#order = form.save()
order = form.save(commit=False)
if cart.coupon:
order.coupon = cart.coupon
order.discount = cart.coupon.discount
order.save()
for item in cart:
OrderItem.objects.create(order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity'])
# clear the cart
cart.clear()
# launch task
order_created(order.id)
return render(request, 'shop/orders/created.html', {'order': order})
else:
form = OrderCreateForm()
return render(request, 'shop/orders/create.html', {'cart': cart,
'form': form})
示例11: add_to_cart
def add_to_cart(request):
product_id = request.POST.get('product_id')[0]
product = Product.objects.get(pk=product_id)
cart = Cart(request)
quantity = 1
cart.add(product, product.price, quantity)
return HttpResponse(cart.summary())
示例12: create_order
def create_order(self, client):
cart = Cart(self.client)
cart.create_cart()
cart = cart.cart
order = Order(client=client, cart=cart)
order.save()
return order
示例13: add_project_to_cart
def add_project_to_cart(self, request, *args, **kwargs):
project = ProjectPage.objects.get(id=request.POST.get('project_id'))
cart = Cart(request)
cart.add(project, request.POST.get('amount'), 1)
request.session['funder_name']=request.POST.get('name')
request.session['funder_organisation']=request.POST.get('organisation')
return redirect('/order/checkout')
示例14: order_create
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrdenCreateForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
if cart.coupon:
order.coupon = cart.coupon
order.discount = cart.coupon.discount
order.save()
for item in cart:
OrderItem.objects.create(
orden=order,
product=item['product'],
precio=item['price'],
cantidad=item['quantity'],
)
#limpiamos l carro
cart.clear()
#lanzamos el proceso asincrono
order_created.delay(order.id)
#set the order in the session
request.session['order_id'] = order.id
#redirect to the payment
return redirect(reverse('payment:process'))
else:
form = OrdenCreateForm()
return render(request, 'orders/order/created.html', {'cart':cart, 'form':form})
示例15: post
def post(self,request):
cart=Cart(request)
form=OrderCreateForm(request.POST)
if form.is_valid():
order=form.save()
for item in cart:
OrderItem.objects.create(order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity'])
# Borrar el carrito
cart.clear()
# Lanzamos la tarea asincrona
# order_created.delay(order.id)
# Mandamos email sin tarea asincrona
# seteamos la orden en la sesion para paypal
request.session['order_id']=order.id
# redireccionamos hacia el cobro
return redirect(reverse('payment:process'))
# template='orders/order/created.html'
# context={
# 'order':order
# }
# return render(request,template,context)
else:
cart=Cart(request)
form=OrderCreateForm(request.POST)
template='orders/order/create.html'
context={
'cart':cart,
'form':form
}
return render(request,template,context)