From b868d0ba58384fce5a9a45ae153dee9815327d55 Mon Sep 17 00:00:00 2001 From: Senni Heidari Date: Tue, 21 Nov 2023 11:36:12 +0200 Subject: Update directory hierarchy --- app.py | 10 +++---- get_questions.py | 18 ------------ nick.py | 28 ------------------ question.py | 40 ------------------------- quiz.py | 13 --------- routes.py | 78 ------------------------------------------------- routes/base.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ routes/get/quiz.py | 18 ++++++++++++ routes/set/nick.py | 28 ++++++++++++++++++ routes/set/question.py | 40 +++++++++++++++++++++++++ routes/set/quiz.py | 13 +++++++++ static/create.js | 2 +- templates/nick.html | 2 +- templates/question.html | 2 +- templates/quiz.html | 2 +- 15 files changed, 186 insertions(+), 186 deletions(-) delete mode 100644 get_questions.py delete mode 100644 nick.py delete mode 100644 question.py delete mode 100644 quiz.py delete mode 100644 routes.py create mode 100644 routes/base.py create mode 100644 routes/get/quiz.py create mode 100644 routes/set/nick.py create mode 100644 routes/set/question.py create mode 100644 routes/set/quiz.py diff --git a/app.py b/app.py index 7124bd6..996be9b 100644 --- a/app.py +++ b/app.py @@ -7,8 +7,8 @@ app.secret_key = getenv("SECRET_KEY") app.config["SQLALCHEMY_DATABASE_URI"] = getenv("SQLALCHEMY_DATABASE_URI") db.init_app(app) -import routes -import nick -import quiz -import question -import get_questions +import routes.base +import routes.set.nick +import routes.set.quiz +import routes.set.question +import routes.get.quiz diff --git a/get_questions.py b/get_questions.py deleted file mode 100644 index 496b99a..0000000 --- a/get_questions.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_questions",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/nick.py b/nick.py deleted file mode 100644 index f476d29..0000000 --- a/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("/new_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/question.py b/question.py deleted file mode 100644 index 96673c2..0000000 --- a/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("/new_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/quiz.py b/quiz.py deleted file mode 100644 index a136209..0000000 --- a/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("/new_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.py b/routes.py deleted file mode 100644 index 144eeef..0000000 --- a/routes.py +++ /dev/null @@ -1,78 +0,0 @@ -from app import app -from flask import render_template,session -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ä)" - -@app.route("/") -def index(): - return app.send_static_file("index.html") - -@app.route("/pages/info.html") -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() ) - -@app.route("/pages/question.html") -def question(): - return render_template("question.html", alert=get_alert() ) - -@app.route("/pages/quiz.html") -def build(): - return render_template("quiz.html", alert=get_alert() ) diff --git a/routes/base.py b/routes/base.py new file mode 100644 index 0000000..144eeef --- /dev/null +++ b/routes/base.py @@ -0,0 +1,78 @@ +from app import app +from flask import render_template,session +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ä)" + +@app.route("/") +def index(): + return app.send_static_file("index.html") + +@app.route("/pages/info.html") +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() ) + +@app.route("/pages/question.html") +def question(): + return render_template("question.html", alert=get_alert() ) + +@app.route("/pages/quiz.html") +def build(): + return render_template("quiz.html", alert=get_alert() ) diff --git a/routes/get/quiz.py b/routes/get/quiz.py new file mode 100644 index 0000000..69e2613 --- /dev/null +++ b/routes/get/quiz.py @@ -0,0 +1,18 @@ +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/set/nick.py b/routes/set/nick.py new file mode 100644 index 0000000..67ddeea --- /dev/null +++ b/routes/set/nick.py @@ -0,0 +1,28 @@ +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 new file mode 100644 index 0000000..deaf9be --- /dev/null +++ b/routes/set/question.py @@ -0,0 +1,40 @@ +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 new file mode 100644 index 0000000..9ad13da --- /dev/null +++ b/routes/set/quiz.py @@ -0,0 +1,13 @@ +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/static/create.js b/static/create.js index 6c25659..d64073f 100644 --- a/static/create.js +++ b/static/create.js @@ -50,7 +50,7 @@ createQuestions = () => { } loadQuestions = async() => { - await fetch( 'get_questions' ) + await fetch( 'get/quiz_creator' ) .then( response => response.json() ) .then( json => questions = json ) .catch( error => { diff --git a/templates/nick.html b/templates/nick.html index f3e1696..fe4a50a 100644 --- a/templates/nick.html +++ b/templates/nick.html @@ -1,5 +1,5 @@
{{ alert }}
-
+ Anna itsellesi nimimerkki ensin: diff --git a/templates/question.html b/templates/question.html index 6d0dca1..016a1ac 100644 --- a/templates/question.html +++ b/templates/question.html @@ -1,5 +1,5 @@
{{ alert }}
- +
Kysymys:
diff --git a/templates/quiz.html b/templates/quiz.html index 4c1871d..b2083ae 100644 --- a/templates/quiz.html +++ b/templates/quiz.html @@ -1,4 +1,4 @@
{{ alert }}
- + -- cgit v1.2.3