From 942bbd1b05a98f957290cf099e9aa8574d5bf7e8 Mon Sep 17 00:00:00 2001 From: tcareer34 Date: Fri, 22 Nov 2024 13:16:03 -0800 Subject: [PATCH 1/6] Update main.py --- main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 67fb91b..c8eadfd 100644 --- a/main.py +++ b/main.py @@ -36,13 +36,13 @@ def root(): # Show last product added cur.execute('SELECT productId, name, price, description, image, stock FROM products ORDER BY productId DESC LIMIT 1 ') # Show all items - #cur.execute('SELECT productId, name, price, description, image, stock FROM products LIMIT 1') + cur.execute('SELECT productId, name, price, description, image, stock FROM products LIMIT 1') item_data = cur.fetchall() # Show an error instead of the categories category_data = [(-1,"Error")] # Show all categories - #cur.execute('SELECT categoryId, name FROM categories') - #category_data = cur.fetchall() + cur.execute('SELECT categoryId, name FROM categories') + category_data = cur.fetchall() item_data = parse(item_data) return render_template('home.html', itemData=item_data, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items, categoryData=category_data) From 690fe6bdfb45ff9f7b23197166c3d037db088b0f Mon Sep 17 00:00:00 2001 From: tcareer34 Date: Fri, 22 Nov 2024 13:25:18 -0800 Subject: [PATCH 2/6] Fix category in code Changes made to line 37: cur.execute('SELECT productId, name, price, description, image, stock FROM products ORDER BY productId DESC LIMIT 1 ') cur.execute( 'SELECT productId, name, price, description, image, stock FROM products ORDER BY productId DESC LIMIT 1 ') Fix line 42: category_data = [(-1,"Error")] category_data = [(-1, "Error")] fix line 47: return render_template('home.html', itemData=item_data, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items, categoryData=category_data) return render_template('home.html', itemData=item_data, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items, categoryData=category_data) --- main.py | 104 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index c8eadfd..b42aca9 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,7 @@ ALLOWED_EXTENSIONS = set(['jpeg', 'jpg', 'png', 'gif']) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER + def get_login_details(): with sqlite3.connect('database.db') as conn: cur = conn.cursor() @@ -28,23 +29,27 @@ def get_login_details(): conn.close() return (logged_in, first_name, no_of_items) + @app.route("/") def root(): logged_in, first_name, no_of_items = get_login_details() with sqlite3.connect('database.db') as conn: cur = conn.cursor() # Show last product added - cur.execute('SELECT productId, name, price, description, image, stock FROM products ORDER BY productId DESC LIMIT 1 ') + cur.execute( + 'SELECT productId, name, price, description, image, stock FROM products ORDER BY productId DESC LIMIT 1 ') # Show all items cur.execute('SELECT productId, name, price, description, image, stock FROM products LIMIT 1') item_data = cur.fetchall() # Show an error instead of the categories - category_data = [(-1,"Error")] + category_data = [(-1, "Error")] # Show all categories cur.execute('SELECT categoryId, name FROM categories') category_data = cur.fetchall() item_data = parse(item_data) - return render_template('home.html', itemData=item_data, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items, categoryData=category_data) + return render_template('home.html', itemData=item_data, loggedIn=logged_in, firstName=first_name, + noOfItems=no_of_items, categoryData=category_data) + @app.route("/add") def admin(): @@ -55,6 +60,7 @@ def admin(): conn.close() return render_template('add.html', categories=categories) + @app.route("/addItem", methods=["GET", "POST"]) def addItem(): if request.method == "POST": @@ -64,7 +70,7 @@ def addItem(): stock = int(request.form['stock']) categoryId = int(request.form['category']) - #Upload image + # Upload image image = request.files['image'] if image and allowed_file(image.filename): filename = secure_filename(image.filename) @@ -73,16 +79,19 @@ def addItem(): with sqlite3.connect('database.db') as conn: try: cur = conn.cursor() - cur.execute('''INSERT INTO products (name, price, description, image, stock, categoryId) VALUES (?, ?, ?, ?, ?, ?)''', (name, price, description, imagename, stock, categoryId)) + cur.execute( + '''INSERT INTO products (name, price, description, image, stock, categoryId) VALUES (?, ?, ?, ?, ?, ?)''', + (name, price, description, imagename, stock, categoryId)) conn.commit() - msg="Added successfully" + msg = "Added successfully" except: - msg="Error occured" + msg = "Error occured" conn.rollback() conn.close() print(msg) return redirect(url_for('root')) + @app.route("/displayCategory") def displayCategory(): logged_in, first_name, no_of_items = get_login_details() @@ -106,6 +115,7 @@ def profile_home(): logged_in, first_name, no_of_items = get_login_details() return render_template("profileHome.html", loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items) + @app.route("/account/profile/edit") def edit_profile(): if 'email' not in session: @@ -113,10 +123,14 @@ def edit_profile(): logged_in, first_name, no_of_items = get_login_details() with sqlite3.connect('database.db') as conn: cur = conn.cursor() - cur.execute("SELECT userId, email, first_name, lastName, address1, address2, zipcode, city, state, country, phone FROM users WHERE email = '" + session['email'] + "'") + cur.execute( + "SELECT userId, email, first_name, lastName, address1, address2, zipcode, city, state, country, phone FROM users WHERE email = '" + + session['email'] + "'") profile_data = cur.fetchone() conn.close() - return render_template("editProfile.html", profileData=profile_data, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items) + return render_template("editProfile.html", profileData=profile_data, loggedIn=logged_in, firstName=first_name, + noOfItems=no_of_items) + @app.route("/account/profile/changePassword", methods=["GET", "POST"]) def change_password(): @@ -135,7 +149,7 @@ def change_password(): try: cur.execute("UPDATE users SET password = ? WHERE userId = ?", (new_password, user_id)) conn.commit() - msg="Changed successfully" + msg = "Changed successfully" except: conn.rollback() msg = "Failed" @@ -147,6 +161,7 @@ def change_password(): else: return render_template("changePassword.html") + @app.route("/updateProfile", methods=["GET", "POST"]) def update_profile(): if request.method == 'POST': @@ -161,27 +176,32 @@ def update_profile(): country = request.form['country'] phone = request.form['phone'] with sqlite3.connect('database.db') as con: - try: - cur = con.cursor() - cur.execute('UPDATE users SET firstName = ?, lastName = ?, address1 = ?, address2 = ?, zipcode = ?, city = ?, state = ?, country = ?, phone = ? WHERE email = ?', (first_name, last_name, address1, address2, zipcode, city, state, country, phone, email)) + try: + cur = con.cursor() + cur.execute( + 'UPDATE users SET firstName = ?, lastName = ?, address1 = ?, address2 = ?, zipcode = ?, city = ?, state = ?, country = ?, phone = ? WHERE email = ?', + (first_name, last_name, address1, address2, zipcode, city, state, country, phone, email)) - con.commit() - msg = "Saved Successfully" - except: - con.rollback() - msg = "Error occured" + con.commit() + msg = "Saved Successfully" + except: + con.rollback() + msg = "Error occured" con.close() return redirect(url_for('edit_profile')) + @app.route("/loginForm") def login_form(): # Uncomment to enable logging in and registration - #if 'email' in session: - return redirect(url_for('root')) - #else: - # return render_template('login.html', error='') + # if 'email' in session: + return redirect(url_for('root')) -@app.route("/login", methods = ['POST', 'GET']) + +# else: +# return render_template('login.html', error='') + +@app.route("/login", methods=['POST', 'GET']) def login(): if request.method == 'POST': email = request.form['email'] @@ -193,6 +213,7 @@ def login(): error = 'Invalid UserId / Password' return render_template('login.html', error=error) + @app.route("/productDescription") def product_description(): logged_in, first_name, no_of_items = get_login_details() @@ -206,6 +227,7 @@ def product_description(): return render_template("productDescription.html", data=productData, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items) + @app.route("/addToCart") def add_to_cart(): if 'email' not in session: @@ -226,6 +248,7 @@ def add_to_cart(): conn.close() return redirect(url_for('root')) + @app.route("/cart") def cart(): if 'email' not in session: @@ -236,12 +259,16 @@ def cart(): cur = conn.cursor() cur.execute("SELECT userId FROM users WHERE email = '" + email + "'") user_id = cur.fetchone()[0] - cur.execute("SELECT products.productId, products.name, products.price, products.image FROM products, kart WHERE products.productId = kart.productId AND kart.userId = " + str(user_id)) + cur.execute( + "SELECT products.productId, products.name, products.price, products.image FROM products, kart WHERE products.productId = kart.productId AND kart.userId = " + str( + user_id)) products = cur.fetchall() total_price = 0 for row in products: total_price += row[2] - return render_template("cart.html", products = products, totalPrice=total_price, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items) + return render_template("cart.html", products=products, totalPrice=total_price, loggedIn=logged_in, + firstName=first_name, noOfItems=no_of_items) + @app.route("/removeFromCart") def remove_from_cart(): @@ -263,11 +290,13 @@ def remove_from_cart(): conn.close() return redirect(url_for('root')) + @app.route("/logout") def logout(): session.pop('email', None) return redirect(url_for('root')) + def is_valid(email, password): con = sqlite3.connect('database.db') cur = con.cursor() @@ -279,7 +308,7 @@ def is_valid(email, password): return False -@app.route("/checkout", methods=['GET','POST']) +@app.route("/checkout", methods=['GET', 'POST']) def payment(): if 'email' not in session: return redirect(url_for('login_form')) @@ -290,7 +319,9 @@ def payment(): cur = conn.cursor() cur.execute("SELECT userId FROM users WHERE email = '" + email + "'") user_id = cur.fetchone()[0] - cur.execute("SELECT products.productId, products.name, products.price, products.image FROM products, kart WHERE products.productId = kart.productId AND kart.userId = " + str(user_id)) + cur.execute( + "SELECT products.productId, products.name, products.price, products.image FROM products, kart WHERE products.productId = kart.productId AND kart.userId = " + str( + user_id)) products = cur.fetchall() total_price = 0 for row in products: @@ -300,14 +331,14 @@ def payment(): cur.execute("DELETE FROM kart WHERE userId = " + str(user_id)) conn.commit() - + return render_template("checkout.html", products=products, totalPrice=total_price, loggedIn=logged_in, + firstName=first_name, noOfItems=no_of_items) - return render_template("checkout.html", products = products, totalPrice=total_price, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items) -@app.route("/register", methods = ['GET', 'POST']) +@app.route("/register", methods=['GET', 'POST']) def register(): if request.method == 'POST': - #Parse form data + # Parse form data password = request.form['password'] email = request.form['email'] first_name = request.form['firstName'] @@ -323,7 +354,10 @@ def register(): with sqlite3.connect('database.db') as con: try: cur = con.cursor() - cur.execute('INSERT INTO users (password, email, firstName, lastName, address1, address2, zipcode, city, state, country, phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (hashlib.md5(password.encode()).hexdigest(), email, first_name, last_name, address1, address2, zipcode, city, state, country, phone)) + cur.execute( + 'INSERT INTO users (password, email, firstName, lastName, address1, address2, zipcode, city, state, country, phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + (hashlib.md5(password.encode()).hexdigest(), email, first_name, last_name, address1, address2, + zipcode, city, state, country, phone)) con.commit() @@ -334,13 +368,16 @@ def register(): con.close() return render_template("login.html", error=msg) + @app.route("/registrationForm") def registration_form(): return render_template("register.html") + def allowed_file(filename): return '.' in filename and \ - filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS + filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS + def parse(data): ans = [] @@ -355,5 +392,6 @@ def parse(data): ans.append(curr) return ans + if __name__ == '__main__': app.run(debug=True) From af6ea314ccceba8338c36517f03d65cd197864ce Mon Sep 17 00:00:00 2001 From: tcareer34 Date: Mon, 25 Nov 2024 11:39:30 -0800 Subject: [PATCH 3/6] Update main.py Fixed line 25 changing user_id and first_name to userId and firstName Fixed line 27 changing user_id to userId --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index b42aca9..ec11ad6 100644 --- a/main.py +++ b/main.py @@ -22,9 +22,9 @@ def get_login_details(): no_of_items = 0 else: logged_in = True - cur.execute("SELECT user_id, first_name FROM users WHERE email = '" + session['email'] + "'") + cur.execute("SELECT userId, firstName FROM users WHERE email = '" + session['email'] + "'") # Fixed user_id to userID and first_name to firstName user_id, first_name = cur.fetchone() - cur.execute("SELECT count(productId) FROM kart WHERE user_id = " + str(user_id)) + cur.execute("SELECT count(productId) FROM kart WHERE userId = " + str(user_id)) # Fixed user_id to userId no_of_items = cur.fetchone()[0] conn.close() return (logged_in, first_name, no_of_items) From 65d21c7b8cc614c7c604849f02254370f5e48fd6 Mon Sep 17 00:00:00 2001 From: tcareer34 Date: Mon, 25 Nov 2024 13:56:58 -0800 Subject: [PATCH 4/6] Update main.py Made changes to final code --- main.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index ec11ad6..7119be5 100644 --- a/main.py +++ b/main.py @@ -22,9 +22,11 @@ def get_login_details(): no_of_items = 0 else: logged_in = True - cur.execute("SELECT userId, firstName FROM users WHERE email = '" + session['email'] + "'") # Fixed user_id to userID and first_name to firstName + cur.execute("SELECT userId, firstName FROM users WHERE email = '" + session[ + 'email'] + "'") # Fixed user_id & first_name to userId & firstName, + user_id, first_name = cur.fetchone() - cur.execute("SELECT count(productId) FROM kart WHERE userId = " + str(user_id)) # Fixed user_id to userId + cur.execute("SELECT count(productId) FROM kart WHERE userId = " + str(user_id)) # Fixed user_id to userId, no_of_items = cur.fetchone()[0] conn.close() return (logged_in, first_name, no_of_items) @@ -37,15 +39,15 @@ def root(): cur = conn.cursor() # Show last product added cur.execute( - 'SELECT productId, name, price, description, image, stock FROM products ORDER BY productId DESC LIMIT 1 ') + 'SELECT productId, name, price, description, image, stock FROM products ORDER BY productId DESC LIMIT 1') # Show all items - cur.execute('SELECT productId, name, price, description, image, stock FROM products LIMIT 1') + cur.execute('SELECT productId, name, price, description, image, stock FROM products ') item_data = cur.fetchall() # Show an error instead of the categories category_data = [(-1, "Error")] # Show all categories - cur.execute('SELECT categoryId, name FROM categories') - category_data = cur.fetchall() + cur.execute('SELECT categoryId, name FROM categories') # hashtag in front of line + category_data = cur.fetchall() # hashtag in front of line item_data = parse(item_data) return render_template('home.html', itemData=item_data, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items, categoryData=category_data) @@ -194,12 +196,11 @@ def update_profile(): @app.route("/loginForm") def login_form(): # Uncomment to enable logging in and registration - # if 'email' in session: - return redirect(url_for('root')) - + if 'email' in session: + return redirect(url_for('root')) + else: # Uncommented by Yeab 1/18/2023 + return render_template('login.html', error='') -# else: -# return render_template('login.html', error='') @app.route("/login", methods=['POST', 'GET']) def login(): @@ -326,7 +327,7 @@ def payment(): total_price = 0 for row in products: total_price += row[2] - print(row) + # print(row) cur.execute("INSERT INTO Orders (userId, productId) VALUES (?, ?)", (user_id, row[0])) cur.execute("DELETE FROM kart WHERE userId = " + str(user_id)) conn.commit() From f144a04f9886357a66f69198c4865f1e18842603 Mon Sep 17 00:00:00 2001 From: tcareer34 Date: Tue, 26 Nov 2024 18:39:10 -0800 Subject: [PATCH 5/6] Update main.py update comments --- main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 7119be5..43f7e01 100644 --- a/main.py +++ b/main.py @@ -23,10 +23,10 @@ def get_login_details(): else: logged_in = True cur.execute("SELECT userId, firstName FROM users WHERE email = '" + session[ - 'email'] + "'") # Fixed user_id & first_name to userId & firstName, + 'email'] + "'") # Elmi A. fixed user_id & first_name to userId & firstName user_id, first_name = cur.fetchone() - cur.execute("SELECT count(productId) FROM kart WHERE userId = " + str(user_id)) # Fixed user_id to userId, + cur.execute("SELECT count(productId) FROM kart WHERE userId = " + str(user_id)) # Elmi A. fixed user_id to userId no_of_items = cur.fetchone()[0] conn.close() return (logged_in, first_name, no_of_items) @@ -46,8 +46,8 @@ def root(): # Show an error instead of the categories category_data = [(-1, "Error")] # Show all categories - cur.execute('SELECT categoryId, name FROM categories') # hashtag in front of line - category_data = cur.fetchall() # hashtag in front of line + cur.execute('SELECT categoryId, name FROM categories') # Tim N. removed hashtag in front of line + category_data = cur.fetchall() # Tim N. removed hashtag in front of line item_data = parse(item_data) return render_template('home.html', itemData=item_data, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items, categoryData=category_data) @@ -198,7 +198,7 @@ def login_form(): # Uncomment to enable logging in and registration if 'email' in session: return redirect(url_for('root')) - else: # Uncommented by Yeab 1/18/2023 + else: # Elmi A fix login registration return render_template('login.html', error='') From 2fb674004b7dad92c49a0b1fccf311ef7e762bdb Mon Sep 17 00:00:00 2001 From: tcareer34 Date: Tue, 26 Nov 2024 19:01:08 -0800 Subject: [PATCH 6/6] Update main.py Adding more comments in the code on the line our team fixed. --- main.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 43f7e01..0760b60 100644 --- a/main.py +++ b/main.py @@ -23,10 +23,10 @@ def get_login_details(): else: logged_in = True cur.execute("SELECT userId, firstName FROM users WHERE email = '" + session[ - 'email'] + "'") # Elmi A. fixed user_id & first_name to userId & firstName + 'email'] + "'") # Elmi A. changed user_id & first_name to userId & firstName user_id, first_name = cur.fetchone() - cur.execute("SELECT count(productId) FROM kart WHERE userId = " + str(user_id)) # Elmi A. fixed user_id to userId + cur.execute("SELECT count(productId) FROM kart WHERE userId = " + str(user_id)) # Elmi A. changed user_id to userId no_of_items = cur.fetchone()[0] conn.close() return (logged_in, first_name, no_of_items) @@ -46,8 +46,8 @@ def root(): # Show an error instead of the categories category_data = [(-1, "Error")] # Show all categories - cur.execute('SELECT categoryId, name FROM categories') # Tim N. removed hashtag in front of line - category_data = cur.fetchall() # Tim N. removed hashtag in front of line + cur.execute('SELECT categoryId, name FROM categories') # Tim N. removed hashtag in front of this line for category fix + category_data = cur.fetchall() # Tim N. removed hashtag in front of this line for category fix item_data = parse(item_data) return render_template('home.html', itemData=item_data, loggedIn=logged_in, firstName=first_name, noOfItems=no_of_items, categoryData=category_data) @@ -195,10 +195,10 @@ def update_profile(): @app.route("/loginForm") def login_form(): - # Uncomment to enable logging in and registration + # Elmi A. to enable logging in and registration if 'email' in session: return redirect(url_for('root')) - else: # Elmi A fix login registration + else: # Elmi A. fix login registration return render_template('login.html', error='')