コード例 #1
0
ファイル: views.py プロジェクト: marcuslind90/lindshop
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
ファイル: tests.py プロジェクト: marcuslind90/lindshop
	def test_updateAmount(self):
		cart = Cart(currency=self.currency)
		cart.save()

		self.factory.session = {'id_cart': cart.pk} # Just make sure the factory got a session object.
		
		addProduct(self.factory, self.product1.id) # Add a product to the cart.

		self.assertEqual(cart.cartitem_set.get(product=self.product1).amount, 1) # Default amount after added should be 1.

		updateAmount(self.factory, cart.cartitem_set.get(product=self.product1).pk, 5) # Update amount to 5.

		self.assertEqual(cart.cartitem_set.get(product=self.product1).amount, 5) # Amount should be 5 after change.
コード例 #3
0
ファイル: tests.py プロジェクト: marcuslind90/lindshop
	def test_removeProduct(self):
		from django.core.exceptions import ObjectDoesNotExist
		cart = Cart(currency=self.currency)
		cart.save()

		self.factory.session = {'id_cart': cart.pk} # Just make sure the factory got a session object.
		
		addProduct(self.factory, self.product1.id) # Add a product to the cart.

		self.assertEqual(cart.cartitem_set.get(product=self.product1).amount, 1) # Default amount after added should be 1.

		removeProduct(self.factory, cart.cartitem_set.get(product=self.product1).pk) # Remove the product that as just added.
		
		self.assertEqual(len(cart.cartitem_set.filter(product=self.product1)), 0) # After removing he product, it should not be found.
コード例 #4
0
ファイル: tests.py プロジェクト: marcuslind90/lindshop
	def test_AddProductToCart(self):
		cart = Cart(currency=self.currency)
		cart.save()

		self.factory.session = {'id_cart': cart.pk} # Just make sure the factory got a session object.
		# Add a product to the cart, with correct data.
		# Should return True.
		addProduct(self.factory, self.product1.id)
		self.assertEqual(len(cart.cartitem_set.all()), 1) # It should now exist 1 item in the cart.

		try:
			addProduct(self.factory, 53) # Add product with wrong ID.
		except:
			pass

		# After adding a product with wrong id (above), it should still only be 1
		# item in the cart.
		self.assertEqual(len(cart.cartitem_set.all()), 1)
コード例 #5
0
ファイル: views.py プロジェクト: marcuslind90/lindshop
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