diff options
author | Minea Salmimaa <msalmima@local> | 2023-12-03 12:44:45 +0200 |
---|---|---|
committer | Minea Salmimaa <msalmima@local> | 2023-12-03 12:44:45 +0200 |
commit | e0ca9caf5a49981ad999b1c0c3fec69d7c4871e4 (patch) | |
tree | 6402666bec2999dda0e1e7460b91b21e17310479 | |
parent | 7353c51df2d074cbe01913044bda7aeef62a31b7 (diff) |
Moving quiz related db actions to separete class and file.
-rw-r--r-- | db/db.py | 47 | ||||
-rw-r--r-- | db/quiz.py | 51 | ||||
-rw-r--r-- | routes/analyse.py | 2 | ||||
-rw-r--r-- | routes/answer.py | 6 | ||||
-rw-r--r-- | routes/create.py | 6 | ||||
-rw-r--r-- | routes/question.py | 2 |
6 files changed, 61 insertions, 53 deletions
@@ -5,12 +5,14 @@ from sqlalchemy.sql import text from app import app from db.user import DBUser +from db.quiz import DBQuiz class DB: def __init__(self): self.db = SQLAlchemy() self.db.init_app(app) self.user = DBUser(self.db) + self.quiz = DBQuiz(self.db) def question_new(self, question, neg_ans, pos_ans ): sql = "INSERT \ @@ -26,51 +28,6 @@ class DB: self.db.session.commit() return result.fetchone()[0] - def quiz_new(self, user_id): - sql = "INSERT \ - INTO questionaires (creator_id) \ - VALUES (:creator_id) \ - RETURNING id ;" - result = self.db.session.execute( text(sql), - { "creator_id":user_id } ) - self.db.session.commit() - return result.fetchone()[0] - - def quiz_add(self, quiz_id, question_id ): - sql = "UPDATE questionaires \ - SET questionset = ARRAY_APPEND(questionset, :question_id) \ - WHERE id=:quiz_id;" - self.db.session.execute(text(sql), { - "quiz_id":quiz_id, - "question_id":question_id - }) - self.db.session.commit() - - - def set_quiz_link( self, quiz_id, link ): - sql = "INSERT \ - INTO quiz_links (quiz_id, link) \ - VALUES (:quiz_id, :link)" - result = self.db.session.execute( text(sql), - { "quiz_id":quiz_id, "link":link } ) - self.db.session.commit() - - - def find_quiz_by_link( self, link ): - sql = "SELECT quiz_id \ - FROM quiz_links \ - WHERE link=:link;" - result = self.db.session.execute(text(sql), { "link":link }).fetchone() - return result[0] if result else False - - - def get_quiz_link( self, quiz_id ): - sql = "SELECT link \ - FROM quiz_links \ - WHERE quiz_id=:quiz_id;" - result = self.db.session.execute(text(sql), { "quiz_id":quiz_id }).fetchone() - return result[0] if result else result - def answer_new(self, user_id, question_id, answer): sql = "INSERT \ diff --git a/db/quiz.py b/db/quiz.py new file mode 100644 index 0000000..8a2bffd --- /dev/null +++ b/db/quiz.py @@ -0,0 +1,51 @@ +from sqlalchemy.sql import text + +class DBQuiz: + def __init__(self, db): + self.db = db + + def new(self, user_id): + sql = "INSERT \ + INTO questionaires (creator_id) \ + VALUES (:creator_id) \ + RETURNING id ;" + result = self.db.session.execute( text(sql), + { "creator_id":user_id } ) + self.db.session.commit() + return result.fetchone()[0] + + def add(self, quiz_id, question_id ): + sql = "UPDATE questionaires \ + SET questionset = ARRAY_APPEND(questionset, :question_id) \ + WHERE id=:quiz_id;" + self.db.session.execute(text(sql), { + "quiz_id":quiz_id, + "question_id":question_id + }) + self.db.session.commit() + + + def set_link( self, quiz_id, link ): + sql = "INSERT \ + INTO quiz_links (quiz_id, link) \ + VALUES (:quiz_id, :link)" + result = self.db.session.execute( text(sql), + { "quiz_id":quiz_id, "link":link } ) + self.db.session.commit() + + + def find_by_link( self, link ): + sql = "SELECT quiz_id \ + FROM quiz_links \ + WHERE link=:link;" + result = self.db.session.execute(text(sql), { "link":link }).fetchone() + return result[0] if result else False + + + def get_link( self, quiz_id ): + sql = "SELECT link \ + FROM quiz_links \ + WHERE quiz_id=:quiz_id;" + result = self.db.session.execute(text(sql), + { "quiz_id":quiz_id }).fetchone() + return result[0] if result else result diff --git a/routes/analyse.py b/routes/analyse.py index 8175d47..7f968d0 100644 --- a/routes/analyse.py +++ b/routes/analyse.py @@ -82,7 +82,7 @@ def analyse(): caller="analyse", alert=get_alert(), nick=get_nick(), - code=D.get_quiz_link(aid), + code=D.quiz.get_link(aid), questions = rows2dicts( comparable, ['q','n','p','a1','a2','c'] ), users = rows2dicts( D.get_users_answered(aid), ['id','nick'] ), user1=int(uid1), diff --git a/routes/answer.py b/routes/answer.py index 89b6aa8..f3a506c 100644 --- a/routes/answer.py +++ b/routes/answer.py @@ -5,7 +5,7 @@ from routes.tools import rows2dicts, get_alert, get_nick, csrf_check @app.route("/kys/<link>") def kys_link(link): - if aid := D.find_quiz_by_link( link ): + if aid := D.quiz.find_by_link( link ): session["answer_id"] = aid return redirect("/#answer") return redirect("/") @@ -25,7 +25,7 @@ def answer_id(): session["alert"] = "Kyselmän nimeä ei ole annettu." return redirect(next) - if aid := D.find_quiz_by_link( request.form["link"] ): + if aid := D.quiz.find_by_link( request.form["link"] ): session["answer_id"] = aid else: session["alert"] = "Koodilla ei löytynyt kyselmää" @@ -77,7 +77,7 @@ def answer(): alert = get_alert(), nick = get_nick(), questions = rows2dicts( D.get_questions(aid), ['i','q','n','p'] ), - link = D.get_quiz_link( aid ) + link = D.quiz.get_link( aid ) ) @app.route("/set/answers",methods=["POST"]) diff --git a/routes/create.py b/routes/create.py index af2e25e..7322079 100644 --- a/routes/create.py +++ b/routes/create.py @@ -18,7 +18,7 @@ def create(): alert=get_alert(), nick=get_nick() ) - if D.get_quiz_link(session["quiz_id"]): + if D.quiz.get_link(session["quiz_id"]): return render_template( "create.html", caller="create", @@ -46,7 +46,7 @@ def new_quiz(): session["alert"]="Tarvitset nimimerkin loudaksesi." return redirect("/#create") user_id = session["id"] - session["quiz_id"] = D.quiz_new( user_id ) + session["quiz_id"] = D.quiz.new( user_id ) return redirect("/#create") @@ -62,5 +62,5 @@ def quiz_ready(): return redirect("/#create") quiz_id = session["quiz_id"] session["answer_id"] = session["quiz_id"] - D.set_quiz_link(session["quiz_id"], generate_link()) + D.quiz.set_link(session["quiz_id"], generate_link()) return redirect("/#analyse") diff --git a/routes/question.py b/routes/question.py index 41722d5..9ddb527 100644 --- a/routes/question.py +++ b/routes/question.py @@ -46,7 +46,7 @@ def new_question(): return redirect("/#question") question_id = D.question_new( question, neg_ans, pos_ans ) - D.quiz_add(qid, question_id) + D.quiz.add(qid, question_id) D.answer_new(sid, question_id, answer) return redirect("/#create") |