diff options
author | Jari Wiik <jwiik@local> | 2023-11-16 22:46:25 +0200 |
---|---|---|
committer | Jari Wiik <jwiik@local> | 2023-11-16 22:46:25 +0200 |
commit | caae3944492b43e470babc500b913e285bf1ea88 (patch) | |
tree | 03f5e2273496398f93f51dec1780f4528d0c3b98 | |
parent | 0ed244ef2dd27a835ec12fdbc93a1f20bbcd89eb (diff) |
Fix lock-up situation when database gets zapped while session is still on
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | db_actions.py | 3 | ||||
-rw-r--r-- | routes.py | 14 |
3 files changed, 19 insertions, 5 deletions
@@ -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) \ @@ -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(), |