From 03b031ea793e91ce2f87cf28c73cefc8ce69a7b5 Mon Sep 17 00:00:00 2001 From: Lilian Teronen Date: Mon, 27 Nov 2023 01:37:03 +0200 Subject: More rework on routes. Best and worst matches. More visuals. --- routes/analyse.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++----- routes/answer.py | 55 +++++++++++++++++++++++++++---------------------- routes/base.py | 39 ++++++++++++++++------------------- routes/create.py | 42 ++++++++++++++++++++++---------------- routes/question.py | 8 +++++--- 5 files changed, 134 insertions(+), 70 deletions(-) (limited to 'routes') diff --git a/routes/analyse.py b/routes/analyse.py index b333e70..3076d99 100644 --- a/routes/analyse.py +++ b/routes/analyse.py @@ -1,19 +1,66 @@ +from itertools import combinations 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, red +def find_best_and_worst(aid, uid): + answers=D.get_all_answers_for_quiz(aid) + alist=rows2dicts( answers, ['q','u','a'] ) + questions = set(x['q'] for x in alist) + users = set(x['u'] for x in alist) + data = {} + for q in questions: + data[q]={} + for i in alist: + data[i['q']][i['u']]=i['a'] + match = {} + comb = list(combinations(users,2)) + if len(comb)<1: + comb=[(uid,uid)] + min, minme, max, maxme = 101, 101, -1, -1 + for pair in comb: + sum=0 + for q in questions: + sum += 1000 - abs(data[q][pair[0]]-data[q][pair[1]]) + match[pair]=int(sum / len(questions) / 10 + 0.5) + if match[pair] < min: + min = match[pair] + min_pair = pair + if match[pair] > max: + max = match[pair] + max_pair = pair + if pair[0]==uid or pair[1]==uid: + if match[pair] < minme: + minme = match[pair] + minme_pair = pair + if match[pair] > maxme: + maxme = match[pair] + maxme_pair = pair + return ( { + 'min': min, 'min_u1': min_pair[0], 'min_u2': min_pair[1], + 'max': max, 'max_u1': max_pair[0], 'max_u2': max_pair[1], + 'minme': minme, 'minme_u1': minme_pair[0], 'minme_u2': minme_pair[1], + 'maxme': maxme, 'maxme_u1': maxme_pair[0], 'maxme_u2': maxme_pair[1] }) + + @app.route("/pages/analyse.html") def analyse(): if "id" in session: sid = session["id"] else: - return red["nick"] + return render_template( + "analyse.html", + caller="analyse", + alert=get_alert() + ) if "answer_id" in session and D.is_user_answered(session["answer_id"],sid): aid = session["answer_id"] else: - return render_template("analyse.html", + return render_template( + "analyse.html", + caller="analyse", alert=get_alert(), nick=get_nick() ) @@ -24,14 +71,16 @@ def analyse(): uid2 = session["anal_user2"] if "anal_user2" in session else sid uid2 = sid if uid2 != sid and not D.is_user_answered(aid,uid2) else uid2 + comparable = D.get_comparable( aid, uid1, uid2 ) avg=0 for i in range(len(comparable)): avg += comparable[i][5] avg//=len(comparable) - - return render_template("analyse.html", + return render_template( + "analyse.html", + caller="analyse", alert=get_alert(), nick=get_nick(), code=D.get_quiz_link(aid), @@ -39,7 +88,8 @@ def analyse(): users = rows2dicts( D.get_users_answered(aid), ['id','nick'] ), user1=int(uid1), user2=int(uid2), - avg = avg + avg = avg, + best = find_best_and_worst(aid, sid) ) @app.route("/set/compare",methods=["POST"]) diff --git a/routes/answer.py b/routes/answer.py index fa3d138..2fbeec8 100644 --- a/routes/answer.py +++ b/routes/answer.py @@ -4,15 +4,6 @@ import db_actions as D from routes.tools import rows2dicts, get_alert, get_nick, red -@app.route("/pages/new_answer.html") -def new_answer(): - if "id" not in session: - return red["nick"] - return render_template("new_answer.html", - alert=get_alert(), - nick=get_nick() - ) - @app.route("/kys/") def kys_link(link): if aid := D.find_quiz_by_link( link ): @@ -22,17 +13,13 @@ def kys_link(link): @app.route("/set/answer_id",methods=["POST"]) def answer_id(): + next = "/#"+request.form["caller"] if "caller" in request.form else "/" if "id" not in session: session["alert"] = "Nimimerkkiä ei ole asetettu." - return redirect("/#nick") + return redirect(next) else: sid = session["id"] - if "next" not in request.form: - next = "/#answer" - else: - next = "/#"+request.form["next"] - if "link" not in request.form or request.form["link"]=="": session["alert"] = "Kyselmän nimeä ei ole annettu." return redirect(next) @@ -48,6 +35,10 @@ def answer_id(): Voit tutkia vastaksia vastattuasi." return redirect("/#answer") + if next == "/#answer" and D.is_user_answered( aid, sid ): + session["alert"] = "Olet jo vastannut valitsemaasi kyselyyn." + return redirect("/#analyse") + return redirect( next ) @app.route("/pages/answer.html") @@ -55,17 +46,33 @@ def answer(): if "id" in session: sid = session["id"] else: - return red["nick"] + return render_template( + "answer.html", + caller = "answer", + alert = get_alert() + ) if "answer_id" in session: aid = session["answer_id"] else: - return red["new_answer"] + return render_template( + "answer.html", + caller = "answer", + alert = get_alert(), + nick = get_nick() + ) if D.is_user_answered(aid, sid): - return red["new_answer"] + return render_template( + "answer.html", + caller = "answer", + alert = get_alert(), + nick = get_nick() + ) - return render_template("answer.html", + return render_template( + "answer.html", + caller = "answer", alert = get_alert(), nick = get_nick(), questions = rows2dicts( D.get_questions(aid), ['i','q','n','p'] ), @@ -76,25 +83,25 @@ def answer(): def set_answers(): if "id" not in session: session["alert"]="Nimimerkkiä ei ole vielä valittu!" - return redirect( "/#nick" ) + return redirect( "/#answer" ) if "answer_id" not in session: session["alert"]="Kyselyä ei ole valittu vastaamista varten!" return redirect( "/#answer" ) sid = session["id"] - for qid, answer in request.form.items(): + for question, answer in request.form.items(): try: if int(answer) < 0 or int(answer) > 999: session["alert"]="Luvattoman pieniä tai suuria lukuja!" return redirect( "/#answer" ) - elif D.get_user_answer(sid, int(qid) ): + elif D.get_user_answer(int(sid), int(question)) != -1: session["alert"]="Kyselyyn olikin jo saatu vastauksia." return redirect( "/#answer" ) except ValueError: session["alert"] = "Vastaukset ei ole lukuja!" return redirect( "/#answer" ) - for qid, answer in request.form.items(): - D.answer_new(sid, int(qid), int(answer)) + for question, answer in request.form.items(): + D.answer_new(int(sid), int(question), int(answer)) return redirect("/#analyse") diff --git a/routes/base.py b/routes/base.py index 695388b..22245c5 100644 --- a/routes/base.py +++ b/routes/base.py @@ -9,43 +9,40 @@ def index(): @app.route("/pages/info.html") def info(): - return render_template("info.html", + if "id" in session: + return render_template( + "info.html", + caller="info", + alert=get_alert(), + nick=get_nick() + ) + return render_template( + "info.html", + caller="info", alert=get_alert() ) -@app.route("/pages/nick.html") -def nick(): - return render_template("nick.html", - alert=get_alert() - ) - @app.route("/set/nick",methods=["POST"]) def new_nick(): + next = "/#"+request.form["caller"] if "caller" in request.form else "/" if "id" in session.keys(): session["alert"]="Sinulla on jo nimimerkki. Käytä sitä." - return redirect("/") + return redirect(next) if "nick" not in request.form or request.form["nick"]=="": session["alert"]="Nimimerkkiä ei voi asettaa ilman nimimerkkiä." - return redirect("/#nick") + return redirect(next) else: nick = request.form["nick"] if len(nick) < 4: session["alert"]="Nimimerkki on liian lyhyt" - return redirect("/#nick") + return redirect(next) if not nick.isalnum(): session["alert"]="Nimimerkissä saa olla vain kirjaimia ja numeroita." - return redirect("/#nick") + return redirect(next) if D.user_exists(nick): session["alert"]="Nimimerkki jonka olet ottamassa on jo varattu." - return redirect("/#nick") - session["id"] = D.user_new(nick) - return redirect("/") - - + return redirect(next) + session["id"] = int(D.user_new(nick)) + return redirect(next) -#@app.route("/pages/moderate.html") -#def moderate(): -# return render_template("moderate.html", -# alert=get_alert() -# ) diff --git a/routes/create.py b/routes/create.py index 0a2a343..36c2521 100644 --- a/routes/create.py +++ b/routes/create.py @@ -7,35 +7,43 @@ from routes.tools import rows2dicts, get_alert, get_nick, generate_link, red @app.route("/pages/create.html") def create(): if "id" not in session: - return red["nick"] + return render_template( + "create.html", + caller="create", + alert=get_alert() + ) if "quiz_id" not in session: - return red["quiz"] + return render_template( + "create.html", + caller="create", + alert=get_alert(), + nick=get_nick() + ) if D.get_quiz_link(session["quiz_id"]): - return red["quiz"] + return render_template( + "create.html", + caller="create", + alert=get_alert(), + nick=get_nick() + ) - return render_template("create.html", + return render_template( + "create.html", + caller="create", alert=get_alert(), nick=get_nick(), + quiz_set=True, questions=rows2dicts( D.get_questions(session["quiz_id"]), ['i','q','n','p','a'] ) ) -@app.route("/pages/quiz.html") -def build(): - if "id" not in session: - return red["nick"] - return render_template("quiz.html", - alert=get_alert(), - nick=get_nick() - ) - @app.route("/set/quiz",methods=["POST"]) def new_quiz(): if not "id" in session.keys(): - session["alert"]="Tarvitset nimimerkin loudaksesi" - return redirect("/#nick") + session["alert"]="Tarvitset nimimerkin loudaksesi." + return redirect("/#create") user_id = session["id"] session["quiz_id"] = D.quiz_new( user_id ) return redirect("/#create") @@ -44,7 +52,8 @@ def new_quiz(): @app.route("/set/quiz_ready",methods=["POST"]) def quiz_ready(): if "quiz_id" not in session.keys(): - return "KUOLETTAVA: kyselyä ei ole" + session["alert"] = "Kyselmä jota ei ole aloitettu ei voi olla valmis." + return redirect("/#create") if not D.is_user_answered(session["quiz_id"], session["id"]): session["alert"] = "Tyhjän kyselmän luominen ei käy päinsä!" return redirect("/#create") @@ -52,4 +61,3 @@ def quiz_ready(): session["answer_id"] = session["quiz_id"] D.set_quiz_link(session["quiz_id"], generate_link()) return redirect("/#analyse") - diff --git a/routes/question.py b/routes/question.py index 02261ca..ad31993 100644 --- a/routes/question.py +++ b/routes/question.py @@ -5,7 +5,9 @@ from routes.tools import rows2dicts, get_alert, get_nick @app.route("/pages/question.html") def question(): - return render_template("question.html", + return render_template( + "question.html", + caller="create", alert=get_alert(), nick=get_nick() ) @@ -25,13 +27,13 @@ def new_question(): sid = session["id"] except (KeyError): session["alert"] = "Nimimerkki puuttuukin." - return redirect("/#nick") + return redirect("/#create") try: qid = session["quiz_id"] except (KeyError): session["alert"] = "Kyselmän luonti ei ollutkaan kesken." - return redirect("/#quiz") + return redirect("/#create") for entry in [question, neg_ans, pos_ans]: if len(entry) < 2 or len(entry) > 80: -- cgit v1.2.3