from flask import Flask, request, session, render_template, g, redirect, url_for, flash
import model
import jinja2
import os

app = Flask(__name__)
app.secret_key = '\xf5!\x07!qj\xa4\x08\xc6\xf8\n\x8a\x95m\xe2\x04g\xbb\x98|U\xa2f\x03'
app.jinja_env.undefined = jinja2.StrictUndefined
#controller function(CF) attached to page
#model function called(MF) (if any)
#template for page (TP)

#CF index, MF (none), TP index.html
@app.route("/")
def index():
    """This is the 'cover' page of the ubermelon site"""
    return render_template("index.html")

#CF list_melons, MF get_melons, TP all_melons.html
@app.route("/melons")
def list_melons():
    """This is the big page showing all the melons ubermelon has to offer"""
    melons = model.get_melons()
    return render_template("all_melons.html",
                           melon_list = melons)

#CF show_melon, MF get_melon_by_id, TP melon_details.html
@app.route("/melon/<int:id>")
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)

#CF shopping_cart, MF (none), TP cart.html
@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."""
    # 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)

#CF add_to_cart, MF (none), TP (none)
@app.route("/add_to_cart/<string: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 "cart" not in session:
        session["cart"] = {id:1}
        flash("Sucessfully added to NEW cart!")
    else:                                   
    # if we do have a cart    
        if id in session["cart"]:
            session["cart"][id] = session["cart"][id] + 1   
            # will equal the quantity
        else:
            session["cart"][id] = 1

        flash("Successfully added to cart!")

    return redirect("/cart")


#CF show_login, MF (none), TP login.html
@app.route("/login", methods=["GET"])
def show_login():
    return render_template("login.html")

#CF process_login, MF (none), TP (none)
@app.route("/login", methods=["POST"])
def process_login():
    """TODO: Receive the user's login credentials located in the 'request.form'
    dictionary, look up the user, and store them in the session."""
    email = request.form.get("email")
    password = request.form.get("password")
    message = model.get_customer_by_email(email, password)
    return render_template("userpage.html", message = message)

#CF checkout, MF (none), TP (none)
@app.route("/checkout")
def checkout():
    """TODO: Implement a payment system. For now, just return them to the main
    melon listing page."""
    flash("Sorry! Checkout will be implemented in a future version of ubermelon.")
    return redirect("/melons")

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(debug=True, port=port)

afb88bb4a0e3d5e9ae76bcba09dd4f5cb742bd8c
from flask import Flask, request, session, render_template, g, redirect, url_for, flash
import model
import jinja2
import os

app = Flask(__name__)
app.secret_key = '\xf5!\x07!qj\xa4\x08\xc6\xf8\n\x8a\x95m\xe2\x04g\xbb\x98|U\xa2f\x03'
app.jinja_env.undefined = jinja2.StrictUndefined
#controller function(CF) attached to page
#model function called(MF) (if any)
#template for page (TP)

#CF index, MF (none), TP index.html
@app.route("/")
def index():
    """This is the 'cover' page of the ubermelon site"""
    return render_template("index.html")

#CF list_melons, MF get_melons, TP all_melons.html
@app.route("/melons")
def list_melons():
    """This is the big page showing all the melons ubermelon has to offer"""
    melons = model.get_melons()
    return render_template("all_melons.html",
                           melon_list = melons)

#CF show_melon, MF get_melon_by_id, TP melon_details.html
@app.route("/melon/<int:id>")
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)

#CF shopping_cart, MF (none), TP cart.html
@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."""
    # 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)

#CF add_to_cart, MF (none), TP (none)
@app.route("/add_to_cart/<string: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 "cart" not in session:
        session["cart"] = {id:1}
        flash("Sucessfully added to NEW cart!")
    else:                                   
    # if we do have a cart    
        if id in session["cart"]:
            session["cart"][id] = session["cart"][id] + 1   
            # will equal the quantity
        else:
            session["cart"][id] = 1

        flash("Successfully added to cart!")

    return redirect("/cart")


#CF show_login, MF (none), TP login.html
@app.route("/login", methods=["GET"])
def show_login():
    return render_template("login.html")

#CF process_login, MF (none), TP (none)
@app.route("/login", methods=["POST"])
def process_login():
    """TODO: Receive the user's login credentials located in the 'request.form'
    dictionary, look up the user, and store them in the session."""
    email = request.form.get("email")
    password = request.form.get("password")
    message = model.get_customer_by_email(email, password)
    return render_template("userpage.html", message = message)

#CF checkout, MF (none), TP (none)
@app.route("/checkout")
def checkout():
    """TODO: Implement a payment system. For now, just return them to the main
    melon listing page."""
    flash("Sorry! Checkout will be implemented in a future version of ubermelon.")
    return redirect("/melons")

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(debug=True, port=port)
