From caae3944492b43e470babc500b913e285bf1ea88 Mon Sep 17 00:00:00 2001 From: Jari Wiik Date: Thu, 16 Nov 2023 22:46:25 +0200 Subject: Fix lock-up situation when database gets zapped while session is still on --- README.md | 7 +++++++ db_actions.py | 3 ++- routes.py | 14 ++++++++++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ef15820..1662d8f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,13 @@ # kyselma Kyselmä - kysele, vastaile ja tutki tuloksia +TO GET IT RUNNING: +Install flask, psycopg, postresql, sqlalchemy (as in course material) +$ git clone https://github.com/triionhe/kyselma.git +$ psql -U postgres < SCHEMA.sql (Be careful with '-U postgres' or edit app.py!) +$ SECRET_KEY=923987295 flask run + + DONE: - Voi lisätä kyselyn - Kysymyksen mukana lisätään vastaus diff --git a/db_actions.py b/db_actions.py index 26f69df..fca9ab0 100644 --- a/db_actions.py +++ b/db_actions.py @@ -21,7 +21,8 @@ def user_get_nick(id): sql = "SELECT nick \ FROM users \ WHERE id=(:id);" - return db.session.execute(text(sql), { "id":id }).fetchone()[0] + result = db.session.execute(text(sql), { "id":id }).fetchone() + return result[0] if result else result def user_exists(nick): sql = "SELECT COUNT(id) \ diff --git a/routes.py b/routes.py index 7e7f2ca..144eeef 100644 --- a/routes.py +++ b/routes.py @@ -10,8 +10,14 @@ def get_alert(): return "" def get_nick(): - if "id" in session.keys(): - return D.user_get_nick(session["id"]) + while "id" in session.keys(): + nick = D.user_get_nick(session["id"]) + if not nick: + del session['id'] + if "quiz_id" in session.keys(): + del session['quiz_id'] + break + return nick return "(ei nimimerkkiä)" @app.route("/") @@ -37,7 +43,7 @@ def create(): @app.route("/pages/answer.html") def answer(): - if "id" not in session: + if "id" not in session.keys(): return "redirect = #nick" return render_template("answer.html", alert=get_alert(), @@ -46,7 +52,7 @@ def answer(): @app.route("/pages/analyse.html") def analyse(): - if "id" not in session: + if "id" not in session.keys(): return "redirect = #nick" return render_template("analyse.html", alert=get_alert(), -- cgit v1.2.3