本文整理汇总了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函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:lenazun,项目名称:melonwebapp,代码行数:31,代码来源:melons.py
示例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")
开发者ID:bizmills,项目名称:ubermelon_webapp,代码行数:34,代码来源:melons.py
示例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)
开发者ID:DanielleYasso,项目名称:Ubermelon_webapp,代码行数:30,代码来源:melons.py
示例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)
开发者ID:hdjewel,项目名称:Ubermelon_Webapp,代码行数:33,代码来源:lp_melons.py
示例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)
开发者ID:carlydacosta,项目名称:ubermelon-flask-webapp,代码行数:26,代码来源:melons.py
示例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)
开发者ID:etothemanders,项目名称:ubermelon_webapp,代码行数:27,代码来源:melons.py
示例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)
开发者ID:magshi,项目名称:ubermelon_webapp,代码行数:32,代码来源:melons.py
示例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)
开发者ID:lzhour,项目名称:ubm_webapp,代码行数:32,代码来源:melons.py
示例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)
开发者ID:bnak,项目名称:Hackbright_Curriculum,代码行数:7,代码来源:melons.py
示例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)
开发者ID:jmcelroy5,项目名称:Ubermelon_Flask_Webapp,代码行数:7,代码来源:melons.py
示例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')
开发者ID:savannahjune,项目名称:ubermelonwebapp,代码行数:27,代码来源:melons.py
示例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)
开发者ID:jmcelroy5,项目名称:Ubermelon_Flask_Webapp,代码行数:29,代码来源:melons.py
示例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'])
开发者ID:bnak,项目名称:Hackbright_Curriculum,代码行数:12,代码来源:melons.py
示例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)
开发者ID:daphnejwang,项目名称:MelonWebApp,代码行数:13,代码来源:melons.py
示例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)
开发者ID:jgrist,项目名称:ubermelon_app,代码行数:14,代码来源:melons.py
示例16: 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_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)
开发者ID:bookstein,项目名称:ubermelon_webapp,代码行数:14,代码来源:melons.py
示例17: add_to_cart
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)
开发者ID:ktel1218,项目名称:Ubermelon,代码行数:14,代码来源:melons.py
示例18: 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" """
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")
开发者ID:bnak,项目名称:Hackbright_Curriculum,代码行数:15,代码来源:melons.py
示例19: 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_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)
开发者ID:arevalomalina,项目名称:ubermelon_webapp2,代码行数:16,代码来源:melons.py
示例20: shopping_cart
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)
开发者ID:ktel1218,项目名称:Ubermelon,代码行数:16,代码来源:melons.py
注:本文中的model.get_melon_by_id函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论