Finished storing the list in the session

This commit is contained in:
Benjamyn Love 2020-02-17 19:42:37 +11:00
parent a3254343cc
commit d6ccc91a9f
2 changed files with 18 additions and 20 deletions

26
shop.py
View File

@ -109,9 +109,13 @@ def get_items(user_id, list_id=None):
return runQuery(query, data) return runQuery(query, data)
def get_list_ids(user_id): def get_list_ids(user_id):
ret = {}
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" 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,) data = (user_id,)
ret = runQuery(query, data) res = runQuery(query, data)
for lid, name in res:
ret[lid] = name
pprint(ret)
return ret return ret
app = Flask(__name__) app = Flask(__name__)
@ -120,29 +124,17 @@ app.secret_key = b'*$#@U9423jr92jioJKL_)_;dasfj()12'
@app.route('/') @app.route('/')
def index(): def index():
print(f"ID in session is: {session['active_id']}")
if session.get('id') is None: if session.get('id') is None:
data = {"title":"Login"} data = {"title":"Login"}
return render_template("auth.html", data=data) return render_template("auth.html", data=data)
if session["active_id"] == "0": if session.get("active_id") == "0":
print("Heres") print("Heres")
res = get_items(session["id"]) res = get_items(session["id"])
else: else:
res = get_items(session["id"], session["active_id"]) res = get_items(session["id"], session["active_id"])
data = {"title": "Shopping List", "results": res, "session": session, "list_ids": session["list_ids"]} 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: for device in MOBILES:
if device in request.user_agent.platform: if device in request.user_agent.platform:
return render_template('mobile.html', data=data) return render_template('mobile.html', data=data)
@ -151,7 +143,6 @@ def index():
@app.route('/post', methods=['POST']) @app.route('/post', methods=['POST'])
def handle_data(): def handle_data():
print(request.form)
if "addValue" in request.form: if "addValue" in request.form:
for x in request.form: for x in request.form:
if request.form[x] == '': if request.form[x] == '':
@ -199,6 +190,7 @@ def handle_data():
if "changeList" in request.form: if "changeList" in request.form:
pprint(request.form["changeList"]) pprint(request.form["changeList"])
if "list" in request.form: if "list" in request.form:
print(f"Change session to {request.form['list']}") print(f"Change session to {request.form['list']}")
session["active_id"] = request.form['list'] session["active_id"] = request.form['list']

View File

@ -5,12 +5,18 @@
<div class="dropdown"> <div class="dropdown">
<form action="/post" method="post" id="test"> <form action="/post" method="post" id="test">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{data["session"]["active_id"]}} {% set id = session["list_ids"] %}
{% set actid = session["active_id"] %}
{% if actid == "0" %}
ALL
{% else %}
{{ id[actid] }}
{% endif %}
</button> </button>
<div class="dropdown-menu" aria-labelledby="dropdownMenu2"> <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
<button class="dropdown-item" type="submit" value="0" id="" name="list">All</button> <button class="dropdown-item" type="submit" value="0" id="" name="list">All</button>
{% for id, name in data["list_ids"] %} {% for listid in data["list_ids"] %}
<button class="dropdown-item" type="submit" value="{{id}}" id="{{id}}" name="list">{{name}}</button> <button class="dropdown-item" type="submit" value="{{listid}}" id="{{id}}" name="list">{{id[listid]}}</button>
{% endfor %} {% endfor %}
</div> </div>
</form> </form>