本文整理汇总了Python中model.get_melon_by_id函数的典型用法代码示例。如果您正苦于以下问题:Python get_melon_by_id函数的具体用法?Python get_melon_by_id怎么用?Python get_melon_by_id使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_melon_by_id函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: shopping_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."""
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)
示例2: shopping_cart
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")
示例3: shopping_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 = {}
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)
示例4: shopping_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."""
# 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)
示例5: shopping_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."""
#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)
示例6: shopping_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 = {}
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)
示例7: shopping_cart
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)
示例8: shopping_cart
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)
示例9: shopping_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)
示例10: show_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
return render_template("melon_details.html",
display_melon = melon)
示例11: add_to_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" """
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')
示例12: shopping_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."""
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)
示例13: shopping_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."""
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'])
示例14: shopping_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_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)
示例15: shopping_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 = []
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)