Example #1
0
def show_shopping_cart():
    """Display content of shopping cart."""

    # TODO: Display the contents of the shopping cart.

    # The logic here will be something like:
    #
    # - get the cart dictionary from the session
    # - create a list to hold melon objects and a variable to hold the total
    #   cost of the order
    # - loop over the cart dictionary, and for each melon id:
    #    - get the corresponding Melon object
    #    - compute the total cost for that type of melon
    #    - add this to the order total
    #    - add quantity and total cost as attributes on the Melon object
    #    - add the Melon object to the list created above
    # - pass the total order cost and the list of Melon objects to the template
    #
    # Make sure your function can also handle the case wherein no cart has
    # been added to the session

    melon_list = melons.get_all()
    running_total = 0

    if 'cart' in session:
        for melon in melon_list:
            # print('melon: ', melon)
            if melon.melon_id in session['cart']:
                running_total += (melon.price * session['cart'][melon.melon_id])

    return render_template("cart.html",
                        melon_list=melon_list,
                        running_total=running_total)
Example #2
0
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: 'Melon successfully added to
    cart'."""
    # session['cart'] is a dict
    # session['cart'][melon_id]
    # if 'cart' not in session:
    # session['cart'] = {}

    melon_list = melons.get_all()

    cart = session.get('cart', {})
    cart[melon_id] = cart.get(melon_id, 0) + 1
    session['cart'] = cart

    total = 0
    for item in cart:
        total += (melons.melon_types[item].price * cart[item])

    melon_name = melons.melon_types[item].common_name
    flash(f"You've successfully added one {melon_name} to your cart.")
    session['total'] = total

    # The logic here should be something like:
    #
    # - check if a "cart" exists in the session, and create one (an empty
    #   dictionary keyed to the string "cart") if not
    # - check if the desired melon id is the cart, and if not, put it in
    # - increment the count for that melon id by 1
    # - flash a success message
    # - redirect the user to the cart page

    return redirect("/cart")
Example #3
0
def list_melons():
    """Return page showing all the melons ubermelon has to offer"""

    melon_list = melons.get_all()
    # `print("*"*20)
    # print(f"melon list = melons.get_all() : {melon_list}")`
    return render_template("all_melons.html", melon_list=melon_list)
Example #4
0
def list_melons():
    """Return page showing all the melons ubermelon has to offer"""

    session['has_shopped'] = True
    print(session)
    melon_list = melons.get_all()
    return render_template("all_melons.html", melon_list=melon_list)
Example #5
0
def show_shopping_cart():
    """Display content of shopping cart."""

    # TODO: Display the contents of the shopping cart.

    
    # list of melons as melon objects
    melon_list = melons.get_all()
    # cart_dict with keys as melon id strings
    cart_dict = session['cart']
    # pull total from session
    # total_cost = 0
    # total_cost += melon_list[melon_id].price + melon_dict[melon_id]


    # The logic here will be something like:
    #
    # - get the cart dictionary from the session
    # - create a list to hold melon objects and a variable to hold the total
    #   cost of the order
    # - loop over the cart dictionary, and for each melon id:
    #    - get the corresponding Melon object
    #    - compute the total cost for that type of melon
    #    - add this to the order total
    #    - add quantity and total cost as attributes on the Melon object
    #    - add the Melon object to the list created above
    # - pass the total order cost and the list of Melon objects to the template
    #
    # Make sure your function can also handle the case wherein no cart has
    # been added to the session

    return render_template("cart.html", cart_dict=cart_dict)
Example #6
0
def list_melons():
    """Return page showing all the melons ubermelon has to offer"""

    melon_list = melons.get_all()
    
    return render_template("all_melons.html",
                           melon_list=melon_list)
Example #7
0
def list_melons():
    """Return page showing all the melons ubermelon has to offer"""

    melon_list = melons.get_all()

    # melon_list is a list of melontype objects
    return render_template("all_melons.html",
                           melon_list=melon_list)
Example #8
0
def list_melons():
    """Return page showing all the melons ubermelon has to offer"""

    # All of the melons displayed on the /melons page. 
    melon_list = melons.get_all()

    # Passes in the melon_list to the
    return render_template("all_melons.html",
                           melon_list=melon_list)
Example #9
0
def show_shopping_cart():
    """Display content of shopping cart."""

    # TODO: Display the contents of the shopping cart.

    # The logic here will be something like:
    #
    # - get the cart dictionary from the session

    # - create a list to hold melon objects and a variable to hold the total
    #   cost of the order
    melons_in_cart = session['cart']
    melon_list = melons.get_all()  #list of all melon instances

    # list of melon objects in cart:
    melon_objects_in_cart = []

    # total cost of order
    total_price = 0
    for melon in melons_in_cart.keys():
        melon_info = melons.get_by_id(melon)
        # melon_info.name = melon_info.common_name
        melon_info.qty = session['cart'][melon]
        melon_info.total_cost = melon_info.qty * melon_info.price
        melon_objects_in_cart.append(melon_info)
        total_price += melon_info.price * session['cart'][melon]

    print(melon_objects_in_cart)

    # - loop over the cart dictionary, and for each melon id:
    #    - get the corresponding Melon object
    #    - compute the total cost for that type of melon
    #    - add this to the order total
    #    - add quantity and total cost as attributes on the Melon object
    #    - add the Melon object to the list created above
    # - pass the total order cost and the list of Melon objects to the template
    #
    # Make sure your function can also handle the case wherein no cart has
    # been added to the session

    return render_template("cart.html",
                           melon_objects_in_cart=melon_objects_in_cart,
                           total_price=total_price)
def show_shopping_cart():
    """Display content of shopping cart."""

    # TODO: Display the contents of the shopping cart.

    # The logic here will be something like:
    #
    # - get the cart dictionary from the session
    melon_list = melons.get_all()
    shop_cart = session["cart"]
    melon_obj = []
    total_qty = 0
    print(melon_list)
    print(shop_cart)

    for key in shop_cart:
        melon_obj.append(key)
        price = melon_list[key][2]
        print(price)
        total_qty += 1

    # total_cost = total_qty * melon_list[2]  #need to access the values not the key

    # print(total_cost)

    # - create a list to hold melon objects and a variable to hold the total
    #   cost of the order

    # - loop over the cart dictionary, and for each melon id:
    #    - get the corresponding Melon object
    #    - compute the total cost for that type of melon
    #    - add this to the order total
    #    - add quantity and total cost as attributes on the Melon object
    #    - add the Melon object to the list created above
    # - pass the total order cost and the list of Melon objects to the template
    #
    # Make sure your function can also handle the case wherein no cart has
    # been added to the session

    return render_template("cart.html")
Example #11
0
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: 'Melon successfully added to
    cart'."""

    # TODO: Finish shopping cart functionality

    # The logic here should be something like:
    #
    # - check if a "cart" exists in the session, and create one (an empty
    #   dictionary keyed to the string "cart") if not
    # - check if the desired melon id is the cart, and if not, put it in
    # - increment the count for that melon id by 1
    # - flash a success message
    # - redirect the user to the cart page

    # check session's cart, grouping/nesting all cart info together as a dict
    # if 'cart' is not already in session, then start a 'cart' session
    if 'cart' not in session:
        session['cart'] = {}

    
    # cart is a dictionary
    cart = session['cart']

    # dictionary session (key cart) (cart's dictionary with key melon_id)
    session['cart'][melon_id] = session['cart'].get(melon_id, 0) + 1
    melon_list = melons.get_all()
    print(cart)
    

    # do a flash message

    return redirect("/cart")
Example #12
0
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


    #Already have the melon information from our get_by_id function in melons.py 
    #and would only need to get from it the melon name and price info. 
    #This way we can just count quantity and get the total 
    #.get_by_id function is only taking 1 id and my cart_ids is a list of ids

    # melons = melons.get_by_id(cart_ids)
    # # print melons

    # cart_ids = session['cart'] #bind values to the variable cart_ids   

    melon_dict = melons.get_all()

    for obj in melon_dict:
        print obj #(checkpoint) This will print every opject within the melon_dict


    melon_objects = {} #create an empty dictionary

    id = {price: melons.melon_types.price, 
        melon_name: melons.common_name, 
        quantity: int,
        total: int}

    for key, value in melon_objects.items():
        for key, value in id.items():
            if value not in id:




    # for id in cart_ids:
    #     if id not in cart_id_dict:
    #         cart_id_dict['id'] = {"quantity":1,
    #     else:
    #         cart_id_dict['id'].append(quantity)

    # if 'price' not in cart_id_dict:
    # cart_id_dict['cart_ids']['price'] = []
    # else:
    # cart_id_dict['cart_ids']['price'].append()
    # # elif 'melon name' not in cart_id_dict:
    #     cart_id_dict['cart_ids']['melon name'] = [name]
    # else:
    #     cart_id_dict['cart_ids']['melon name'].append[name]
    # elif 'quantity' not in cart_id_dict:
    #     cart_id_dict['cart_ids']['quantity'] = [amount]
    # else:
    #     cart_id_dict['cart  ds']['total'] = [value]
    # else:
    #     cart_id_dict['cart_ids']['total'].append[value]



    return render_template("cart.html")