diff options
author | Jani Peter Karhunen <jkarhun2@local> | 2023-11-25 03:32:29 +0200 |
---|---|---|
committer | Jani Peter Karhunen <jkarhun2@local> | 2023-11-25 03:32:29 +0200 |
commit | d2de3ae10b6f5314bd4b8243218212d2004f35b0 (patch) | |
tree | 2ab448e0c2ec361478851dbcb43bcd176d17b584 /routes/question.py | |
parent | c059696450d301b99956d643c21b0e46b4fc39dc (diff) |
MEGA Commit. Rework of routing. Visuals. Answer analysis and much more.
Diffstat (limited to 'routes/question.py')
-rw-r--r-- | routes/question.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/routes/question.py b/routes/question.py new file mode 100644 index 0000000..02261ca --- /dev/null +++ b/routes/question.py @@ -0,0 +1,49 @@ +from app import app +from flask import render_template,session,request,redirect +import db_actions as D +from routes.tools import rows2dicts, get_alert, get_nick + +@app.route("/pages/question.html") +def question(): + return render_template("question.html", + alert=get_alert(), + nick=get_nick() + ) + +@app.route("/set/question",methods=["POST"]) +def new_question(): + try: + question = request.form["question"] + neg_ans = request.form["neg_ans"] + pos_ans = request.form["pos_ans"] + answer = int(request.form["answer"]) + except (KeyError, ValueError): + session["alert"] = "Nyt kaikkea ei tullut perille tai jotain outoa." + return redirect("/#question") + + try: + sid = session["id"] + except (KeyError): + session["alert"] = "Nimimerkki puuttuukin." + return redirect("/#nick") + + try: + qid = session["quiz_id"] + except (KeyError): + session["alert"] = "Kyselmän luonti ei ollutkaan kesken." + return redirect("/#quiz") + + for entry in [question, neg_ans, pos_ans]: + if len(entry) < 2 or len(entry) > 80: + session["alert"] = "Syötteiden tulee olla 2-80 merkkiä pitkiä" + return redirect("/#question") + + if answer < 0 or answer > 999: + session["alert"] = "Vastauksessasi on nyt jotain häikkää." + return redirect("/#question") + + question_id = D.question_new( question, neg_ans, pos_ans ) + D.quiz_add(qid, question_id) + D.answer_new(sid, question_id, answer) + return redirect("/#create") + |