From b0fe9fa71d73f7fae8e5017699ad7acf921b7e99 Mon Sep 17 00:00:00 2001
From: Michael Drury <mpdrury15@gmail.com>
Date: Wed, 3 May 2023 10:45:15 +0100
Subject: [PATCH] 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

---
 instance/app.db | Bin 20480 -> 20480 bytes
 run.py          |   2 +-
 shop/routes.py  |  58 +++++++++++++++++++++++++++++++-----------------
 3 files changed, 39 insertions(+), 21 deletions(-)

diff --git a/instance/app.db b/instance/app.db
index e573ceba742506f1c743c4562ba4af9dbcbb2039..658026cda18b1026ae13d2ff7edef976e8e77d5c 100644
GIT binary patch
delta 74
zcmZozz}T>Wae_2s{6raN#`uj13;B5&7#NuO7BTR5@r&{;+AOFb!Y2r1Lm@+jbYmp1
Y;ABI&{K<>tUa*ufvNLS%lP@v=0O<r2IRF3v

delta 83
zcmZozz}T>Wae_3X-$WT_M!$^-3;B8ZyBL`HG#L0a_(k~^Z5C7z;S=NU0t(huFtRg9
mH%5x`=4K{mB&Oy}UN0v)*<UVr@&dV6EDQ_`Wt;otiwppgBo^BM

diff --git a/run.py b/run.py
index eaa0389..9d9183a 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 250f4c1..a973297 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'])
-- 
GitLab