summaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
Diffstat (limited to 'routes')
-rw-r--r--routes/analyse.py50
-rw-r--r--routes/answer.py100
-rw-r--r--routes/base.py97
-rw-r--r--routes/create.py55
-rw-r--r--routes/get/quiz.py18
-rw-r--r--routes/question.py49
-rw-r--r--routes/set/answers.py32
-rw-r--r--routes/set/nick.py28
-rw-r--r--routes/set/question.py40
-rw-r--r--routes/set/quiz.py13
-rw-r--r--routes/tools.py46
11 files changed, 335 insertions, 193 deletions
diff --git a/routes/analyse.py b/routes/analyse.py
new file mode 100644
index 0000000..b333e70
--- /dev/null
+++ b/routes/analyse.py
@@ -0,0 +1,50 @@
+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
+
+@app.route("/pages/analyse.html")
+def analyse():
+ if "id" in session:
+ sid = session["id"]
+ else:
+ return red["nick"]
+
+ if "answer_id" in session and D.is_user_answered(session["answer_id"],sid):
+ aid = session["answer_id"]
+ else:
+ return render_template("analyse.html",
+ alert=get_alert(),
+ nick=get_nick()
+ )
+
+ uid1 = session["anal_user1"] if "anal_user1" in session else sid
+ uid1 = sid if uid1 != sid and not D.is_user_answered(aid,uid1) else uid1
+
+ 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",
+ alert=get_alert(),
+ nick=get_nick(),
+ code=D.get_quiz_link(aid),
+ questions = rows2dicts( comparable, ['q','n','p','a1','a2','c'] ),
+ users = rows2dicts( D.get_users_answered(aid), ['id','nick'] ),
+ user1=int(uid1),
+ user2=int(uid2),
+ avg = avg
+ )
+
+@app.route("/set/compare",methods=["POST"])
+def set_compare():
+ session["anal_user1"] = request.form["user1"]
+ session["anal_user2"] = request.form["user2"]
+ return redirect("/#analyse")
+
diff --git a/routes/answer.py b/routes/answer.py
new file mode 100644
index 0000000..fa3d138
--- /dev/null
+++ b/routes/answer.py
@@ -0,0 +1,100 @@
+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
+
+
+@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/<link>")
+def kys_link(link):
+ if aid := D.find_quiz_by_link( link ):
+ session["answer_id"] = aid
+ return redirect("/#answer")
+ return redirect("/")
+
+@app.route("/set/answer_id",methods=["POST"])
+def answer_id():
+ if "id" not in session:
+ session["alert"] = "Nimimerkkiä ei ole asetettu."
+ return redirect("/#nick")
+ 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)
+
+ if aid := D.find_quiz_by_link( request.form["link"] ):
+ session["answer_id"] = aid
+ else:
+ session["alert"] = "Koodilla ei löytynyt kyselmää"
+ return redirect(next)
+
+ if next == "/#analyse" and not D.is_user_answered( aid, sid ):
+ session["alert"] = "Et ole vielä vastannut tähän kyselmään. \
+ Voit tutkia vastaksia vastattuasi."
+ return redirect("/#answer")
+
+ return redirect( next )
+
+@app.route("/pages/answer.html")
+def answer():
+ if "id" in session:
+ sid = session["id"]
+ else:
+ return red["nick"]
+
+ if "answer_id" in session:
+ aid = session["answer_id"]
+ else:
+ return red["new_answer"]
+
+ if D.is_user_answered(aid, sid):
+ return red["new_answer"]
+
+ return render_template("answer.html",
+ alert = get_alert(),
+ nick = get_nick(),
+ questions = rows2dicts( D.get_questions(aid), ['i','q','n','p'] ),
+ link = D.get_quiz_link( aid )
+ )
+
+@app.route("/set/answers",methods=["POST"])
+def set_answers():
+ if "id" not in session:
+ session["alert"]="Nimimerkkiä ei ole vielä valittu!"
+ return redirect( "/#nick" )
+ 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():
+ 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) ):
+ 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))
+
+ return redirect("/#analyse")
diff --git a/routes/base.py b/routes/base.py
index 144eeef..695388b 100644
--- a/routes/base.py
+++ b/routes/base.py
@@ -1,24 +1,7 @@
from app import app
-from flask import render_template,session
+from flask import render_template,session,request,redirect
import db_actions as D
-
-def get_alert():
- if "alert" in session:
- alert = session["alert"]
- del session["alert"]
- return f"{alert}"
- return ""
-
-def get_nick():
- 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ä)"
+from routes.tools import rows2dicts, get_alert, get_nick
@app.route("/")
def index():
@@ -29,50 +12,40 @@ def info():
return render_template("info.html",
alert=get_alert()
)
-
-@app.route("/pages/create.html")
-def create():
- if "id" not in session.keys():
- return "redirect = #nick"
- if "quiz_id" not in session.keys():
- return "redirect = #quiz"
- return render_template("create.html",
- alert=get_alert(),
- nick=get_nick()
- )
-
-@app.route("/pages/answer.html")
-def answer():
- if "id" not in session.keys():
- return "redirect = #nick"
- return render_template("answer.html",
- alert=get_alert(),
- nick=get_nick()
- )
-
-@app.route("/pages/analyse.html")
-def analyse():
- if "id" not in session.keys():
- return "redirect = #nick"
- return render_template("analyse.html",
- alert=get_alert(),
- nick=get_nick()
- )
-
-@app.route("/pages/moderate.html")
-def moderate():
- return render_template("moderate.html",
- alert=get_alert()
- )
-
+
@app.route("/pages/nick.html")
def nick():
- return render_template("nick.html", alert=get_alert() )
+ return render_template("nick.html",
+ alert=get_alert()
+ )
-@app.route("/pages/question.html")
-def question():
- return render_template("question.html", alert=get_alert() )
+@app.route("/set/nick",methods=["POST"])
+def new_nick():
+ if "id" in session.keys():
+ session["alert"]="Sinulla on jo nimimerkki. Käytä sitä."
+ return redirect("/")
+ if "nick" not in request.form or request.form["nick"]=="":
+ session["alert"]="Nimimerkkiä ei voi asettaa ilman nimimerkkiä."
+ return redirect("/#nick")
+ else:
+ nick = request.form["nick"]
+ if len(nick) < 4:
+ session["alert"]="Nimimerkki on liian lyhyt"
+ return redirect("/#nick")
+ if not nick.isalnum():
+ session["alert"]="Nimimerkissä saa olla vain kirjaimia ja numeroita."
+ return redirect("/#nick")
+ 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("/")
+
+
+
+#@app.route("/pages/moderate.html")
+#def moderate():
+# return render_template("moderate.html",
+# alert=get_alert()
+# )
-@app.route("/pages/quiz.html")
-def build():
- return render_template("quiz.html", alert=get_alert() )
diff --git a/routes/create.py b/routes/create.py
new file mode 100644
index 0000000..0a2a343
--- /dev/null
+++ b/routes/create.py
@@ -0,0 +1,55 @@
+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, generate_link, red
+
+
+@app.route("/pages/create.html")
+def create():
+ if "id" not in session:
+ return red["nick"]
+ if "quiz_id" not in session:
+ return red["quiz"]
+ if D.get_quiz_link(session["quiz_id"]):
+ return red["quiz"]
+
+ return render_template("create.html",
+ alert=get_alert(),
+ nick=get_nick(),
+ 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")
+ user_id = session["id"]
+ session["quiz_id"] = D.quiz_new( user_id )
+ return redirect("/#create")
+
+
+@app.route("/set/quiz_ready",methods=["POST"])
+def quiz_ready():
+ if "quiz_id" not in session.keys():
+ return "KUOLETTAVA: kyselyä ei ole"
+ 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")
+ quiz_id = session["quiz_id"]
+ session["answer_id"] = session["quiz_id"]
+ D.set_quiz_link(session["quiz_id"], generate_link())
+ return redirect("/#analyse")
+
diff --git a/routes/get/quiz.py b/routes/get/quiz.py
deleted file mode 100644
index 69e2613..0000000
--- a/routes/get/quiz.py
+++ /dev/null
@@ -1,18 +0,0 @@
-from app import app
-from flask import render_template, session, request, redirect, jsonify
-import db_actions as D
-
-
-@app.route("/get/quiz_creator",methods=["GET"])
-def get_questions_by_id():
- if "quiz_id" not in session.keys():
- return "KUOLETTAVA: Sessiota / kyselmä id:tä ei ole"
-
- results = D.get_questions(session['quiz_id'])
- r={}
- names=['i','q','n','p','a']
- for i in range(len(results)):
- r[i]={}
- for j in range(len(results[i])):
- r[i][names[j]]=results[i][j]
- return (jsonify(r))
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")
+
diff --git a/routes/set/answers.py b/routes/set/answers.py
deleted file mode 100644
index 859d65c..0000000
--- a/routes/set/answers.py
+++ /dev/null
@@ -1,32 +0,0 @@
-from app import app
-from flask import render_template, session, request, redirect
-import db_actions as D
-
-def validate_answer(ans):
- if len(ans)<1:
- return False
- try:
- value=int(ans)
- if value<0 or value>1000:
- return False
- except ValueError:
- return False
- return True
-
-@app.route("/set/answers",methods=["POST"])
-def set_answers():
- if "id" not in session.keys():
- return "KUOLETTAVA: Nimimerkkiä ei ole vielä valittu!"
- if "quiz_id" not in session.keys():
- return "KUOLETTAVA: Yrität vastata kyselyyn ilman sen valintaa!"
-
- user_id = session["id"]
- for id, answer in request.form.items():
- question_id = int(id)
- if not validate_answer(answer):
- return "KUOLETTAVA: Epäkelpo vastaus!"
- if D.get_user_answer(user_id,question_id):
- return "KUOLETTAVA: On jo vastattu!"
- D.answer_new(user_id, question_id, answer)
-
- return redirect("/#analyse")
diff --git a/routes/set/nick.py b/routes/set/nick.py
deleted file mode 100644
index 67ddeea..0000000
--- a/routes/set/nick.py
+++ /dev/null
@@ -1,28 +0,0 @@
-from app import app
-from flask import render_template, session, request, redirect
-import db_actions as D
-
-
-@app.route("/set/nick",methods=["POST"])
-def new_nick():
- nick = request.form["nick"]
- if "id" in session.keys():
- msg = "You already have a nick."
- elif D.user_exists(nick):
- msg = "Nick is already reserved."
- elif msg := invalid_nick(nick):
- pass
- else:
- session["id"] = D.user_new(nick)
- return redirect("/")
- session["alert"]="Nick in not created: "+msg
- return redirect("/#nick")
-
-
-def invalid_nick(nick):
- if len(nick)<4:
- return "Nick is too short"
- if not nick.isalnum():
- return "Only letters and numbers are allowed"
- return 0
- \ No newline at end of file
diff --git a/routes/set/question.py b/routes/set/question.py
deleted file mode 100644
index deaf9be..0000000
--- a/routes/set/question.py
+++ /dev/null
@@ -1,40 +0,0 @@
-from app import app
-from flask import render_template, session, request, redirect
-import db_actions as D
-
-
-def validate_answer(ans):
- if len(ans)<1:
- return False
- return True
-
-def validate_question(question):
- if len(question)<2:
- return False
- return True
-
-@app.route("/set/question",methods=["POST"])
-def new_question():
- question = request.form["question"]
- neg_ans = request.form["neg_ans"]
- pos_ans = request.form["pos_ans"]
- answer = request.form["answer"]
- if not validate_question(question):
- msg = "Kysymys on virheellinen"
- elif not validate_answer(neg_ans):
- msg = "Vasen selite on virheellinen"
- elif not validate_answer(pos_ans):
- msg = "Oikea selite on virheellinen"
- elif "id" not in session.keys():
- msg = "Tarvitaan nimimerkki"
- elif "quiz_id" not in session.keys():
- msg = "Ei voi lisätä kysymystä ilman kyselmää"
- else:
- quiz_id = session["quiz_id"]
- user_id = session["id"]
- question_id = D.question_new( question, neg_ans, pos_ans )
- D.quiz_add(quiz_id, question_id)
- D.answer_new(user_id, question_id, answer)
- return redirect("/#create")
- session["alert"]="Kysymystä ei luotu: "+msg
- return redirect("/#create")
diff --git a/routes/set/quiz.py b/routes/set/quiz.py
deleted file mode 100644
index 9ad13da..0000000
--- a/routes/set/quiz.py
+++ /dev/null
@@ -1,13 +0,0 @@
-from app import app
-from flask import render_template, session, request, redirect
-import db_actions as D
-
-
-@app.route("/set/quiz",methods=["POST"])
-def new_quiz():
- if not "id" in session.keys():
- session["alert"]="Tarvitset nimimerkin loudaksesi"
- return redirect("/#nick")
- user_id = session["id"]
- session["quiz_id"] = D.quiz_new( user_id )
- return redirect("/#create")
diff --git a/routes/tools.py b/routes/tools.py
new file mode 100644
index 0000000..69e6fef
--- /dev/null
+++ b/routes/tools.py
@@ -0,0 +1,46 @@
+from random import randint
+from flask import session
+import db_actions as D
+
+red = {
+ "nick": "<script>window.location.hash=\"nick\"</script>",
+ "new_answer": "<script>window.location.hash=\"new_answer\"</script>",
+ "quiz": "<script>window.location.hash=\"quiz\"</script>"
+}
+
+
+def rows2dicts( rows, names ):
+ dlist=[]
+ for i in range(len(rows)):
+ row={}
+ for j in range(len(names)):
+ row[names[j]]=rows[i][j]
+ dlist.append(row)
+ return dlist
+
+def get_alert():
+ if "alert" in session:
+ alert = session["alert"]
+ del session["alert"]
+ return f"{alert}"
+ return ""
+
+def get_nick():
+ 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ä)"
+
+def generate_link():
+ konso="rtpshjklvnm"
+ vocal="eyuioa"
+ str=""
+ for i in range(4):
+ str+=konso[randint(0,len(konso)-1)]
+ str+=vocal[randint(0,len(vocal)-1)]
+ return str