Example #1
0
def adjust_stock(sender,instance, **kwargs):
	if not instance.active:
		#Decrement stock counts
		orders= BookOrder.objects.filter(cart=instance)
		for order in orders:
			book=order.book
			book.stock -= order.quantity
			book.save()
		#Send Thank You mail
		subject = 'Thanks for shopping with Mystery Books!'
		from_email= '*****@*****.**'
		to_email= [instance.user.email]
		
		email_context= Context({
			'username': instance.user.username,
			'orders': orders
		})
		
		text_email= render_to_string('email/purchase_email.txt', email_context)
		html_email= render_to_string('email/purchase_email.html', email_context)
		
		msg= EmailMultiAlternatives(subject,text_email,from_email,to_email)
		msg.attach_alterntives(html_email, 'text/html')
		msg.content_subtype = 'html'
		msg.send()