Added list_ids to the session, with getter func

This commit is contained in:
Benjamyn Love 2020-02-09 18:20:50 +11:00
parent 0bb5a5823b
commit 2811a784cc

26
shop.py
View File

@ -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