From 9c27ae26867f381a8ec94db18f885ae42a7868aa Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sun, 9 Feb 2020 02:26:47 +1100 Subject: [PATCH 01/14] Added new table and updated schema --- shop.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shop.py b/shop.py index e2a6402..73c4324 100644 --- a/shop.py +++ b/shop.py @@ -30,7 +30,8 @@ def doesTableExist(): if len(tables) == 0: mycursor.execute('''CREATE TABLE SHOPLIST (id INT AUTO_INCREMENT PRIMARY KEY, item VARCHAR(255), gotten BOOLEAN, user_id INT, FOREIGN KEY (`user_id`) REFERENCES `USERS`(`id`) ON DELETE CASCADE)''') - mycursor.execute('''CREATE TABLE USERS (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255), admin BOOLEAN)''') + mycursor.execute('''CREATE TABLE USERS (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255), admin BOOLEAN, list_id INT, FOREIGN KEY list_id REFERENCES `LISTS`(`id`))''') + mycursor.execute('''CREATE TABLE LISTS (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))''') mydb.close() def runQuery(query, data=None): From a7ca76d1e691b65299eaeec671826a9070c4d863 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sun, 9 Feb 2020 02:57:23 +1100 Subject: [PATCH 02/14] Updated schema, added USER_META --- shop.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/shop.py b/shop.py index 73c4324..a398377 100644 --- a/shop.py +++ b/shop.py @@ -28,10 +28,12 @@ def doesTableExist(): mycursor.execute('''SHOW TABLES''') tables = mycursor.fetchall() if len(tables) == 0: - mycursor.execute('''CREATE TABLE SHOPLIST (id INT AUTO_INCREMENT PRIMARY KEY, item VARCHAR(255), gotten BOOLEAN, user_id INT, FOREIGN KEY (`user_id`) REFERENCES `USERS`(`id`) - ON DELETE CASCADE)''') - mycursor.execute('''CREATE TABLE USERS (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255), admin BOOLEAN, list_id INT, FOREIGN KEY list_id REFERENCES `LISTS`(`id`))''') + mycursor.execute('''CREATE TABLE USERS (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255), admin BOOLEAN)''') mycursor.execute('''CREATE TABLE LISTS (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))''') + mycursor.execute('''CREATE TABLE SHOPLIST (id INT AUTO_INCREMENT PRIMARY KEY, item VARCHAR(255), gotten BOOLEAN, user_id INT, list_id INT, FOREIGN KEY (`list_id`) REFERENCES `LISTS`(`id`), FOREIGN KEY (`user_id`) REFERENCES `USERS`(`id`) + ON DELETE CASCADE)''') + mycursor.execute('''CREATE TABLE USER_META (user_id INT, list_id INT, FOREIGN KEY (`list_id`) REFERENCES `LISTS`(`id`), FOREIGN KEY (`user_id`) REFERENCES `USERS`(`id`) + ON DELETE CASCADE)''') mydb.close() def runQuery(query, data=None): From 32c0baedaf593514eae0607a023c62aa688aeda9 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sun, 9 Feb 2020 03:08:01 +1100 Subject: [PATCH 03/14] Updated query to use the USER_META table --- shop.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shop.py b/shop.py index a398377..385b8e5 100644 --- a/shop.py +++ b/shop.py @@ -52,8 +52,8 @@ def runQuery(query, data=None): return ret def readFromDB(): - # query = '''select * from SHOPLIST''' - query = '''select SHOPLIST.id, SHOPLIST.item, SHOPLIST.gotten, USERS.username from SHOPLIST inner join USERS on SHOPLIST.user_id = USERS.id''' + # By default load all shopping lists the user is a part of (Most users will only have one so this this fine) + query = '''select SHOPLIST.id, SHOPLIST.item, SHOPLIST.gotten, USERS.username, SHOPLIST.list_id from SHOPLIST inner join USERS on SHOPLIST.user_id = USERS.id inner join USER_META on SHOPLIST.list_id = USER_META.list_id''' return runQuery(query) def insertToDB(data): From 0bb5a5823be5229b3f1f1bec8595ecdbf577c2da Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sun, 9 Feb 2020 18:10:04 +1100 Subject: [PATCH 04/14] SQL now correctly filters based on user_meta --- shop.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/shop.py b/shop.py index 385b8e5..b38f22b 100644 --- a/shop.py +++ b/shop.py @@ -99,6 +99,15 @@ def update_pass(user_id, newpass): data = (newpass, user_id) runQuery(query, data) +def get_items(user_id, list_id=None): + if list_id != None: + query = "select SHOPLIST.id, SHOPLIST.item, SHOPLIST.gotten, USERS.username from SHOPLIST inner join USERS on SHOPLIST.user_id = USERS.id inner join USER_META on SHOPLIST.list_id = USER_META.list_id where USER_META.user_id = %s and SHOPLIST.list_id = %s" + data = (user_id, list_id) + return runQuery(query, data) + query = "select SHOPLIST.id, SHOPLIST.item, SHOPLIST.gotten, USERS.username from SHOPLIST inner join USERS on SHOPLIST.user_id = USERS.id inner join USER_META on SHOPLIST.list_id = USER_META.list_id where USER_META.user_id = %s" + data = (user_id,) + return runQuery(query, data) + app = Flask(__name__) app.config["DEBUG"] = True app.secret_key = b'*$#@U9423jr92jioJKL_)_;dasfj()12' @@ -109,8 +118,9 @@ def index(): data = {"title":"Login"} return render_template("auth.html", data=data) - query = readFromDB() - data = {"title": "Shopping List", "results": query, "username": session["username"]} + #Get initial data, contains all lists the user is apart of + res = get_items(session["id"]) + data = {"title": "Shopping List", "results": res, "username": session["username"]} for device in MOBILES: if device in request.user_agent.platform: return render_template('mobile.html', data=data) From 2811a784cc68d49c75ed48a6bce7872d54a52168 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sun, 9 Feb 2020 18:20:50 +1100 Subject: [PATCH 05/14] Added list_ids to the session, with getter func --- shop.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/shop.py b/shop.py index b38f22b..f07e735 100644 --- a/shop.py +++ b/shop.py @@ -57,24 +57,24 @@ def readFromDB(): return runQuery(query) def insertToDB(data): - query = f"INSERT INTO SHOPLIST (item, gotten, user_id) VALUES (%s, 0, %s)" + query = "INSERT INTO SHOPLIST (item, gotten, user_id) VALUES (%s, 0, %s)" data = (data['item'], data['name']) # print(query) runQuery(query, data) def deleteRow(rowID): - query = f"DELETE FROM SHOPLIST WHERE id = %s" + query = "DELETE FROM SHOPLIST WHERE id = %s" data = (rowID, ) runQuery(query, data) def getItem(rowID): - query = f"UPDATE SHOPLIST set gotten = 1 where id = %s" + query = "UPDATE SHOPLIST set gotten = 1 where id = %s" data = (rowID, ) runQuery(query, data) def unGetItem(rowID): - query = f"UPDATE SHOPLIST set gotten = 0 where id = %s" + query = "UPDATE SHOPLIST set gotten = 0 where id = %s" data = (rowID, ) runQuery(query, data) @@ -83,19 +83,19 @@ def get_users(username=None): #return all users query = "select username, admin, id from USERS" return runQuery(query) - query = f"select username, admin from USERS where username like %s" + query = "select username, admin from USERS where username like %s" data = (username, ) return runQuery(query, data) def add_user(userData): username = userData["username"] password = userData["password"] - query = f"insert into USERS (username, password, admin) values (%s, md5(%s), False)" + query = "insert into USERS (username, password, admin) values (%s, md5(%s), False)" data = (username, password) runQuery(query, data) def update_pass(user_id, newpass): - query = f"update USERS set password=md5(%s) where id=%s" + query = "update USERS set password=md5(%s) where id=%s" data = (newpass, user_id) runQuery(query, data) @@ -108,6 +108,13 @@ def get_items(user_id, list_id=None): data = (user_id,) return runQuery(query, data) +def get_list_ids(user_id): + query = "select list_id from USER_META where user_id = %s" + data = (user_id,) + ret = runQuery(query, data) + list_ids = [idx for idx, in ret] + return list_ids + app = Flask(__name__) app.config["DEBUG"] = True app.secret_key = b'*$#@U9423jr92jioJKL_)_;dasfj()12' @@ -120,7 +127,7 @@ def index(): #Get initial data, contains all lists the user is apart of res = get_items(session["id"]) - data = {"title": "Shopping List", "results": res, "username": session["username"]} + data = {"title": "Shopping List", "results": res, "username": session["username"], "list_ids": session["list_ids"]} for device in MOBILES: if device in request.user_agent.platform: return render_template('mobile.html', data=data) @@ -145,12 +152,13 @@ def handle_data(): if "loginform" in request.form: query = "select id, username, admin from USERS where username = %s and password = md5(%s)" data = (request.form["username"].lower(), request.form["password"]) - res = runQuery(query, data) if len(res) != 0: + list_ids = get_list_ids(res[0][0]) session["id"] = res[0][0] session["username"] = res[0][1] session["isAdmin"] = res[0][2] + session["list_ids"] = list_ids if "newuser" in request.form: #first check if the user exists From 01efceca450ba82c3dda886f3ec1a105b1f64a76 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Mon, 10 Feb 2020 18:09:32 +1100 Subject: [PATCH 06/14] Better filtering for lists on homepage --- shop.py | 5 ++--- templates/index.html | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/shop.py b/shop.py index f07e735..1b1bfa5 100644 --- a/shop.py +++ b/shop.py @@ -109,11 +109,10 @@ def get_items(user_id, list_id=None): return runQuery(query, data) def get_list_ids(user_id): - query = "select list_id from USER_META where user_id = %s" + query = "select USER_META.list_id, LISTS.name from USER_META inner join LISTS on LISTS.id = USER_META.list_id where USER_META.user_id = %s" data = (user_id,) ret = runQuery(query, data) - list_ids = [idx for idx, in ret] - return list_ids + return ret app = Flask(__name__) app.config["DEBUG"] = True diff --git a/templates/index.html b/templates/index.html index 168924b..df23087 100644 --- a/templates/index.html +++ b/templates/index.html @@ -25,6 +25,9 @@ vpn_key +
+ {{data["list_ids"]}} +
From 2d4a35814d1df47a12d6e20e5b5023ea292a2518 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Mon, 10 Feb 2020 18:27:55 +1100 Subject: [PATCH 07/14] Added Host to mysql config --- config | 1 + shop.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/config b/config index 2956e3e..5e866d3 100644 --- a/config +++ b/config @@ -1,4 +1,5 @@ [mysql] +Host = localhost Username = SQLUsernam Password = SQLPassword Database = DBNAME \ No newline at end of file diff --git a/shop.py b/shop.py index 1b1bfa5..9dd9d03 100644 --- a/shop.py +++ b/shop.py @@ -14,7 +14,7 @@ except Exception as E: def dbConnect(): mydb = mysql.connector.connect( - host="localhost", + host=config["mysql"]["Host"], user=config["mysql"]["Username"], passwd=config["mysql"]["Password"], database=config["mysql"]["Database"] From 7082dc86fef551aba7536c08ebcae13d534537d1 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Mon, 10 Feb 2020 19:11:28 +1100 Subject: [PATCH 08/14] Added list selection on index --- shop.py | 20 ++++++++++++++++---- static/css/main.css | 2 +- templates/index.html | 18 ++++++++++++++++-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/shop.py b/shop.py index 9dd9d03..98ba8c1 100644 --- a/shop.py +++ b/shop.py @@ -123,10 +123,17 @@ def index(): if session.get('id') is None: data = {"title":"Login"} return render_template("auth.html", data=data) - - #Get initial data, contains all lists the user is apart of - res = get_items(session["id"]) - data = {"title": "Shopping List", "results": res, "username": session["username"], "list_ids": session["list_ids"]} + try: + if request.args["list"]: + print(request.args["list"]) + res = get_items(session["id"], request.args["list"]) + else: + res = get_items(session["id"]) + data = {"title": "Shopping List", "results": res, "username": session["username"], "list_ids": session["list_ids"]} + except KeyError: + #Get initial data, contains all lists the user is apart of unless list is defined + res = get_items(session["id"]) + data = {"title": "Shopping List", "results": res, "username": session["username"], "list_ids": session["list_ids"]} for device in MOBILES: if device in request.user_agent.platform: return render_template('mobile.html', data=data) @@ -134,6 +141,7 @@ def index(): @app.route('/post', methods=['POST']) def handle_data(): + if "addValue" in request.form: for x in request.form: if request.form[x] == '': @@ -177,6 +185,10 @@ def handle_data(): if "logout" in request.form: session.clear() + + if "changeList" in request.form: + pprint(request.form["changeList"]) + return redirect(url_for('index')) @app.route("/admin") diff --git a/static/css/main.css b/static/css/main.css index f9bad10..33de747 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -1,5 +1,5 @@ * { - color: azure; + color: azure; } aside { width: 20%; diff --git a/templates/index.html b/templates/index.html index df23087..7f57c8f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,7 +1,21 @@ {% include "header.html" %} -

{{ data["title"] }}, {{data["username"].title()}}

+

{{ data["title"] }}, {{data["username"].title()}} + +

{% include "addForm.html" %} @@ -26,7 +40,7 @@
- {{data["list_ids"]}} +
From a3254343cc171e3fe3c610af766806f33110227d Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Mon, 17 Feb 2020 18:59:22 +1100 Subject: [PATCH 09/14] Made the session handle the active list --- __pycache__/shop.cpython-38.pyc | Bin 0 -> 7109 bytes shop.py | 36 ++++++++++++++++++++++---------- templates/index.html | 8 +++---- 3 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 __pycache__/shop.cpython-38.pyc diff --git a/__pycache__/shop.cpython-38.pyc b/__pycache__/shop.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..af39d7974f36a971e3a6df6552ee041e698c90fe GIT binary patch literal 7109 zcmds6Npl;=6`mCsfZ!sES}05MNFrrIrm4k}SIIUPP@+wNG#A;PIEFxUlLQH1sGgxD zqM%fz=*lUo$|;qqI4&wT|ASm|$sb5gIm|UD-*U*wl|#PQJs5(Fta4?kN(I#P`nvnQ z*Kg~m`U_ z@>2D*m##aWqZ6D_%hWSoMz+mb*6YHUS<87jS$BIqJh#{TO!NAf#cY;ZGrc3t9^SXt z&(hCyUHe39T(UHNbTcb{scYVVt~GP~7$4Y`b2qS;H}Io@PqojqhViM!9G3Y~^NzDD z>q32kz0Pv1`%B$B$$D5XMh00QJA(QY>t{z%zr+UEG1RBoadrar8FrnWWP@07mYrfR zVdQ0Ynw>%IvK#Cydl@t5n9I&#BHjQ8C;vqk=tLYk6USe0- zwWpw$nTuNfjc@5z@=jz=*UG_W)GK&{aZw6+eXCXudDLAmZ}O7h!Is|$c$60W!8Q-V z$Pt`X1+RordRx>=tGz+3gkNP|s=BrAH@KHw zsg*06D_n@3W_qmRH&&}_6+)%`gIH)Oe3STsw@`%IhQ48hCesHY27_AMGLx2-v}{16 zo?1_|mIe~j#mKJj1P^MFb;l1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d0(3T=X5lwwLTnO^!z?Uj!AYJ{@I(&~zt z6OaEalr84Orl2>CLYhKab)VoSQnxsIxRP-?PZ@y+*r2^@{O=dg~xC`Ie1i^ z^=z87ChTrOq<}v@2(Go?BYbZn)R42i68~J%@00x_=v#p6zp1gKs0UCVOYr%5*we-a zJKON(wp5cG)$#c&>^Lct!dce9m!0^^c#;ZxTPX=oJUvM~g##T-cK?HfK7}Aj@ox|< zC+usbTMoqw@+R7=UP@N|IZW*xk;$pk$=F}Re!17_n4@Rn8AF_fYM+&JXF81outi_d zyMN;_wJ#Sv3V!ZUKox0H<^(-<1wCx=29j-xYw3Ml_e@0kp1s|mDpMo%B*|~i$JUzO zu=`<~%4Q`tdhu|SN-kA8+jcwzCmTmZ9YLFp!@rdUJq@FDH5jKXnN()+0fxjoC_D<2 z=;&teGD_(kWHuuJ5fp672oTv?x%N7($f~f34v$ix#orFSEV3w#H7<>bMaHDrKPg8N zdxiKke^ijFNmJli!~j7Nmq=4V4?tyY=$$3#JtOGVAkI=TLSq*IVlwTv%ioJM&Y zUKWDlPgo=UafjQvLu<%>c?jmk=BWpB$73_4)=Ps0qtydrHGmz&J?z~~U6$-U zV4UdE65~WrAu_j=?2(@8JcHMgo(cvBUc#21iXc*eNW(zgl;Y|rWeRQNesz{<>G7Iu zdriKpC`DG9eQ^uti1(;?pNe@ZNQ9((Lw@O<)67a_mF-WdE1M!1lZyQdrI$~f3?SZf zLBer}*@7NTf5I2^q0l7LBy&t;&C4FIOG)v1Wx}8SUqL15Wzs&%sRaF2SpelI?FVC9<#0VR{2@Yl_{(Ofg)&6}YKmabB7gTL>TdM)e*ilo BfC&Hq literal 0 HcmV?d00001 diff --git a/shop.py b/shop.py index 98ba8c1..a59e694 100644 --- a/shop.py +++ b/shop.py @@ -120,28 +120,38 @@ app.secret_key = b'*$#@U9423jr92jioJKL_)_;dasfj()12' @app.route('/') def index(): + print(f"ID in session is: {session['active_id']}") if session.get('id') is None: data = {"title":"Login"} return render_template("auth.html", data=data) - try: - if request.args["list"]: - print(request.args["list"]) - res = get_items(session["id"], request.args["list"]) - else: - res = get_items(session["id"]) - data = {"title": "Shopping List", "results": res, "username": session["username"], "list_ids": session["list_ids"]} - except KeyError: - #Get initial data, contains all lists the user is apart of unless list is defined + + if session["active_id"] == "0": + print("Heres") res = get_items(session["id"]) - data = {"title": "Shopping List", "results": res, "username": session["username"], "list_ids": session["list_ids"]} + else: + res = get_items(session["id"], session["active_id"]) + data = {"title": "Shopping List", "results": res, "session": session, "list_ids": session["list_ids"]} + #Store active list in the session + # try: + # if request.args["list"]: + # print(request.args["list"]) + # res = get_items(session["id"], request.args["list"]) + # else: + # res = get_items(session["id"]) + # data = {"title": "Shopping List", "results": res, "username": session["username"], "list_ids": session["list_ids"]} + # except KeyError: + # #Get initial data, contains all lists the user is apart of unless list is defined + # res = get_items(session["id"]) + # data = {"title": "Shopping List", "results": res, "username": session["username"], "list_ids": session["list_ids"]} for device in MOBILES: if device in request.user_agent.platform: return render_template('mobile.html', data=data) + print(session["list_ids"]) return render_template('index.html', data=data) @app.route('/post', methods=['POST']) def handle_data(): - + print(request.form) if "addValue" in request.form: for x in request.form: if request.form[x] == '': @@ -166,6 +176,7 @@ def handle_data(): session["username"] = res[0][1] session["isAdmin"] = res[0][2] session["list_ids"] = list_ids + session["active_id"] = "0" if "newuser" in request.form: #first check if the user exists @@ -188,6 +199,9 @@ def handle_data(): if "changeList" in request.form: pprint(request.form["changeList"]) + if "list" in request.form: + print(f"Change session to {request.form['list']}") + session["active_id"] = request.form['list'] return redirect(url_for('index')) diff --git a/templates/index.html b/templates/index.html index 7f57c8f..bc060cd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,14 +1,14 @@ {% include "header.html" %} -

{{ data["title"] }}, {{data["username"].title()}} +

{{ data["title"] }}, {{data["session"]["username"].title()}}