def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" if 'cart' in session: id_list = session['cart'] else: flash ('Your cart is empty') session['cart'] = [] id_list = session['cart'] melon_dict = {} total = 0 for i in id_list: melon_name = model.get_melon_by_id(i).common_name melon_price = float(model.get_melon_by_id(i).price) if i not in melon_dict: melon_quantity = 1 else: melon_quantity += 1 melon_subtotal = (melon_price * melon_quantity) melon_dict[i] = {'name': melon_name, 'price': melon_price, 'quantity': melon_quantity, 'subtotal': melon_subtotal} total += melon_dict[i]['price'] return render_template("cart.html", current_list= melon_dict, total= total)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" if session.get('cart') == None: session['cart'] = {} current_cart = [] total = 0.00 return render_template("cart.html", current_cart=current_cart, total=total) else: current_cart = [] melon_ids = session['cart'].keys() for melon_id in melon_ids: temp_melon = model.get_melon_by_id(melon_id) temp_quantity = session['cart'][melon_id] temp_melon_total = temp_melon.price * temp_quantity temp_melon_total = "$%.2f" % temp_melon_total current_cart.append((temp_melon, temp_quantity, temp_melon_total)) print current_cart total = 0 for tup in current_cart: total = total + tup[0].price * float(tup[1]) total = float(total) pretty_total = "$%.2f" % total return render_template("cart.html", current_cart=current_cart, total=pretty_total)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melon_dict = {} cart_list = session.get("cart", []) if not cart_list: flash("Your cart is empty") # create a dictionary where key = melon_id and value = dictionary with key=quantity, key=total for melon_id in cart_list: melon_dict.setdefault(melon_id, {}) melon_dict[melon_id]["quantity"] = melon_dict[melon_id].get("quantity", 0) + 1 melon_info_list = [model.get_melon_by_id(melon_id) for melon_id in melon_dict] cart_total = 0 for melon in melon_info_list: quantity = melon_dict[melon.id]["quantity"] total = melon.price * quantity cart_total += total melon_dict[melon.id]["total"] = total melon_dict[melon.id]["formatted_price"] = "$%.2f" % melon.price melon_dict[melon.id]["formatted_total_price"] = "$%.2f" % total cart_total_string = "Total: $%.2f" % cart_total return render_template("cart.html", melon_list = melon_info_list, quantotals = melon_dict, cart_total = cart_total_string)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" if 'cart' in session: if 'cart' != {}: cart_items = session['cart'] melon_details = [] for id, quantity in cart_items.iteritems(): melon = model.get_melon_by_id(id) melon_dict = { "name": melon.common_name, "price": melon.price, "quantity": quantity, "total": melon.price * quantity } melon_details.append(melon_dict) order_total = 0 for melon in melon_details: order_total += melon['total'] return render_template("cart.html", melons = melon_details, order_total = order_total) else: return render_template("cart.html", melons = None, order_total = 0)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" if "cart" not in session: session["cart"] = {} new_qty = request.args.get('quantity', None) if new_qty: session["cart"][request.args.get("id")] = int(new_qty) melon_list = [] for id, qty in session["cart"].items(): melon = model.get_melon_by_id(id) melon_list.append((id, melon, qty)) sum = 0 for id, melon, qty in melon_list: sub_total = melon.price * qty sum = (sum + sub_total) sum = "%.2f" % sum sub_total_dictionary = {} for id, melon, qty in melon_list: sub_total = melon.price * qty sub_total = "%.2f" % sub_total sub_total_dictionary[melon.common_name] = sub_total return render_template("cart.html", melon_list=melon_list, sum=sum, sub_total_dictionary=sub_total_dictionary)
def shopping_cart(): """Shopping cart page: displays the contents of the shopping cart with a list of melons, quantities, prices, and total prices held in this session.""" list_of_melons = session.get('ourcart', []) # for melon_id in list_of_melons: # melon = model.get_melon_by_id(melon_id) # melon_name_list.append(melon.common_name) # melon_price_list.append(melon.price) melon_qty = {} for melon_id in list_of_melons: if melon_qty.get(melon_id): melon_qty[melon_id] += 1 else: melon_qty[melon_id] = 1 print melon_qty melon_objects = {} for melon_id, qty in melon_qty.items(): melon_obj = model.get_melon_by_id(melon_id) melon_objects[melon_obj] = qty cart_total = 0; for melon, qty in melon_objects.items(): cart_total += melon.price * qty print melon_objects return render_template("cart.html", melons = melon_objects, total = cart_total)
def add_to_cart(id): """TODO: Finish shopping cart functionality using session variables to hold cart list. Intended behavior: when a melon is added to a cart, redirect them to the shopping cart page, while displaying the message "Successfully added to cart" """ temp_id=str(id) if 'cart' in session: # if temp_id in session['cart']: # session['cart'][temp_id] += 1 # else: # session['cart'][temp_id] = 1 session['cart'][temp_id] = session['cart'].get(temp_id, 0) + 1 else: print "initializing cart in session" session['cart'] = {temp_id: 1} display_melon = model.get_melon_by_id(id) flash("You successfully added one " + display_melon.common_name +" to your cart!") return redirect('/cart')
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melons = [ (model.get_melon_by_id(int(id)), count) for id, count in session.setdefault("cart", {}).items() ] total = sum([melon[0].price * melon[1] for melon in melons]) return render_template("cart.html", melons = melons, total=total)
def show_melon(id): """This page shows the details of a given melon, as well as giving an option to buy the melon.""" melon = model.get_melon_by_id(id) print melon return render_template("melon_details.html", display_melon = melon)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" if "cart" not in session: flash("There is nothing in your cart.") return render_template("cart.html", display_cart={}, total=0) else: items = session["cart"] dict_of_melons = {} total_price = 0 for item in items: melon = model.get_melon_by_id(item) total_price += melon.price if melon.id in dict_of_melons: dict_of_melons[melon.id]["qty"] += 1 else: dict_of_melons[melon.id] = { "qty": 1, "name": melon.common_name, "price": melon.price } return render_template("cart.html", display_cart=dict_of_melons, total=total_price)
def show_melon(id): """This page shows the details of a given melon, as well as giving an option to buy the melon.""" melon = model.get_melon_by_id(id) return render_template("melon_details.html", display_melon = melon)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" #assigning the variable cart_items to the 'cart' value, which as indicated is a dictionary itself, with ids as keys and qty as values. cart_items = session['cart'] #we initialize an empty list b/c in our for loop, we are creating a list of dictionaries, with keys name, price, qty and values name, price, qty based on ids. melon_details = [] #this for loop interates over each id:qty in cart_items for id, quantity in cart_items.iteritems(): #call the get_melon_by_id() function so we can obtain all info for each melon. we will use this info below to create yet another dictionary melon = model.get_melon_by_id(id) #referencing attributes of melon and assigning as values to the melon_dict. melon_dict = { "name": melon.common_name, "price": melon.price, "quantity": quantity } #appending to the melon_details list initiated above the for loop melon_details.append(melon_dict) #the list melon_details is then passed to the html document, which iterates over this list to create the items in the cart. return render_template("cart.html", melons = melon_details)
def add_to_cart(id): """TODO: Finish shopping cart functionality using session variables to hold cart list. Intended behavior: when a melon is added to a cart, redirect them to the shopping cart page, while displaying the message "Successfully added to cart" """ melon = model.get_melon_by_id(id) melon_name = melon.common_name melon_price = melon.price if 'cart' not in session: session['cart'] = [] for a_melon_list in session['cart']: if a_melon_list[0] == melon_name: a_melon_list[1] = a_melon_list[1] + 1 flash('You just incremented the quantity of %s in your cart.' % melon_name) return render_template("cart.html", melons_in_cart=session['cart'], total=get_total()) session['cart'].append([melon_name, 1, melon_price]) flash('You just added %s to your cart.' % melon_name) return render_template("cart.html", melons_in_cart=session['cart'], melon_name=melon_name, melon_price=melon_price, total=get_total())
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" # this is where we add the logic to get the qty from the "session cart" # that was "built" the "add_to_cart" function. # then use the melon ids in the "cart" create a list of melons. # The melon details can be retrieved using the "model.get_melon_by_id". # get_melon_by_id returns id, melon_type, common_name, price, imgurl, # flesh_color, rind_color, seedless # Before passing the "melons" remember to calculate the "total price" # and add it to the retrieved list of attributes. if "cart" in session: melons = [] running_total = 0 for key in session["cart"]: melon = model.get_melon_by_id(key) total_price = melon.price * session["cart"][key] print total_price melons = melons + [[melon.common_name, session["cart"][key], melon.price, total_price]] print "melons =", print melons running_total = running_total + total_price print "Order total = ", running_total session["order total"] = running_total print "session = ", session return render_template("cart.html", melons = melons)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added.""" if "cart" not in session or len(session["cart"])== 0 : flash("There is nothing in your cart.") return render_template("cart.html", display_cart = {}, total = 0) else: if "user" in session and session["logged_in"] == True: customer = model.get_customer_by_email(str(session["user"])) flash("Welcome, %s %s" % (customer.givenname, customer.surname)) else: flash("That is an invalid login.") items = session["cart"] dict_of_melons = {} total_price = 0 for item in items: #print(item,"#############") melon = model.get_melon_by_id(item) total_price += melon.price if melon.id in dict_of_melons: dict_of_melons[melon.id]["qty"] += 1 else: dict_of_melons[melon.id] = {"qty":1, "name": melon.common_name, "price":melon.price} print(dict_of_melons) return render_template("cart.html", display_cart = dict_of_melons, total = total_price)
def shopping_cart(): # """TODO: Display the contents of the shopping cart. The shopping cart is a # list held in the session that contains all the melons to be added. Check # accompanying screenshots for details.""" if not 'cart' in session: flash('Your cart is empty!') return render_template("cart.html") cart_list = [] # session['cart'] == {1: 17, 2: 10} # melon #1 is 17, #2 is has 10 # cart_list == [(melon obj, 17), (melon obj, 10)] for melon_id, qty in session['cart'].items(): melon = model.get_melon_by_id(melon_id) melon_tuple = (melon, qty) cart_list.append(melon_tuple) print cart_list final_price = total_price(cart_list) return render_template("cart.html", cart_list=cart_list, final_price=final_price)
def shopping_cart(): # if there's nothing in the cart, redirect to melons page if 'cart' not in session: return redirect("/melons") # initialize empty dictionary for the melon count melon_count = {} # this block iterates through a list of melon IDs and generates a dictionary with key: melon id, value: # of occurrences for melon_id in session['cart']: if melon_count.get(melon_id): melon_count[melon_id] += 1 else: melon_count[melon_id] = 1 print melon_count melon_dict = {} cart_total = 0 #for each melon id, and the quantity for that melon type, in the melon_count dict, get the melon object associated with that melon id from the db using the model. Also calculates the total cost for the cart. for melon_id, qty in melon_count.iteritems(): melon = model.get_melon_by_id(melon_id) melon_dict[melon] = qty cart_total += (qty * melon.price) print melon_dict #convert the cart total price to a two-digit float string cart_total_float = "%0.2f" % cart_total #re-cast variables as variables we can pass into cart.html to use with jinja return render_template("cart.html", cart = melon_dict, total = cart_total_float)
def add_to_cart(id): # checking if there is a cart key in the session dict, and if not, adding it with the id as the value melon = model.get_melon_by_id(int(id)) this_melon_name = melon.common_name this_melon_price = melon.price if 'cart' not in session: session['cart'] = {} session['cart'][id] = [this_melon_name, this_melon_price, 1] else: if id not in session['cart']: session['cart'][id] = [this_melon_name, this_melon_price, 1] else: session['cart'][id][2] += 1 flash(this_melon_name + " added to cart.") print session """TODO: Finish shopping cart functionality using session variables to hold cart list. Intended behavior: when a melon is added to a cart, redirect them to the shopping cart page, while displaying the message "Successfully added to cart" """ return redirect("/melons")
def shopping_cart(): if "cart" in session: cart_list = session["cart"] melon_ids = {} for melon_id in cart_list: if melon_id in melon_ids: melon_ids[melon_id] += 1 else: melon_ids[melon_id] = 1 melon_dict = {} total = 0 for melon_id, quantity in melon_ids.items(): melon = model.get_melon_by_id(melon_id) melon_dict[melon_id] = { "common_name": melon.common_name, "qty": quantity, "price": melon.price, "subtotal": melon.price * quantity } total += melon_dict[melon_id]['subtotal'] basket = melon_dict.keys() # print "TESTING DICTIONARY", melon_ids print melon_dict return render_template("cart.html", melon_dict = melon_dict, basket = basket, total = total) """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" return render_template("cart.html")
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melons = {} order_total=0 if 'cart' in session: for melon_id in session["cart"]: if melons.get(melon_id) == None: melons[melon_id] = {} melons[melon_id]['melon']=model.get_melon_by_id(melon_id) melons[melon_id]["qty"]=1 else: melons[melon_id]['qty'] += 1 melons[melon_id]['total']= melons[melon_id]["qty"]*melons[melon_id]['melon'].price for melon_id in melons: order_total=order_total+float(melons[melon_id]['total']) order_total="$%.2f" % order_total return render_template("cart.html", melon_dict = melons, order_total = order_total) else: return render_template("cart.html", melon_dict = {}, order_total = 0)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melons = [(model.get_melon_by_id(int(id)), count) for id, count in session.setdefault("cart", {}).items()] total = sum([melon[0].price * melon[1] for melon in melons]) return render_template("cart.html", melons=melons, total=total)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" tally = collections.Counter(session['cart']) purchased_melons = {m_id: model.get_melon_by_id(m_id) for m_id in tally} if not purchased_melons: flash("Buy melons!") return render_template("cart.html", melon_list=purchased_melons, qty=tally, total_price=session['total'])
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melon_types = {} total_price = 0.00 if "cart" in session: for key, quantity in session["cart"].iteritems(): melon = model.get_melon_by_id(int(key)) total = melon.price * quantity melon_types[melon.common_name] = {'quantity': quantity, 'price': melon.price, 'total': total} total_price += total return render_template("cart.html", display_melons=melon_types, total_price = total_price)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" shopping_cart = [] total = 0 for id in session['cart']: melon = model.get_melon_by_id(id) shopping_cart.append(melon) total += (melon.price * session['cart'][id]) return render_template("cart.html", cart=shopping_cart, total=total)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melons_in_cart = [] total = 0 if "cart" in session: for id in session["cart"]: melon = model.get_melon_by_id(id) melons_in_cart.append(melon) total += (melon.price * session["cart"][id]) return render_template("cart.html", melons = melons_in_cart, total = total)
def add_to_cart(id): session.setdefault('shopping_cart', []).append(id) melon = model.get_melon_by_id(id) """TODO: Finish shopping cart functionality using session variables to hold cart list. Intended behavior: when a melon is added to a cart, redirect them to the shopping cart page, while displaying the message "Successfully added to cart" """ flash(Markup("Successfully added to <a href='/cart'>cart</a>")) # return render_template("cart.html") return render_template("melon_details.html", display_melon = melon)
def show_melon(id): """This page shows the details of a given melon, as well as giving an option to buy the melon.""" melon = model.get_melon_by_id(id) print melon # in session dict, have dict 'cart' # values in dict cart are {id: [name,price]} if 'cart' not in session: session['cart'] = {} session['cart'][melon.id]= list() session['cart'][melon.id].append(melon.common_name) session['cart'][melon.id].append(melon.price) return render_template("melon_details.html", display_melon = melon)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melons = [] total = 0 if "cart" in session.keys(): for key in session["cart"].iterkeys(): melon = model.get_melon_by_id(str(key)) melons.append(melon) melon.quantity = session["cart"][str(key)] total += melon.quantity * melon.price return render_template("cart.html", cart_contents = melons, order_total = total)
def add_to_cart(id): """TODO: Finish shopping cart functionality using session variables to hold cart list. Intended behavior: when a melon is added to a cart, redirect them to the shopping cart page, while displaying the message "Successfully added to cart" """ session['cart'] = session.get('cart', []) + [id] session['total'] += model.get_melon_by_id(id).price print session flash("Successfully added to cart") return redirect("/cart")
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melon_dict = {} total = 0 for key in session['cart']: melon = model.get_melon_by_id(key) melon_dict[key] = {"name": melon.common_name, "quantity": session['cart'][key], "price": melon.price } total += melon_dict[key]["price"] * melon_dict[key]["quantity"] return render_template("cart.html", melon_dict=melon_dict, cart_total="%0.2f" % total)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" # make loop to get the id out of session # then get that melons deets from model.get_melon_by_id cart_contents = [] total = 0 for k, v in session["cart"].items(): melon_object = model.get_melon_by_id(k) cart_contents.append([melon_object, v]) total = total + (melon_object.price * v) return render_template("cart.html", cart_contents=cart_contents, total=total)
def shopping_cart(): melons = {} total = 0 ids = session.get('shopping_cart') if ids: for melon_id in ids: melon = model.get_melon_by_id(melon_id) melons.setdefault(melon_id, {'melon': None, 'count': 0}) melons[melon_id]['melon'] = melon melons[melon_id]['count'] += 1 total += melon.price """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" return render_template("cart.html", melons=melons, total=total)
def create_melon_dict(): melon_dict = {} if "melon_dict" not in session: session["melon_dict"] = {} cart_list = session.get("cart", []) if cart_list is not None: for id in cart_list: if id not in melon_dict: new_melon = model.get_melon_by_id(id) melon_info = [new_melon.common_name, float(new_melon.price), 1, new_melon.price*1.00] melon_dict[id] = melon_info else: melon_dict[id][2] += 1 melon_dict[id][3] = melon_dict[id][1] * melon_dict[id][2] session["melon_dict"] = melon_dict
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melon_quantity = session.get('cart') melon_dict = {} for melon_id, quantity in melon_quantity.items(): melon = model.get_melon_by_id(melon_id) print "this is melon: " , melon melon_dict[melon] = quantity print melon_dict return render_template("cart.html", melons = melon_dict)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melons_list = [] for key, value in session["cart"].iteritems(): melon = model.get_melon_by_id(key) melons_list.append({ "name": melon.common_name, "price": melon.price, "qty": value, "total": (melon.price * value) }) return render_template("cart.html", melons_in_cart = melons_list)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" current_cart = {} total = 0 if 'cart' in session: for key in session['cart']: melon = model.get_melon_by_id(key) #current_cart[key] = [melon.common_name, melon.price] #current_car[key] = melon so that the melon can be accessed current_cart[key] = {'common_name': melon.common_name, 'price': melon.price} total += melon.price * session['cart'][key] return render_template("cart.html",current_cart = current_cart, total = total) else: flash('You have nothing in your cart. Keep shopping') return redirect("/melons")
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melon_rows = [] total = 0 if "cart" in session: for melon_id in session["cart"]: melon = model.get_melon_by_id(melon_id) melon_rows.append(melon) subtotal = melon.price * session["cart"][str(melon.id)] total += subtotal else: flash("Your cart is empty.") return render_template("cart.html", melon_rows = melon_rows, total = total)
def shopping_cart(): if "cart" not in session: flash("Your cart is empty.") return redirect(url_for("list_melons")) full_cart = {} cart_total = 0 """ take melon_id and qty from session and build line to send to table in list melons""" for item in session["cart"]: melon_id = item[0] melon = model.get_melon_by_id(melon_id) melon.qty = item[1] full_cart[melon_id] = melon cart_total += melon.price * melon.qty return render_template("cart.html", full_cart = full_cart, total="$%.2f"%cart_total)
def add_to_cart(id): """TODO: Finish shopping cart functionality using session variables to hold cart list. Intended behavior: when a melon is added to a cart, redirect them to the shopping cart page, while displaying the message "Successfully added to cart" """ string_id = str(id) if 'cart' in session: session['cart'][string_id] = session['cart'].get(string_id, 0) + 1 else: session['cart'] = {string_id: 1} display_melon = model.get_melon_by_id(id) flash("You added a " + display_melon.common_name + " to your cart") return redirect("/cart")
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" # seshlist = list(session["cart"]) # cart = {} # cart["melon"] = seshlist full_info = [] total_price = 0 for info, quant in session["cart"].iteritems(): melon = model.get_melon_by_id(info) cart_items = (str(melon.id), melon.common_name, float(melon.price), melon.price * quant) full_info.append(cart_items) total_price = total_price + cart_items[3] print total_price return render_template("cart.html", cart = full_info, total = total_price)
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" melon_dict = {} total_sum = 0 for id in session.get("cart",{}): melon = model.get_melon_by_id(id) melon_dict[melon] = session["cart"][id] total_sum += melon.price*melon_dict[melon] melon_list = melon_dict.items() melon_list.sort(key=lambda melon: melon[0].common_name) return render_template("cart.html", cart = melon_list, total = "$%.2f" % total_sum)
def add_to_cart(id): """TODO: Finish shopping cart functionality using session variables to hold cart list. Intended behavior: when a melon is added to a cart, redirect them to the shopping cart page, while displaying the message "Successfully added to cart" """ # session.clear() id = str(id) if 'cart' in session: if id in session['cart']: session['cart'][id] += 1 else: session['cart'][id] = 1 else: session['cart'] = {id: 1} melon = model.get_melon_by_id(id) flash("Successfully added %s to cart!" % str(melon.common_name)) return redirect("/cart")
def add_to_cart(id): """TODO: Finish shopping cart functionality using session variables to hold cart list. Intended behavior: when a melon is added to a cart, redirect them to the shopping cart page, while displaying the message "Successfully added to cart" """ if session.get('cart'): session['cart'].append(id) else: session['cart'] = [id] # could add show_cart function for lines below in order to allow all computation to be done only when /cart is viewed melon_dict = {} # get quantity counts, create dict for melon_id: [qty,] as key:value for i in session['cart']: if melon_dict.get(i): melon_dict[i][0] += 1 else: melon_dict[i] = [] melon_dict[i].append(1) # get melon info, append to value list for each melon_id: [qty, common_name, price, total] for k in melon_dict: result = model.get_melon_by_id(k) quantity = melon_dict[k][0] melon_dict[k].append(result.common_name) melon_dict[k].append(result.price) melon_dict[k].append(quantity*result.price) session['all_melon_dict'] = melon_dict total = 0 for item in melon_dict: total += melon_dict[item][3] session['total'] = total html = render_template("cart.html") return html
def shopping_cart(): """TODO: Display the contents of the shopping cart. The shopping cart is a list held in the session that contains all the melons to be added. Check accompanying screenshots for details.""" dictionary = {} total = 0 if not session: flash("Your cart is empty.") else: for key in session['cart']: melon = model.get_melon_by_id(key) dictionary[key] = { "name": melon.common_name, "quantity": session['cart'][key], "price": melon.price } total = total + dictionary[key]["price"] * dictionary[key][ "quantity"] return render_template("cart.html", dictionary=dictionary, totalcart=total)
def add_to_cart(id): # id = melon id from melon_details.html melon = model.get_melon_by_id(id) cart = session.get('cart', {}) if melon.common_name in cart: cart[melon.common_name]["qty"] += 1 else: cart[melon.common_name] = {"qty": 1, "price": melon.price} flash("%s was sucessfully added to the cart!" % melon.common_name) total = 0 for melon, info in cart.iteritems(): total += (info["qty"] * info["price"]) session['cart'] = cart session['total'] = total print cart return shopping_cart()
def add_to_cart(melon_id): # import pdb; pdb.set_trace() # """TODO: Finish shopping cart functionality using session variables to hold # cart list. # Intended behavior: when a melon is added to a cart, redirect them to the # shopping cart page, while displaying the message # "Successfully added to cart" """ if not 'cart' in session: session['cart'] = {} session['cart'][melon_id] = 1 print "Here!" else: if session['cart'].get(melon_id) == None: flash('melon added!') session['cart'][melon_id] = 1 else: session['cart'][melon_id] += 1 flash('melon added!') cart_list = [] for key in session['cart']: melon = model.get_melon_by_id(key) melon_tuple = (melon, session['cart'][key]) cart_list.append(melon_tuple) print cart_list final_price = total_price(cart_list) return render_template("cart.html", cart_list=cart_list, final_price=final_price)
def checkout(): """TODO: Implement a payment system. For now, just return them to the main melon listing page.""" flash("恭喜您! 結帳完成!!") items = session["cart"] dict_of_melons = {} total_price = 0 for item in items: melon = model.get_melon_by_id(item) total_price += melon.price if melon.id in dict_of_melons: dict_of_melons[melon.id]["qty"] += 1 else: dict_of_melons[melon.id] = { "qty": 1, "name": melon.common_name, "price": melon.price } session.pop('cart') return render_template("checkout.html", display_cart=dict_of_melons, total=total_price)
def show_cart(): melons = [(model.get_melon_by_id(int(id)), count) for id, count in session.setdefault("cart", {}).items()] total = sum([melon[0].price * melon[1] for melon in melons]) return render_template("_cart_items.html", melons=melons, total=total)