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 makeCartCookie(request): print("Creating cookie CartID...") #Before we continue, if the user already had a CartID cookie # then don't create a new one. Instead just leave their CartID # cookie how it is. for key in request.session.keys(): print(key + " :=> " + request.session.get(key)) if request.session.get("sessionid"): print("There was a session key in request.session") else: print("There was no session key in request.session...") if request.session.get("CartID"): print("There was a CartID in request.session") else: print("There is no CartID in request.session") if 'CartID' in request.COOKIES: print("In request.COOKIES there is a CartID") print("The CarID is: " + request.COOKIES['CartID']) else: print("In request.COOKIES there is no CartID") if request.session.get("CartID") is None: print("No CartID cookie detected in client. Creating one now.") #generate a random string 32 chars long val = CART.generate_Cart_ID() print("Value generated is: " + val) #set the cookie response = HttpResponse("") max_age = 86400 #1 day expires = datetime.datetime.strftime( datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie('CartID', val, max_age=86400, expires=expires) response.write("<h1>Cookie has been set</h1>") print("Cookie has been set.") return response else: print("The request already has a CartID cookie.") print("The value of the cookie is: " + request.session.get("CartID")) return HttpResponse( "A cookie had already been set. It will not be changed.")
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 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