diff --git a/instance/app.db b/instance/app.db index e573ceba742506f1c743c4562ba4af9dbcbb2039..658026cda18b1026ae13d2ff7edef976e8e77d5c 100644 Binary files a/instance/app.db and b/instance/app.db differ diff --git a/run.py b/run.py index eaa0389ae5c6ccc25d7383bb57c041d521d827b9..9d9183a319b6146e68e15266a78d037631e6e491 100644 --- a/run.py +++ b/run.py @@ -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() diff --git a/shop/routes.py b/shop/routes.py index 250f4c19e2cbef66dc5a74bda5421dbe585bb685..a97329796bd18b993bd4a52955688424ce1bc2e5 100644 --- a/shop/routes.py +++ b/shop/routes.py @@ -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'])