summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEila Väyrynen <evayryne@local>2023-12-03 13:28:36 +0200
committerEila Väyrynen <evayryne@local>2023-12-03 13:28:36 +0200
commit83eefd51d79dc2c0fa778303042c581b4691e82e (patch)
treebb957d130855e7ba798f90604468c3665a81528d
parente0ca9caf5a49981ad999b1c0c3fec69d7c4871e4 (diff)
Rearrenge rest of db actions.
-rw-r--r--db/analyse.py22
-rw-r--r--db/answer.py28
-rw-r--r--db/db.py113
-rw-r--r--db/question.py20
-rw-r--r--db/quiz.py43
-rw-r--r--routes/analyse.py12
-rw-r--r--routes/answer.py8
-rw-r--r--routes/create.py4
-rw-r--r--routes/question.py4
9 files changed, 134 insertions, 120 deletions
diff --git a/db/analyse.py b/db/analyse.py
new file mode 100644
index 0000000..a4076f5
--- /dev/null
+++ b/db/analyse.py
@@ -0,0 +1,22 @@
+from sqlalchemy.sql import text
+
+class DBAnalyse:
+ def __init__(self, db):
+ self.db = db
+
+ def comparable(self, quiz_id,user1,user2):
+ sql = "SELECT q.question, q.neg_answer, q.pos_answer, \
+ a1.answer, a2.answer, \
+ 100 - ABS( a1.answer - a2.answer ) / 10 \
+ FROM questionaires quiz \
+ JOIN questions q ON q.id = ANY(quiz.questionset) \
+ JOIN answers a1 ON a1.user_id = (:user1) \
+ JOIN answers a2 ON a2.user_id = (:user2) \
+ WHERE a1.question_id = q.id \
+ AND a2.question_id = q.id \
+ AND quiz.id = (:quiz_id);"
+ return self.db.session.execute( text(sql), {
+ "quiz_id":quiz_id,
+ "user1":user1,
+ "user2":user2
+ } ).fetchall()
diff --git a/db/answer.py b/db/answer.py
new file mode 100644
index 0000000..8234963
--- /dev/null
+++ b/db/answer.py
@@ -0,0 +1,28 @@
+from sqlalchemy.sql import text
+
+class DBAnswer:
+ def __init__(self, db):
+ self.db = db
+
+ def new(self, user_id, question_id, answer):
+ sql = "INSERT \
+ INTO answers (user_id, question_id, answer ) \
+ VALUES (:user_id, :question_id, :answer);"
+ self.db.session.execute( text(sql), {
+ "question_id":question_id,
+ "user_id":user_id,
+ "answer":answer
+ } )
+ self.db.session.commit()
+
+ def get(self, user_id, question_id):
+ sql = "SELECT answer \
+ FROM answers \
+ WHERE question_id = (:question_id) AND user_id = (:user_id);"
+ result = self.db.session.execute( text(sql), {
+ 'question_id': question_id,
+ 'user_id': user_id
+ } ).fetchone()
+ return result[0] if result else -1
+
+
diff --git a/db/db.py b/db/db.py
index 06b15cc..23a3471 100644
--- a/db/db.py
+++ b/db/db.py
@@ -6,117 +6,18 @@ from app import app
from db.user import DBUser
from db.quiz import DBQuiz
+from db.question import DBQuestion
+from db.answer import DBAnswer
+from db.analyse import DBAnalyse
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 \
- INTO questions (question, neg_answer, pos_answer) \
- VALUES (:question, :neg_answer, :pos_answer) \
- RETURNING id ;"
- result = self.db.session.execute(
- text(sql), {
- "question":question,
- "neg_answer":neg_ans,
- "pos_answer":pos_ans }
- )
- self.db.session.commit()
- return result.fetchone()[0]
-
-
- def answer_new(self, user_id, question_id, answer):
- sql = "INSERT \
- INTO answers (user_id, question_id, answer ) \
- VALUES (:user_id, :question_id, :answer);"
- self.db.session.execute( text(sql), {
- "user_id":user_id,
- "question_id":question_id,
- "answer":answer
- } )
- self.db.session.commit()
-
-
- def get_questions(self, quiz_id):
- sql = "SELECT q.id, q.question, q.neg_answer, q.pos_answer, a.answer \
- FROM questionaires quiz \
- JOIN questions q ON q.id = ANY(quiz.questionset) \
- JOIN answers a ON a.user_id = quiz.creator_id \
- WHERE a.question_id = q.id AND quiz.id = (:quiz_id);"
- return self.db.session.execute( text(sql), { "quiz_id":quiz_id } ).fetchall()
-
- def get_comparable(self, quiz_id,user1,user2):
- sql = "SELECT q.question, q.neg_answer, q.pos_answer, \
- a1.answer, a2.answer, \
- 100 - ABS( a1.answer - a2.answer ) / 10 \
- FROM questionaires quiz \
- JOIN questions q ON q.id = ANY(quiz.questionset) \
- JOIN answers a1 ON a1.user_id = (:user1) \
- JOIN answers a2 ON a2.user_id = (:user2) \
- WHERE a1.question_id = q.id \
- AND a2.question_id = q.id \
- AND quiz.id = (:quiz_id);"
- return self.db.session.execute( text(sql), {
- "quiz_id":quiz_id,
- "user1":user1,
- "user2":user2
- } ).fetchall()
-
-
- def get_user_answer(self, user_id, question_id):
- sql = "SELECT answer \
- FROM answers \
- WHERE question_id = (:question_id) AND user_id = (:user_id);"
- result = self.db.session.execute( text(sql), {
- 'question_id': question_id,
- 'user_id': user_id
- } ).fetchone()
- return result[0] if result else -1
-
-
- def get_user_answers_for_quiz(self, quiz_id, user_id):
- sql = "SELECT a.question_id, a.answer \
- FROM questionaires quiz \
- JOIN answers a ON a.question_id = ANY(quiz.questionset) \
- WHERE a.user_id = (:user_id) AND quiz.id = (:quiz_id);"
- return self.db.session.execute( text(sql), {
- 'user_id': user_id,
- 'quiz_id': quiz_id
- } ).fetchall()
-
- def get_users_answered(self, quiz_id):
- sql = "SELECT DISTINCT a.user_id, u.nick \
- FROM questionaires quiz \
- JOIN answers a ON a.question_id = quiz.questionset[1] \
- JOIN users u ON u.id = a.user_id \
- WHERE quiz.id = (:quiz_id);"
- return self.db.session.execute( text(sql), {
- 'quiz_id': quiz_id
- } ).fetchall()
-
- def is_user_answered(self, quiz_id, user_id):
- sql = "SELECT a.answer \
- FROM questionaires quiz \
- JOIN answers a ON a.question_id = quiz.questionset[1] \
- WHERE quiz.id = (:quiz_id) AND a.user_id = (:user_id);"
- results = self.db.session.execute( text(sql), {
- 'quiz_id': quiz_id,
- 'user_id': user_id
- } ).fetchone()
- return True if results else False
-
-
- def get_all_answers_for_quiz(self, quiz_id):
- sql = "SELECT a.question_id, a.user_id, a.answer \
- FROM questionaires quiz \
- JOIN answers a ON a.question_id = ANY(quiz.questionset) \
- WHERE quiz.id = (:quiz_id);"
- return self.db.session.execute( text(sql), {
- 'quiz_id': quiz_id
- } ).fetchall()
-
+ self.question = DBQuestion(self.db)
+ self.answer = DBAnswer(self.db)
+ self.analyse = DBAnalyse(self.db)
\ No newline at end of file
diff --git a/db/question.py b/db/question.py
new file mode 100644
index 0000000..25f9003
--- /dev/null
+++ b/db/question.py
@@ -0,0 +1,20 @@
+from sqlalchemy.sql import text
+
+class DBQuestion:
+ def __init__(self, db):
+ self.db = db
+
+ def new(self, question, neg_ans, pos_ans ):
+ sql = "INSERT \
+ INTO questions (question, neg_answer, pos_answer) \
+ VALUES (:question, :neg_answer, :pos_answer) \
+ RETURNING id ;"
+ result = self.db.session.execute(
+ text(sql), {
+ "question":question,
+ "neg_answer":neg_ans,
+ "pos_answer":pos_ans }
+ )
+ self.db.session.commit()
+ return result.fetchone()[0]
+
diff --git a/db/quiz.py b/db/quiz.py
index 8a2bffd..806b38b 100644
--- a/db/quiz.py
+++ b/db/quiz.py
@@ -14,6 +14,7 @@ class DBQuiz:
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) \
@@ -49,3 +50,45 @@ class DBQuiz:
result = self.db.session.execute(text(sql),
{ "quiz_id":quiz_id }).fetchone()
return result[0] if result else result
+
+
+ def questions(self, quiz_id):
+ sql = "SELECT q.id, q.question, q.neg_answer, q.pos_answer, a.answer \
+ FROM questionaires quiz \
+ JOIN questions q ON q.id = ANY(quiz.questionset) \
+ JOIN answers a ON a.user_id = quiz.creator_id \
+ WHERE a.question_id = q.id AND quiz.id = (:quiz_id);"
+ return self.db.session.execute( text(sql),
+ { "quiz_id":quiz_id } ).fetchall()
+
+ def answers(self, quiz_id):
+ sql = "SELECT a.question_id, a.user_id, a.answer \
+ FROM questionaires quiz \
+ JOIN answers a ON a.question_id = ANY(quiz.questionset) \
+ WHERE quiz.id = (:quiz_id);"
+ return self.db.session.execute( text(sql), {
+ 'quiz_id': quiz_id
+ } ).fetchall()
+
+
+ def users(self, quiz_id):
+ sql = "SELECT DISTINCT a.user_id, u.nick \
+ FROM questionaires quiz \
+ JOIN answers a ON a.question_id = quiz.questionset[1] \
+ JOIN users u ON u.id = a.user_id \
+ WHERE quiz.id = (:quiz_id);"
+ return self.db.session.execute( text(sql), {
+ 'quiz_id': quiz_id
+ } ).fetchall()
+
+ def user(self, quiz_id, user_id):
+ sql = "SELECT a.answer \
+ FROM questionaires quiz \
+ JOIN answers a ON a.question_id = quiz.questionset[1] \
+ WHERE quiz.id = (:quiz_id) AND a.user_id = (:user_id);"
+ results = self.db.session.execute( text(sql), {
+ 'quiz_id': quiz_id,
+ 'user_id': user_id
+ } ).fetchone()
+ return True if results else False
+
diff --git a/routes/analyse.py b/routes/analyse.py
index 7f968d0..152a189 100644
--- a/routes/analyse.py
+++ b/routes/analyse.py
@@ -4,7 +4,7 @@ from flask import render_template,session,request,redirect
from routes.tools import rows2dicts, get_alert, get_nick, csrf_check
def find_best_and_worst(aid, uid):
- answers=D.get_all_answers_for_quiz(aid)
+ answers=D.quiz.answers(aid)
alist=rows2dicts( answers, ['q','u','a'] )
questions = set(x['q'] for x in alist)
users = set(x['u'] for x in alist)
@@ -54,7 +54,7 @@ def analyse():
alert=get_alert()
)
- if "answer_id" in session and D.is_user_answered(session["answer_id"],sid):
+ if "answer_id" in session and D.quiz.user(session["answer_id"],sid):
aid = session["answer_id"]
else:
return render_template(
@@ -65,13 +65,13 @@ def analyse():
)
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
+ uid1 = sid if uid1 != sid and not D.quiz.user(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
+ uid2 = sid if uid2 != sid and not D.quiz.user(aid,uid2) else uid2
- comparable = D.get_comparable( aid, uid1, uid2 )
+ comparable = D.analyse.comparable( aid, uid1, uid2 )
avg=0
for i in range(len(comparable)):
avg += comparable[i][5]
@@ -84,7 +84,7 @@ def analyse():
nick=get_nick(),
code=D.quiz.get_link(aid),
questions = rows2dicts( comparable, ['q','n','p','a1','a2','c'] ),
- users = rows2dicts( D.get_users_answered(aid), ['id','nick'] ),
+ users = rows2dicts( D.quiz.users(aid), ['id','nick'] ),
user1=int(uid1),
user2=int(uid2),
avg = avg,
diff --git a/routes/answer.py b/routes/answer.py
index f3a506c..2307a83 100644
--- a/routes/answer.py
+++ b/routes/answer.py
@@ -63,7 +63,7 @@ def answer():
nick = get_nick()
)
- if D.is_user_answered(aid, sid):
+ if D.quiz.user(aid, sid):
return render_template(
"answer.html",
caller = "answer",
@@ -76,7 +76,7 @@ def answer():
caller = "answer",
alert = get_alert(),
nick = get_nick(),
- questions = rows2dicts( D.get_questions(aid), ['i','q','n','p'] ),
+ questions = rows2dicts( D.quiz.questions(aid), ['i','q','n','p'] ),
link = D.quiz.get_link( aid )
)
@@ -99,7 +99,7 @@ def set_answers():
if int(answer) < 0 or int(answer) > 999:
session["alert"]="Luvattoman pieniä tai suuria lukuja!"
return redirect( "/#answer" )
- elif D.get_user_answer(int(sid), int(question)) != -1:
+ elif D.answer.get(int(sid), int(question)) != -1:
session["alert"]="Kyselyyn olikin jo saatu vastauksia."
return redirect( "/#answer" )
except ValueError:
@@ -109,6 +109,6 @@ def set_answers():
for question, answer in request.form.items():
if question=="csrf":
continue
- D.answer_new(int(sid), int(question), int(answer))
+ D.answer.new(int(sid), int(question), int(answer))
return redirect("/#analyse")
diff --git a/routes/create.py b/routes/create.py
index 7322079..d2701a2 100644
--- a/routes/create.py
+++ b/routes/create.py
@@ -33,7 +33,7 @@ def create():
nick=get_nick(),
quiz_set=True,
questions=rows2dicts(
- D.get_questions(session["quiz_id"]),
+ D.quiz.questions(session["quiz_id"]),
['i','q','n','p','a']
)
)
@@ -57,7 +57,7 @@ def quiz_ready():
if "quiz_id" not in session.keys():
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"]):
+ if not D.quiz.user(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"]
diff --git a/routes/question.py b/routes/question.py
index 9ddb527..6965529 100644
--- a/routes/question.py
+++ b/routes/question.py
@@ -45,8 +45,8 @@ def new_question():
session["alert"] = "Vastauksessasi on nyt jotain häikkää."
return redirect("/#question")
- question_id = D.question_new( question, neg_ans, pos_ans )
+ question_id = D.question.new( question, neg_ans, pos_ans )
D.quiz.add(qid, question_id)
- D.answer_new(sid, question_id, answer)
+ D.answer.new(sid, question_id, answer)
return redirect("/#create")