Skip to content
Snippets Groups Projects
Commit b0fe9fa7 authored by Michael Drury's avatar Michael Drury
Browse files

made a make_lists function that makes the lists for cart and wishlist as they...

made a make_lists function that makes the lists for cart and wishlist as they are similar and so to get rid of repeated code. also added some comments throught the code
parent 70f1d0e9
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -2,7 +2,7 @@ from shop import app, db
from shop.databases import User
from shop.databases import product_table
# initialtes some users and products in the tables as well as runs the programs
# initialtes some users and products in the tables as well as runs the program
if __name__ == '__main__':
with app.app_context():
db.create_all()
......
......@@ -98,19 +98,39 @@ def shopping():
# renders the shopping page
return render_template('shopping.html', product_table = products_pass, order_by_form = order_by_form, type_form = type_form)
# used to make lists for cart and wishlist
# creates a list for the current user and takes the string of ids which consists of the item
# ids of the items and splits up by spaces. It returns the item_list it adds the rows for the items that are in the
# current users cart as well as the id_list which is a list of ids
def make_lists(item_id_string):
id_list = item_id_string.split()
item_list = []
for item_id in id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
item_list.append(item)
return item_list, id_list
@app.route('/cart', methods=['GET', 'POST'])
@login_required
def cart():
cart_string = current_user.cart
cart_id_list = cart_string.split()
cart_list = []
for item_id in cart_id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
cart_list.append(item)
# creates a cart list and a list of ids in cart using make_list
make_lists_out = make_lists(current_user.cart)
cart_list = make_lists_out[0]
cart_id_list = make_lists_out[1]
# performs the functionality of the remove from cart form. If an item id is submitted it removes one case of that
# item from the current users cart list. If the remove all button is ticked it makes the current cart empty by
# making the users cart string ''.
# At the end it refreshes the page so that the removed item is not on the page.
remove_form = removeFormCart()
if remove_form.validate_on_submit() and remove_form.validate():
remove_item_id = remove_form.item_id_remove.data
clear_all = remove_form.clear_all.data
......@@ -128,6 +148,9 @@ def cart():
db.session.commit()
return redirect(url_for('cart'))
# gets the total price of the cart and then decides on if the user is allowed free shipping and if not adds
# shipping cost to the final price
total_price = get_total_cart_price()
free_shipping = False
......@@ -145,17 +168,12 @@ def cart():
@app.route('/wishlist', methods=['GET', 'POST'])
@login_required
def wishlist():
wishlist_string = current_user.wishlist
wishlist_id_list = wishlist_string.split()
wishlist_list = []
for item_id in wishlist_id_list:
items = db.session.query(product_table).all()
for item in items:
if item.product_id == int(item_id):
wishlist_list.append(item)
total_price = 0
for item in wishlist_list:
total_price += item.product_price
make_lists_out = make_lists(current_user.wishlist)
wishlist_list = make_lists_out[0]
wishlist_id_list = make_lists_out[1]
remove_form = removeFormWishlist()
if remove_form.validate_on_submit():
remove_item_id = remove_form.item_id_remove.data
......@@ -173,7 +191,7 @@ def wishlist():
c.wishlist = ''
db.session.commit()
return redirect(url_for('wishlist'))
return render_template('wishlist.html', wishlist_list = wishlist_list, wishlist_size = len(wishlist_list), total_price = total_price, remove_form = remove_form)
return render_template('wishlist.html', wishlist_list = wishlist_list, wishlist_size = len(wishlist_list), remove_form = remove_form)
@app.route('/item', methods=['GET', 'POST'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment