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