예제 #1
0
def add_product_from_form(request, amount=1):
	"""Add a product from POST form on Product Page.
	Unlike function `addProduct()` this function also adds attributes
	and other product data.
	"""
	cart = get_cart(request)

	attributes = request.POST.getlist('attributes[]', None)
	id_product = request.POST.get('id_product', None)

	attribute_list = []
	attribute_filter = {}

	# Turn a JSON list of attributes into a list (`attribute_list`)
	# of AttributeChoice objects.
	for json_attribute in attributes:
		attribute = json.loads(json_attribute)
		attribute['attribute'] = attribute['attribute'].split('-')[1:][0]  # Remove the "attribute-" part and only keep the real attribute slug.
		
		try:
			attribute_obj = AttributeChoice.objects.get(attribute__slug=attribute['attribute'], slug=attribute['value'])
			attribute_list.append(attribute_obj)
		except AttributeChoice.DoesNotExist:
			pass

	product = Product.objects.get(pk=id_product)

	# If attribute is added. Then check for a CartItem with all those attributes.
	if len(attribute_list) > 0:
		ci = CartItem.objects.filter(cart=cart, product=product, attribute__in=attribute_list).annotate(num_attr=Count('attribute')).filter(num_attr=len(attribute_list))
	# If no attribute is added, check for a CartItem without any attributes at all.
	else:
		ci = CartItem.objects.filter(cart=cart, product=product, attribute__isnull=True)

	# If CartItem that is same to the added product already exist in this cart, 
	# then just update the amount of the already existing cart item.
	if len(ci) > 0:
		ci = ci[0]
		ci.amount += amount
		ci.save()
	# If no CartItem exist, then create a new one. 
	else:
		ci = CartItem(cart=cart, product=product)
		ci.amount = amount
		ci.save()
		ci.attribute.add(*attribute_list)

	return HttpResponse(status=200)
예제 #2
0
def addProduct(request, id_product, amount=1):

	cart = get_cart(request)

	p = Product.objects.get(pk=id_product)

	try:
		ci = CartItem.objects.get(cart=cart, product=p)
		ci.amount += amount
	except CartItem.DoesNotExist:
		ci = CartItem(cart=cart, product=p)
		ci.amount = amount

	ci.save()

	return HttpResponse("Product Added: %s in Cart #%s" % (p.name, request.session['id_cart']) )
예제 #3
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