예제 #1
0
def get_cart(request):
	"""First see if current user already got a cart.
	If got cart, load it. else create a new one with default settings.
	"""
	if 'id_cart' in request.session:
		cart = Cart.objects.get(pk=request.session['id_cart'])
	else:
		# Get the current currency of the user, or default currency.
		currency = Currency.get_current_currency(request)

		cart = Cart(currency=currency)
		
		# If a Default Carrier exist, then add it to the cart
		# when the user creates his first cart.
		try: 
			carrier = Carrier.objects.get(default=True)
			cart.carrier = carrier
		except Carrier.DoesNotExist:
			pass

		cart.save()

		request.session['id_cart'] = cart.pk

	return cart
예제 #2
0
def add_subscription(request, attributes=None):
	if request.method == "POST":
		plan = request.POST.get('plan', None)
		pet_size = request.POST.get('pet_size', None)
		upgrade = request.POST.get('upgrade', None)

		email = request.POST.get('email', None)

		# If user already have a cart when we try to add a product.
		# Remove the session (remove connection between user and cart)
		# to start on new cart.
		if 'id_cart' in request.session:
			request.session['id_cart'] = None
			request.session.pop('id_cart', None)

		# Get amount of months for the plan
		try:
			amount = int(plan.split('-')[0])
		except Exception as ex:
			pass # Return 404

		if upgrade == 'upgrade':
			try:
				plan_object = Plan.objects.get(slug=plan, premium=True)
			except Plan.DoesNotExist:
				pass # Return error
		else:
			try:
				plan_object = Plan.objects.get(slug=plan, premium=False)
			except Plan.DoesNotExist:
				pass # Return error

		# Create a new customer
		try: 
			customer = Customer.objects.get(email=email)
			address = customer.user_address.all()[0]
		except Customer.DoesNotExist:
			customer = Customer(email=email)
			customer.save()

			# Create a new address
			address = Address(customer=customer)
			address.save()



		# Create a new cart
		cart = Cart(customer=customer)

		# Add default shipping to cart
		# If a Default Carrier exist, then add it to the cart
		# when the user creates his first cart.
		try: 
			carrier = Carrier.objects.get(default=True)
			cart.carrier = carrier
		except Carrier.DoesNotExist:
			pass

		cart.save()
		request.session['id_cart'] = cart.pk

		# Add item to Cart
		ci = CartItem(cart=cart, plan=plan_object)
		ci.amount = amount
		ci.save()

		# If attributes are set, then also add the attributes
		# to this cartitem.
		if attributes:
			for k, v in attributes.iteritems():
				attribute = Attribute.objects.get(slug=k)
				choice = attribute.attributechoice_set.get(slug=v)

				ci.attribute.add(choice)
				ci.save()

		# Return True on success
		return True
	else:
		pass # Return 404