Skip to content

Instantly share code, notes, and snippets.

@chriszf
Created June 24, 2014 18:22
Show Gist options
  • Save chriszf/b6072c204918e4223146 to your computer and use it in GitHub Desktop.
Save chriszf/b6072c204918e4223146 to your computer and use it in GitHub Desktop.
@app.route("/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_ids = session.get("my_melons")
melon_qty = {}
for melon_id in melon_ids:
if melon_qty.get(melon_id):
melon_qty[melon_id] += 1
else:
melon_qty[melon_id] = 1
print melon_qty
melon_dict = {}
for melon_id, qty in melon_qty.items():
melon = model.get_melon_by_id(melon_id)
melon_dict[melon] = qty
print melon_dict
return render_template("cart.html",
melons = melon_dict)
@app.route("/add_to_cart/<int:id>")
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 not session.get("my_melons"):
session["my_melons"] = []
session["my_melons"].append(id)
print session
flash("Successfully added to cart")
return redirect("/cart")
{% extends 'base.html' %}
{% block body %}
<table class="table table-striped">
<tr><th>Melon Name</th><th>Quantity</th><th>Price</th><th>Total</th></tr>
{% for melon, qty in melons.items() %}
<tr><td>{{ melon.common_name }}</td><td>{{ qty }}</td><td>{{ melon.price_str() }}</td><td>$15.00</td></tr>
{% endfor %}
</table>
<h3>Total: $27.00</h3>
<a href="/checkout" class="btn btn-large btn-primary">Check Out</a>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment