def add_address(request): """Add data to the address object. Return address object. """ country = request.POST.get("country", None) address = request.POST.get("address", None) city = request.POST.get("city", None) zipcode = request.POST.get("zipcode", None) cart = Cart.objects.get(pk=request.session["id_cart"]) # Get country object of country. country_obj = Country.objects.get(pk=country) # If user already have an address. if len(cart.user.user_address.all()) > 0: customer_address = cart.user.user_address.all()[0] customer_address.country = country_obj customer_address.address = address customer_address.city = city customer_address.zipcode = zipcode customer_address.save() # Else create a new address else: customer_address = Address(user=cart.user, country=country_obj, address=address, city=city, zipcode=zipcode) customer_address.save() return customer_address
def updateCustomerCountry(request, id_country): cart = Cart.objects.get(pk=request.session['id_cart']) country = Country.objects.get(pk=id_country) # If customer exist, check if address exists. # If address exists, set country. If not, create a new address. if cart.user: if cart.user.user_address.all()[0]: address = cart.user.user_address.all()[0] address.country = country address.save() else: address = Address(user=cart.user, country=country) address.save() # If no customer exists yet, create a customer, save it, connect to cart # and add a address to the customer with the country set. else: return HttpResponse("Can't Update Country: No customer exist yet.") return HttpResponse("Updated Country to: %s" % country.name)
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