Beispiel #1
0
def create_order(request):
	order = Order()
	checkout_form = CheckoutForm(request.POST, instance=order)
	order = checkout_form.save(commit=False)
	order.user = None
	if request.user.is_authenticated():
		order.user = request.user
	order.status = Order.SUBMITTED
	print (order.number)
	order.save()
	print (order.number)
	order.number = int(str(order.id) + datetime.today().strftime('%m%d%Y'))
	order.save()
	print (order.number)
	# create order item for each cart item
	if order.pk:
		cart_items = carts.get_cart_items(request)
		for ci in cart_items: 
			oi = OrderItem()
			oi.order = order
			oi.quantity = ci.quantity
			oi.price = ci.product.price
			oi.product = ci.product
			oi.save()
			carts.empty_cart(request)
	return order
Beispiel #2
0
def create_order(request):
	order = Order()
	checkout_form = CheckoutForm(request.POST, instance=order)
	order = checkout_form.save(commit=False)
	order.user = None
	if request.user.is_authenticated():
		order.user = request.user
	order.status = Order.SUBMITTED
	order.save()
	order.number = int(str(order.id) + datetime.today().strftime('%m%d%Y'))
	order.save()
	# create order item for each cart item
	if order.pk:
		cart_items = carts.get_cart_items(request)
		for ci in cart_items: 
			oi = OrderItem()
			oi.order = order
			oi.quantity = ci.quantity

			if ci.product.quantity >= ci.quantity:
				ci.product.quantity = ci.product.quantity - ci.quantity
			else:
				ci.product.quantity = 0	
			ci.product.save()

			oi.price = ci.product.price
			oi.product = ci.product
			oi.save()
			carts.empty_cart(request)
	# send_mail('Subject here', 'Here is the message.', '*****@*****.**',
	# ['*****@*****.**'], fail_silently=False)
	items = OrderItem.objects.filter(order = order)
	plaintext = get_template('email.txt')
	htmly = get_template('email.html')
	d = Context({ 'order': order, 'items': items})
	subject, from_email, to = 'Ваш заказ', '*****@*****.**', [order.email]
	text_content = plaintext.render(d)
	html_content = htmly.render(d)
	msg = EmailMultiAlternatives(subject, text_content, from_email, to)
	msg.attach_alternative(html_content, "text/html")
	msg.send()
	print('sent!')
	return order