def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the cart dictionary from the session # - create a list to hold melon objects and a variable to hold the total # cost of the order # - loop over the cart dictionary, and for each melon id: # - get the corresponding Melon object # - compute the total cost for that type of melon # - add this to the order total # - add quantity and total cost as attributes on the Melon object # - add the Melon object to the list created above # - pass the total order cost and the list of Melon objects to the template # # Make sure your function can also handle the case wherein no cart has # been added to the session melon_dict = {} melon_price = [] total_cost = 0 if 'cart' not in session: flash("Your cart is empty.") else: for m_id in session['cart']: melon_dict[melons.get_by_id(m_id)] = session['cart'][m_id] melon_price.append(session['cart'][m_id] * float(melons.get_by_id(m_id).price)) total_cost = sum(melon_price) return render_template("cart.html", melons=melons.melon_types, melon_dict=melon_dict, total_cost=total_cost)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # - get the list-of-ids-of-melons from the session cart melon_order = session['cart'] # - loop over this list: order = {} for id in melon_order: if id not in order: order[id] = (melons.get_by_id(id).common_name, melons.get_by_id(id).price, melon_order.count(id)) # id = tuple(common_name, price, qty) # make a dictiionary within dictionary # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types return render_template("cart.html", order=order) )
def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the cart dictionary from the session # - create a list to hold melon objects and a variable to hold the total # cost of the order # - loop over the cart dictionary, and for each melon id: # - get the corresponding Melon object # - compute the total cost for that type of melon # - add this to the order total # - add quantity and total cost as attributes on the Melon object # - add the Melon object to the list created above # - pass the total order cost and the list of Melon objects to the template # # Make sure your function can also handle the case wherein no cart has # been added to the session melon_list = [] total_cost = 0 for melon in session['cart']: melon_list.append(melons.get_by_id(melon)) total_cost += melons.get_by_id(melon).price*session['cart'][melon] return render_template("cart.html", melon_list = melon_list, total_cost = total_cost)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart melon_order = session['cart'] total_melons = {} for item in melon_order: total_melons[item] = {} total_melons[item]["count"] = total_melons[item].get("count", 0) + 1 total_melons[item]["price"] = melons.get_by_id(item).price total_melons[item]["common_name"] = melons.get_by_id(item).common_name total_melons[item]["total"] = total_melons[item]["price"] * total_melons[item]["count"] # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types return render_template("cart.html", dictionary=total_melons)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types total_order_cost = 0 # list of tuples of name, qty, price, total individual_melon_order = [] for melon_id in session["cart"]: melon_quantity = session["cart"].count(melon_id) melon_name = melons.get_by_id(melon_id).common_name melon_price = melons.get_by_id(melon_id).price melon_total = melon_quantity * melon_price melon_tuple = (melon_name, melon_quantity, melon_price, melon_total) total_order_cost += melon_total if melon_tuple not in individual_melon_order: individual_melon_order.append(melon_tuple) return render_template("cart.html", melon_order=individual_melon_order, order_total=total_order_cost)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types shopping_list = session["cart"] melon_id_dictionary = {} for id in shopping_list: if id in melon_id_dictionary: melon_id_dictionary[id]["qty"] += 1 melon_id_dictionary[id]["subtotal"] += melons.get_by_id(id).price else: melon_id_dictionary[id] = {} melon_id_dictionary[id]["qty"] = 1 melon_id_dictionary[id]["price"] = melons.get_by_id(id).price melon_id_dictionary[id]["common_name"] = melons.get_by_id( id).common_name melon_id_dictionary[id]["subtotal"] = melon_id_dictionary[id][ "price"] order_total = 0 for melon in melon_id_dictionary: order_total += melon_id_dictionary[melon]["subtotal"] # for id in shopping_list: # melon_id_dictionary.setdefault(id, {}) # if id in melon_id_dictionary: # melon_id_dictionary[id]["qty"] += 1 # melon_id_dictionary[id]["subtotal"] += melons.get_by_id(id).price # else: # melon_id_dictionary[id]["qty"] = 1 # melon_id_dictionary[id]["price"] = melons.get_by_id(id).price # melon_id_dictionary[id]["common_name"] = melons.get_by_id(id).common_name # melon_id_dictionary[id]["subtotal"] = melons.get_by_id(id).price return render_template("cart.html", melon_id_dictionary=melon_id_dictionary, total="%.2f" % order_total)
def shopping_cart(): """Display content of shopping cart.""" if 'cart' not in session: session['cart'] = [] melons_in_cart = [] total = 0 for num in session['cart']: current_melon = melons.get_by_id(num) total += current_melon.price if current_melon in melons_in_cart: current_melon.quantity += 1 else: current_melon.quantity = 1 melons_in_cart.append(current_melon) # TODO: Display the contents of the shopping cart. # The logic here will be something like: # - get the list-of-ids-of-melons from the session cart # see if session has a cart; if not add one. # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types return render_template("cart.html", melons=melons_in_cart, grand_total=total)
def shopping_cart(): """Display content of shopping cart.""" order_total = 0 melon_cart_ids = session.get("cart",[]) cart={} for melon_id in melon_cart_ids: if melon_id in cart: melon_info =cart[melon_id] else: melon_type=melons.get_by_id(melon_id) melon_info=cart[melon_id] = { "melon_name":melon_type.common_name, "melon_price":melon_type.price, "qty":0, "total_cost":0 } melon_info["qty"] += 1 melon_info["total_cost"] += melon_info["melon_price"] order_total += melon_info["melon_price"] cart = cart.values() return render_template("cart.html", cart=cart, order_total=order_total )
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types total_cost = 0 melon_items = {} for melon_id in session["cart"]:#iterate through the cart list quantity = session["cart"].count(melon_id) #current melon is melon object current_melon = melons.get_by_id(melon_id) melon_total = quantity * current_melon.price total_cost += melon_total melon_items[melon_id] = {"melon": current_melon.common_name, "qty":quantity, "price":current_melon.price, "mel_total": melon_total} return render_template("cart.html", total_cost = total_cost, melon_items=melon_items)
def shopping_cart(): """Display content of shopping cart.""" cart = {} for item in session["cart"]: if item in cart: cart[item] += 1 else: cart[item] = 1 print cart # TODO make this really work for melon_id in cart.keys(): melon = melons.get_by_id(melon_id) melon_name = melon.common_name melon_price = melon.price melon_quantity = cart[melon_id] # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types return render_template("cart.html")
def show_shopping_cart(): """Display content of shopping cart.""" # Keep track of the total cost of order order_total = 0 # Create a cart list to hold melons corresponding to the melon_id's cart_melons = [] # Get the cart dictionary out of the session and checks if empty cart = session.get("cart", {}) # Loop over the cart dictionary for melon_id, quantity in cart.items(): # Retrieve the melon corresponding to id melon = melons.get_by_id(melon_id) # Calculate the total cost for this type of melon and add it to the # overall total for the order total_cost = quantity * melon.price order_total += total_cost # Add the quantity and total cost for this melon melon.quantity = quantity melon.total_cost = total_cost # Adds the melons to our list cart_melons.append(melon) # Sends list of melons and order total to cart template return render_template("cart.html", cart=cart_melons, order_total=order_total)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart current_cart = session['cart'] for melon_id in current_cart: melon_info = melons.get_by_id(melon_id) # melon_price = melon_info.price # melon_name = melon_info.common_name print melon_info # - hand to the template the total order cost and the list of melon types print "*" * 20 # - keep track of the total amt ordered for a melon-type print request.args.get("quantity") # print type(melon_quantity) # - keep track of the total amt of the entire order return render_template("cart.html")
def add_to_cart(melon_id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Successfully added to cart'. """ # TODO: Finish shopping cart functionality # The logic here should be something like: # # - add the id of the melon they bought to the cart in the session melon = melons.get_by_id(melon_id) # Check if the melon ID exists in the session. If it doesn't, add # the ID as a key with a quantity of 0 as its value, and add 1. # Otherwise, add 1 to the quantity. session.setdefault("cart", []).append(melon_id) print "#########" print melon print "#########" print session print "#########" flash("You have added {} to your cart.".format(melon.common_name)) return redirect('/melons')
def add_to_cart(melon_id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Melon successfully added to cart'.""" # TODO: Finish shopping cart functionality # The logic here should be something like: # # - check if a "cart" exists in the session, and create one (an empty # dictionary keyed to the string "cart") if not # - check if the desired melon id is the cart, and if not, put it in # - increment the count for that melon id by 1 # - flash a success message # - redirect the user to the cart page melon = melons.get_by_id(melon_id).common_name if "cart" not in session: session["cart"] = {} session["cart"].setdefault(melon_id, 0) session["cart"][melon_id] += 1 flash("{} successfully added to your cart".format(melon)) # print "\n\n\nthis is your shopping cart:", session["cart"], "\n\n\n" return redirect("/cart")
def shopping_cart(): """Display content of shopping cart.""" melon_list = [] total_cost = 0 if "cart" in session: melon_id_list = session['cart'] melon_cost = 0 for m_id in melon_id_list: melon = melons.get_by_id(m_id) melon_cost += melon.price print melon melon_list.append(melon) total_cost = melon_cost melon_order = {} for melon in melon_list: melon_order[melon] = { "price": melon.price, "quantity": melon_order[melon].get("quantity", 0) + 1, "sub_total": melon_order[melon]["price"] * melon_order[melon]["quantity"], } print melon_order # total_cost = melon_cost return render_template("cart.html", melon_list=melon_list, total_cost=total_cost)
def show_shopping_cart(): """Display content of shopping cart.""" order_total = 0 cart_melons = [] cart = session.get("cart", {}) for melon_id, quantity in cart.items(): melon = melons.get_by_id( melon_id ) #Declaring melon from Melon Object with the get_by_id func from melons.py #Melons for import Melons.py total_cost = quantity * melon.price order_total += total_cost melon.quantity = quantity melon.total_cost = total_cost cart_melons.append(melon) return render_template("cart.html", cart=cart_melons, order_total=order_total)
def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # - get the cart dictionary from the session shopping_cart = session["melon_cart"] print(shopping_cart) # - create a list to hold melon objects and a variable to hold the total # cost of the order cart_list = [] total_cost = 0 # - loop over the cart dictionary, and for each melon id: for melon_id in shopping_cart: # - get the corresponding Melon object melon_in_cart = melons.get_by_id(melon_id) # - compute the total cost for that type of melon melon_cost = melon_in_cart.price melon_qty = shopping_cart[melon_id] this_melon_cost = melon_cost * melon_qty # - add this to the order total total_cost += this_melon_cost # - add quantity and total cost as attributes on the Melon object melon_in_cart.this_melon_cost = this_melon_cost melon_in_cart.qty = melon_qty # - add the Melon object to the list created above cart_list.append(melon_in_cart) # - pass the total order cost and the list of Melon objects to the template # Make sure your function can also handle the case wherein no cart has # been added to the session return render_template("cart.html", cart_list=cart_list, total_cost=total_cost)
def add_to_cart(id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Successfully added to cart'. """ melon = melons.get_by_id(id) melon_name = melon.common_name print melon_name # TODO: Finish shopping cart functionality # - add the id of the melon they bought to the cart in the session # if session does not contain a cart, create a new cart if session.get('cart', None) is None: session['cart'] = [] #append the melon id to cart list session['cart'].append(id) # print session['cart'] # flash mesage msg = "Added %s to cart." % melon_name flash(msg) #flask.flash(message, category='message') return redirect("/cart")
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types session["cart"] = session.get("cart",[]) melon_cart_dict = {} #Iterating over the list of id's in our cart. for melon_id in session["cart"]: #Calling function get_by_id to get melon object associated with id. melon_object = melons.get_by_id(melon_id) #adding a key to the dictionary such that the key is the id, and #value is the returned melon object. melon_cart_dict[melon_id] = melon_cart_dict.get(melon_id, melon_object) print melon_object.quantity melon_object.quantity += 1 print melon_object.quantity return render_template("cart.html", my_cart=melon_cart_dict)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types # import pdb; pdb.set_trace() melons_in_cart_id = session["cart"].keys() cart_melons = {} total = 0.00 for melon_id in melons_in_cart_id: melon_id = int(melon_id) melon_type = melons.get_by_id(melon_id) cart_melons[melon_id] = {} cart_melons[melon_id]["common_name"] = melon_type.common_name cart_melons[melon_id]["price"] = melon_type.price cart_melons[melon_id]["qty"] = session["cart"][str(melon_id)] total += cart_melons[melon_id]["qty"] * cart_melons[melon_id]["price"] # print cart_melons return render_template("cart.html", cart_melons = cart_melons, total=total)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types # import pdb; pdb.set_trace() cart_dict = {} for id in session["cart"]: melon = melons.get_by_id(id) if id not in cart_dict: cart_dict[melon] = 1 else: cart_dict[melon] += 1 print cart_dict return render_template("cart.html", cart_dict=cart_dict)
def shopping_cart(): """Display content of shopping cart.""" list_ids = session['cart'] dict_melon = {} for each_id in list_ids: count_ids = list_ids.count(each_id) dict_melon[each_id] = [count_ids] dict_melon[each_id].append() melon = melons.get_by_id(each_id) # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types return render_template("cart.html")
def show_melon(melon_id): """Return page showing the details of a given melon. Show all info about a melon. Also, provide a button to buy that melon. """ melon = melons.get_by_id(melon_id) return render_template("melon_details.html", display_melon=melon)
def show_shopping_cart(): """Display content of shopping cart.""" shopping_cart = session["cart"] melon_objects = [] total_cost_of_cart = float(0) for melon_id, qty in shopping_cart.items(): # water_id, 5 each_melon_total = 0 cart_each_melon = melons.get_by_id( melon_id ) #[water_id,melon-type, watermelon, 3, url, green, False ] each_melon_total = qty * cart_each_melon.price #15 total_cost_of_cart += each_melon_total cart_each_melon.price = "${:.2f}".format(cart_each_melon.price) #"${:.2f}".format(self.price) cart_each_melon.quantity = qty cart_each_melon.total_cost = "${:.2f}".format(each_melon_total) melon_objects.append(cart_each_melon) return render_template("cart.html", melon_in_cart=melon_objects, cart_price=total_cost_of_cart)
def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the cart dictionary from the session # - create a list to hold melon objects and a variable to hold the total # cost of the order # - loop over the cart dictionary, and for each melon id: # - get the corresponding Melon object # - compute the total cost for that type of melon # - add this to the order total # - add quantity and total cost as attributes on the Melon object # - add the Melon object to the list created above # - pass the total order cost and the list of Melon objects to the template # # Make sure your function can also handle the case wherein no cart has # been added to the session # print("=====>",session["cart"]) session["cart"] = session.get("cart", {}) cart = [melons.get_by_id(melon_id) for melon_id in session["cart"]] total = 0 for melon_object in cart: melon_object.quantity = session['cart'][melon_object.melon_id] melon_object.total_price = melon_object.quantity * melon_object.price total += melon_object.total_price return render_template("cart.html", cart=cart, total=total)
def show_shopping_cart(): """Display content of shopping cart.""" # Keep track of the total cost of the order order_total = 0 # Create a list to hold Melon objects corresponding to the melon_id's in # the cart cart_melons = [] # Get the cart dictionary out of the session (or an empty one if none # exists yet) cart = session.get("cart", {}) # Loop over the cart dictionary for melon_id, quantity in cart.iteritems(): # Retrieve the Melon object corresponding to this id melon = melons.get_by_id(melon_id) # Calculate the total cost for this type of melon and add it to the # overall total for the order total_cost = quantity * melon.price order_total += total_cost # Add the quantity and total cost as attributes on the Melon object melon.quantity = quantity melon.total_cost = total_cost # Add the Melon object to our list cart_melons.append(melon) # Pass the list of Melon objects and the order total to our cart template return render_template("cart.html", cart=cart_melons, order_total=order_total)
def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the cart dictionary from the session # - create a list to hold melon objects and a variable to hold the total # cost of the order # - loop over the cart dictionary, and for each melon id: # - get the corresponding Melon object # - compute the total cost for that type of melon # - add this to the order total # - add quantity and total cost as attributes on the Melon object # - add the Melon object to the list created above # - pass the total order cost and the list of Melon objects to the template # # Make sure your function can also handle the case wherein no cart has # been added to the session order_total = 0 cart_list = [] if 'cart' in session: session_cart = session['cart'] for item in session_cart: melon_obj = melons.get_by_id(item) cart_list.append(melon_obj) melon_obj.quantity = session_cart[item] melon_obj.totalprice = session_cart[item] * melon_obj.price order_total += melon_obj.totalprice return render_template("cart.html", cart=cart_list, ordertotal="{:.2f}".format(order_total))
def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the cart dictionary from the session cart_dictionary = session.get("cart") # - create a list to hold melon objects hold_melon = [] # variable to hold the total cost of the order total_cost = 0 # - loop over the cart dictionary for melon_id in cart_dictionary: # and for each melon id: #get the corresponding Melon object melon = melons.get_by_id(melon_id) melon_price = melon.price #gives us the individual price of each melon #compute the total cost for that type of melon cost_of_melon = melon_price * cart_dictionary[melon_id] #add this to the order total total_cost += cost_of_melon melon.quantity = cart_dictionary[melon_id] melon.total_cost = total_cost # - add quantity and total cost as attributes on the Melon object # - add the Melon object to the list created above # - pass the total order cost and the list of Melon objects to the template # Make sure your function can also handle the case wherein no cart has # been added to the session return render_template("cart.html")
def add_to_cart(melon_id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Melon successfully added to cart'.""" # TODO: Finish shopping cart functionality # The logic here should be something like: # - check if a "cart" exists in the session, and create one (an empty # dictionary keyed to the string "cart") if not # - check if the desired melon id is the cart, and if not, put it in # - increment the count for that melon id by 1 # - flash a success message # - redirect the user to the cart page if 'cart' in session: session['cart'][melon_id] = session['cart'].get(melon_id, 0) + 1 flash("The melon was successfully added to your cart.") # return redirect(url_for('cart.html')) else: session['cart'] = {} melon = melons.get_by_id(melon_id) new_melon = melon.common_name return render_template("cart.html", melon_name=new_melon)
def show_shopping_cart(): """Display content of shopping cart.""" if 'cart' not in session.keys(): flash("You have nothing in your cart. Continue shopping!") return redirect("/melons") cart_dict = session['cart'] melon_list = [] order_total = 0 for melon_id in cart_dict.keys(): melon = melons.get_by_id(melon_id) quantity = cart_dict[melon_id] melon.quantity = quantity melon_total = melon.price * quantity melon.melon_total = melon_total order_total += melon_total melon_list.append(melon) return render_template("cart.html", melon_list=melon_list, order_total=order_total)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types melons_in_cart = {} if session: melon_id_list = session["cart"] for mel_id in melon_id_list: melon_to_add = melons.get_by_id(mel_id) melons_in_cart[melon_to_add] = melons_in_cart.get(melon_to_add, 0) + 1 print melons_in_cart else: flash("Your cart is empty.") return render_template("cart.html", melon_dict=melons_in_cart)
def shopping_cart(): """Display content of shopping cart.""" # get the list-of-ids-of-melons from the session cart melon_list = session['cart_items'] melon_counts = {} order_total = 0 # using set() reduces number of loops: for melon_id in set(melon_list): # grabbing the melon object based on id stored in session melon = melons.get_by_id(melon_id) qty = melon_list.count(melon_id) total = qty * melon.price order_total += total # creating an entry in the dictionary for each melon, with id as key melon_counts[melon_id] = {'melon_name': melon.common_name, 'melon_price': melon.price, 'qty': qty, 'total_per_type': total, } return render_template("cart.html", melons=melon_counts, order_total=order_total)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types melon_data = {} for melon_id in session['cart']: melon_data.setdefault(melon_id, {}) melon_data[melon_id].setdefault('count', 0) melon_data[melon_id]['count'] += 1 melon_data[melon_id]['melon'] = melons.get_by_id(melon_id) print melon_data return render_template("cart.html", melon_data=melon_data)
def shopping_cart(): """Display content of shopping cart.""" #TODO: Display the contents of the shopping cart. # The logic here will be something like: # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types # ex. session is {"shopping cart": [44, 31, 18]} list_of_ids = session["shopping_cart"] session.setdefault("cart", {}) # while list_of_ids != []: for melon_id in list_of_ids: #if melon_id in list_of_ids: # we have the id, now we need to get its price and name melon = melons.get_by_id(melon_id) if melon_id in session["cart"]: session["cart"][melon_id]["qty"] += 1 session["cart"][melon_id]["subtotal"] = session["cart"][melon_id]["price"] * session["cart"][melon_id]["qty"] else: session["cart"][melon_id] = {"price": melon.price, "name": melon.common_name, "qty": 1, "subtotal": session["cart"][melon_id]["price"] * session["cart"][melon_id]["qty"] } cart = session["cart"] return render_template("cart.html", cart=cart)
def shopping_cart(): """Display content of shopping cart.""" cart = {} order_total = 0 for item in session["cart"]: if item in cart: cart[item] += 1 else: cart[item] = 1 for melon_id in cart.keys(): melon = melons.get_by_id(melon_id) melon_quantity = cart[melon_id] melon_name = melon.common_name melon_price = melon.price cart[melon_id] = [melon_name, melon_quantity, melon_price] for melon in cart: order_total = order_total + melon_quantity * melon_price return render_template("cart.html", cart=cart, order_total=order_total)
def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # - get the cart dictionary from the session melon_cart = session['cart'] # - create a list to hold melon objects and a variable to hold the total # cost of the order melon_list = [] # for mel in melon_id_list: # melon_list.append(melon_cart[mel]) order_total = 0 for mel_id in melon_cart: melon_object = melons.get_by_id(mel_id) qty = melon_cart[mel_id] cost = qty * melon_object.price order_total += cost melon_object.quantity = qty melon_object.cost = cost melon_list.append(melon_object) return render_template("cart.html", melon_list=melon_list, order_total=order_total)
def add_to_cart(id1): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Successfully added to cart'. """ id1 = str(id1) # get cart from session or create it if it doesn't already exist cart = session.setdefault("cart", {}) print cart # add 1 to the quantity of melons of type <id> (which will be 0 if we # haven't yet put any of that kind of melon into the cart) cart[id1] = cart.setdefault(id1, 0) + 1 print cart # add a confirmation message to the "flash" messages buffer melon_type = melons.get_by_id(int(id1)).common_name.lower() print melon_type flash_message = "Successfully added a {type} melon to your cart." flash(flash_message.format(type=melon_type)) return redirect("/cart")
def show_shopping_cart(): """Display content of shopping cart.""" # - get the cart dictionary from the session cart = session.get("cart") # - create a list to hold melon objects cart_list = [] # create a variable to hold the total cost of the order order_total = 0 # - loop over the cart dictionary, and for each melon id: # - get the corresponding Melon object # - compute the total cost for that type of melon # - add this to the order total # - add quantity and total cost as attributes on the Melon object # - add the Melon object to the list created above # Make sure your function can also handle the case wherein no cart has # been added to the session if cart: for melon in cart: melon_object = melons.get_by_id(melon) melon_object.quantity = cart[melon] melon_object.total_cost = melon_object.price * melon_object.quantity order_total = order_total + melon_object.total_cost cart_list.append(melon_object) # - pass the total order cost and the list of Melon objects to the template return render_template("cart.html", order_total=order_total, cart_list=cart_list)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types # creates an empty dictionary to store information about each # melon in the specific user's cart cart = {} # creates a list of id's of melon's in the user's cart # if the cart is empty, the list will default to [] melon_ids_in_cart = session.get("cart", []) # iterates through the melon id's in cart to update quantities # keeps track of total price for each melon for melon_id in melon_ids_in_cart: # if the melon is not already in the cart, create a key of melon id # the value is a dictionary containing melon info if melon_id not in cart: melon_obj = melons.get_by_id(melon_id) cart[melon_id] = { "melon_name": melon_obj.common_name, "qty_ordered": 1, "unit_price": melon_obj.price, "total_price": melon_obj.price, } # if melon is already in cart, increment quantity and total cost else: cart[melon_id]["qty_ordered"] += 1 cart[melon_id]["total_price"] = ( cart[melon_id]["qty_ordered"] * cart[melon_id]["unit_price"]) # formats numerical values as monetary strings in a new key: value # pair in the melon dictionary we created above cart[melon_id]["unit_price_str"] = "${:,.2f}".format( cart[melon_id]["unit_price"]) cart[melon_id]["total_price_str"] = "${:,.2f}".format( cart[melon_id]["total_price"]) # initialize variable to count total cost total = 0 # for every melon in cart, add the total price to the total for melon in cart: total += cart[melon]["total_price"] total = "${:,.2f}".format(total) return render_template("cart.html", cart=cart, total=total)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types ordered_melons = {} total = 0 if 'cart' in session: for melon_id in set(session['cart']): ordered_melons[melon_id] = {} ordered_melons[melon_id]['quantity'] = session["cart"].count(melon_id) melon = melons.get_by_id(melon_id) ordered_melons[melon_id]['name'] = melon.common_name ordered_melons[melon_id]['price'] = melon.price total = total + (ordered_melons[melon_id]['quantity'] * ordered_melons[melon_id]['price']) # flash(ordered_melons) # Test code to see dictionary return render_template("cart.html", ordered_melons=ordered_melons, total=total)
def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the cart dictionary from the session #session["cart"] # - create a list to hold melon objects and a variable to hold the total # cost of the order cart = [] total = 0 # - loop over the cart dictionary, and for each melon id: for melon_id in session["cart"]: # - get the corresponding Melon object melon = melons.get_by_id( melon_id) # gives melon object when takes in melon_id # - compute the total cost for that type of melon melon_total = melon.price * session["cart"][ melon_id] # price * quantity # - add this to the order total total += melon_total # - add quantity and total cost as attributes on the Melon object melon.quantity = session["cart"][melon_id] melon.melon_total = melon_total # - add the Melon object to the list created above cart.append(melon) # - pass the total order cost and the list of Melon objects to the template # # Make sure your function can also handle the case wherein no cart has # been added to the session print(cart) return render_template("cart.html", total=total, cart=cart)
def add_to_cart(melon_id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Melon successfully added to cart'.""" # - check if a "cart" exists in the session, and create one (an empty # dictionary keyed to the string "cart") if not # - check if the desired melon id is the cart, and if not, put it in # - increment the count for that melon id by 1s # - flash a success message # - redirect the user to the cart page if 'cart' not in session: session['cart'] = {} session['cart'][melon_id] = session['cart'].get(melon_id, 0) + 1 melon = melons.get_by_id(melon_id) flash(f'{melon.common_name.title()} was successfully added.') return redirect('/cart')
def show_shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the cart dictionary from the session # - create a list to hold melon objects and a variable to hold the total # cost of the order # - loop over the cart dictionary, and for each melon id: # - get the corresponding Melon object # - compute the total cost for that type of melon # - add this to the order total # - add quantity and total cost as attributes on the Melon object # - add the Melon object to the list created above # - pass the total order cost and the list of Melon objects to the template # # Make sure your function can also handle the case wherein no cart has # been added to the session cart = session["cart"] print('This is the cart dict!', cart) for melon_key in cart.keys(): melon = melons.get_by_id(melon_key) print("melon object:", melon) return render_template("cart.html")
def add_to_cart(id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Successfully added to cart'. """ # TODO: Finish shopping cart functionality # The logic here should be something like: # # - add the id of the melon they bought to the cart in the session # on adding an item, check to see if the session contains a cart # if no, add a new cart to the session # append the melon id under consideration to our cart if session['cart']: session["cart"].append(id) else: session['cart'] = [id] # flash the message indicating the melon was added to the cart flash('You have added ' + melons.get_by_id(id).common_name + ' to your cart!!!') # redirect user to shopping cart route return redirect("/cart")
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types total_cost = 0 melon_items = {} for melon_id in session["cart"]: #iterate through the cart list quantity = session["cart"].count(melon_id) #current melon is melon object current_melon = melons.get_by_id(melon_id) melon_total = quantity * current_melon.price total_cost += melon_total melon_items[melon_id] = { "melon": current_melon.common_name, "qty": quantity, "price": current_melon.price, "mel_total": melon_total } return render_template("cart.html", total_cost=total_cost, melon_items=melon_items)
def show_shopping_cart(): """Display content of shopping cart.""" cart_dict = session["cart"] cart_list = [] grand_total = 0 for melon_id in cart_dict: melon = melons.get_by_id(melon_id) melon.quantity = session["cart"][melon_id] melon.total_price = float(melon.price) * melon.quantity grand_total += melon.total_price cart_list.append(melon) # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the cart dictionary from the session # - create a list to hold melon objects and a variable to hold the total # cost of the order # - loop over the cart dictionary, and for each melon id: # - get the corresponding Melon object # - compute the total cost for that type of melon # - add this to the order total # - add quantity and total cost as attributes on the Melon object # - add the Melon object to the list created above # - pass the total order cost and the list of Melon objects to the template # # Make sure your function can also handle the case wherein no cart has # been added to the session return render_template("cart.html", final_cart_list=cart_list, final_total=grand_total)
def add_to_cart(melon_id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Melon successfully added to cart'.""" # TODO: Finish shopping cart functionality # The logic here should be something like: # # - check if a "cart" exists in the session, and create one (an empty # dictionary keyed to the string "cart") if not # - check if the desired melon id is the cart, and if not, put it in # - increment the count for that melon id by 1 # - flash a success message # - redirect the user to the cart page quantity = int(request.args.get("quantity")) if "cart" not in session: session["cart"] = {} session["cart"][melon_id] = quantity flash("Added to cart") melon = melons.get_by_id(melon_id) return render_template("melon_details.html", display_melon=melon)
def add_to_cart(melon_id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Melon successfully added to cart'.""" # TODO: Finish shopping cart functionality # The logic here should be something like: # # - check if a "cart" exists in the session, and create one (an empty # dictionary keyed to the string "cart") if not # - check if the desired melon id is the cart, and if not, put it in # - increment the count for that melon id by 1 # - flash a success message # - redirect the user to the cart page melon_id = request.path.split("/")[-1] if "cart" in session: pass else: session['cart'] = {} session['cart'][melon_id] = session['cart'].get(melon_id, 0) + 1 flash("Successfully added {} to cart".format( melons.get_by_id(melon_id).common_name)) print(session['cart']) return redirect("/cart")
def add_to_cart(melon_id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Melon successfully added to cart'.""" # TODO: Finish shopping cart functionality # The logic here should be something like: # # - check if a "cart" exists in the session, and create one (an empty # dictionary keyed to the string "cart") if not if "cart" not in session.keys(): session["cart"] = {} # - check if the desired melon id is the cart, and if not, put it in cart_ids = session["cart"].keys() if melon_id not in cart_ids: session["cart"][melon_id] = 1 else: session["cart"][melon_id] += 1 # - increment the count for that melon id by 1 # - flash a success message flash("You have successfully added %s to your cart." % melons.get_by_id(melon_id).common_name) # - redirect the user to the cart page return redirect("/cart")
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types shopping_list = session["cart"] melon_id_dictionary = {} for id in shopping_list: if id in melon_id_dictionary: melon_id_dictionary[id]["qty"] += 1 melon_id_dictionary[id]["subtotal"] += melons.get_by_id(id).price else: melon_id_dictionary[id] = {} melon_id_dictionary[id]["qty"] = 1 melon_id_dictionary[id]["price"] = melons.get_by_id(id).price melon_id_dictionary[id]["common_name"] = melons.get_by_id(id).common_name melon_id_dictionary[id]["subtotal"] = melon_id_dictionary[id]["price"] order_total = 0 for melon in melon_id_dictionary: order_total += melon_id_dictionary[melon]["subtotal"] # for id in shopping_list: # melon_id_dictionary.setdefault(id, {}) # if id in melon_id_dictionary: # melon_id_dictionary[id]["qty"] += 1 # melon_id_dictionary[id]["subtotal"] += melons.get_by_id(id).price # else: # melon_id_dictionary[id]["qty"] = 1 # melon_id_dictionary[id]["price"] = melons.get_by_id(id).price # melon_id_dictionary[id]["common_name"] = melons.get_by_id(id).common_name # melon_id_dictionary[id]["subtotal"] = melons.get_by_id(id).price return render_template("cart.html", melon_id_dictionary = melon_id_dictionary, total="%.2f" % order_total)
def show_melon(melon_id): """Return page showing the details of a given melon. Show all info about a melon. Also, provide a button to buy that melon. """ melon = melons.get_by_id(melon_id) print melon return render_template("melon_details.html", display_melon=melon)
def show_melon(melon_id): """Return page showing the details of a given melon. Show all info about a melon. Also, provide a button to buy that melon. """ #What is the connecting point between melon_id and melon.id melon = melons.get_by_id(melon_id) print melon return render_template("melon_details.html", display_melon=melon) #display_melon is place jinja place holder.
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types # TODO: turn the session to have a key of cart, value is a dictionary # session = cart{ # {id {quantity: 1}} # {id {quantity: 1}} # print "session[cart] is", session["cart"] # # sum = 0 if 'cart' not in session: session["cart"] = {} shopping_cart = {} total_cart =0 for id in session["cart"]: # import pdb; pdb.set_trace() common_name = melons.get_by_id(int(id)).common_name price_int = melons.get_by_id(int(id)).price price = "${:.2f}".format(melons.get_by_id(int(id)).price) quantity = session["cart"][id] total = "${:.2f}".format(price_int * quantity) shopping_cart[id] = {"common_name": common_name, "price": price, "quantity": quantity, "total": total} total_cart += price_int * quantity return render_template("cart.html", shopping_cart=shopping_cart, total_cart=total_cart)
def shopping_cart(): """Display content of shopping cart.""" melon_tally = {} cart_items = session["cart"] for melon_id in set(cart_items): melon = melons.get_by_id(melon_id) # print melon_tally.get(melon.common_name, "not here") quantity = cart_items.count(melon_id) melon_info = [quantity, melon.price] melon_tally[melon.common_name] = melon_info # melon_tally[melon.common_name] = melon_tally.get(melon.common_name, 0) +1 # melon_tally[melon.common_name].append(melon.price) #print melon_price # quanity = melon_tally.get(melon.common_name) # melon[dixie_queen] = [qty, price] # qty_price = melon[dixie_queen] # qty_price[0] + 1 # print melon.common_name, melon.price print melon_tally # for stuff in melon_tally.items(): # print stuff, stuff[1] * melon.price #still a work in progress, but this give us the total price per melon! #print melon_tally[""] * melon.price # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types return render_template("cart.html")
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types # FIRST VERSION # melons_only.pop('_flash') # melons_only = session.keys() # cart = {} # for i in melons_only: # print "this is i:", session[i], i # melon_quantity = session[i] # melon_name = this_melon.common_name # melon_price = this_melon.price # melon_total = melon_price * melon_quantity melons_in_cart = {} for item in session['cart']: if item in melons_in_cart.keys(): melons_in_cart[item]['quantity'] += 1 else: this_melon = melons.get_by_id(item) melons_in_cart.setdefault(item, {}) melons_in_cart[item].setdefault('quantity', 1) melons_in_cart[item]['name'] = this_melon.common_name melons_in_cart[item]['price'] = this_melon.price melons_in_cart[item]['total'] = (melons_in_cart[item]['price'] * melons_in_cart[item]['quantity']) super_total = 0 for item in melons_in_cart.keys(): super_total += melons_in_cart[item]['total'] # print "*** THIS IS OUR CART ****", melons_in_cart return render_template("cart.html", cart = melons_in_cart, super_total = super_total)
def shopping_cart(): """Display content of shopping cart.""" # TODO: Display the contents of the shopping cart. # The logic here will be something like: # # - get the list-of-ids-of-melons from the session cart # - loop over this list: # - keep track of information about melon types in the cart # - keep track of the total amt ordered for a melon-type # - keep track of the total amt of the entire order # - hand to the template the total order cost and the list of melon types order_total = 0 # get the list of melon ids from the session dictionary (cart key), or an # empty list if it doesn't exist cart_ids = session.get('cart', []) # keep track of melon info in cart dictionary cart_dict = {} # for every melon id in the cart ids list, if the melon id is in the # cart dictionary, melon info will be bound to the values of a melon for melon_id in cart_ids: if melon_id in cart_dict: melon_info = cart_dict[melon_id] # if the melon id is not in the cart dictionary, pull in the melon object # and the melon info will be bound to the values of the melon else: melon_object = melons.get_by_id(melon_id) melon_info = cart_dict[melon_id] = { 'common_name': melon_object.common_name, 'unit_cost': melon_object.price, 'qty': 0, 'total_cost': 0, } # increment quantity and melon subtotal by this melon in cart list melon_info['qty'] += 1 melon_info['total_cost'] += melon_info['unit_cost'] # increment order total by this melon order_total += melon_info['unit_cost'] # cart variable will be bound to a list of melon values displaying melon info cart = cart_dict.values() return render_template("cart.html", cart=cart, order_total=order_total)
def add_to_cart(id): """Add a melon to cart and redirect to shopping cart page. When a melon is added to the cart, redirect browser to the shopping cart page and display a confirmation message: 'Successfully added to cart'. """ # - add the id of the melon they bought to the cart in the session session.setdefault('cart_items', []).append(id) melon = melons.get_by_id(id) #Flash a message indicating the melon was successfully added to the cart. flash(melon.common_name + " was added to cart") return redirect("/cart")