def createCookieAndCart(request, cookie_value=""): print("Setting a cookie for the user. Adding ") cookie_value = CART.generate_Cart_ID() c = CART() value = cookie_value c.Cart_ID = value c.save() request.set_cookie("CartID", cookie_value) return "" #test
def create_cart_id_value(request): print("generating cart_value...") cart_value = generate_Cart_ID() print("Done, cart_value is: " + cart_value) #while cart_value in CART.objects.get(CartId=cart_value): # cart_value = generate_Cart_ID() print("Generated value was unique, great!") request.session['CartID'] = cart_value #add cart_value to the CART database c = CART(Cart_ID=cart_value) c.save() return cart_value
def index(request): print("View [index] was called.") COOKIE_NAME = 'CartID' context = {} Cart_ID = '' #problems: if user has cookies disabled, might be infinite loop if COOKIE_NAME in request.COOKIES: print(COOKIE_NAME + " is a cookie in the request") print("The value of " + COOKIE_NAME + " is: " + request.COOKIES[COOKIE_NAME]) cookieValue = request.COOKIES[COOKIE_NAME] #Search every cart until we find a match for cart in CART.objects.all(): if cart.Cart_ID == cookieValue: #We've found a match print( "The cookie's value matches a Cart_ID value of a CART object" ) #Success! Load the page! print("TEST:" + str(cart.Cart_ID) + " length = " + str(len(cart.Cart_ID))) cart_content_list = cart.cart_content_set.all() print("cart_content_list contains: " + str(cart_content_list)) context["cart_content_list"] = cart_content_list context["Cart_ID"] = cart.Cart_ID #create the context later print("Context is: " + str(context)) return render(request, 'myCart/myCart.html', context=context) #No match was found in the database # Do: 1)Create cookie 2)Store Data in CART DB table 3)Set the cookie on the response print( "The cookie value DOES NOT match any Cart_ID value in the CART table" ) #do nothing for now return HttpResponse("Under construction") else: ''' there was no CartID associated with the session, make one, \ update the DB and then reload the page''' #This response's cookie will be set below response = HttpResponseRedirect(reverse('index', args=None)) print("CartID is not a cookie in the request. Making a cookie now.") #max_age = 86400 #Remove this CartID after it has been the the DB for 7 days #generate a cookie value cookie_value = CART.generate_Cart_ID() print("Generated a cookie, the value is: " + cookie_value + " setting the key/value pair now.") response.set_cookie("CartID", cookie_value) print("Done setting cookie") print("Done creating cookie, redirecting user now.") print("Creating a cart") c = CART() value = c.generate_Cart_ID( ) #generater random string of len 32 via static function c.Cart_ID = value c.save() print("Done creating a cart.") print("Reloading page.") #reload the page now that the cookie has been set return response