summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md14
-rw-r--r--SCHEMA.sql21
-rw-r--r--app.py4
-rw-r--r--db_actions.py47
-rw-r--r--question.py38
-rw-r--r--quiz.py13
-rw-r--r--routes.py41
-rw-r--r--static/index.html15
-rw-r--r--static/pages.json10
-rw-r--r--templates/analyse.html2
-rw-r--r--templates/answer.html1
-rw-r--r--templates/create.html3
-rw-r--r--templates/question.html9
-rw-r--r--templates/quiz.html4
14 files changed, 211 insertions, 11 deletions
diff --git a/README.md b/README.md
index 65f957e..ef15820 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,12 @@
Kyselmä - kysele, vastaile ja tutki tuloksia
DONE:
+- Voi lisätä kyselyn
+- Kysymyksen mukana lisätään vastaus
+- Kysymyksen lisäys
+- Vastaustaulu
+- Kysymystaulu
+- Kyselytaulu
- Tietokantayhteys
- Nimierkkitaulu
- Nimimerkin käyttöönotto ja tarkistus
@@ -9,11 +15,13 @@ DONE:
- Nettisivurunko
TODO:
-- Kysymystaulu
-- Kyselytaulu
-- Kysymyksen lisäys
+- Kyselyn luomisen näyttö
+- Kyselyn lukitseminen vastattavaksi
- Kysymyksen näyttö
- Ulkoasu
+- Moderointi
+- Vastausten anto
+- Vastausten tarkistelu
...
Tarkoitus on luoda sivu jossa voi luoda kysymyksiä ja kyselyitä, joita
diff --git a/SCHEMA.sql b/SCHEMA.sql
index 1c5c372..6832923 100644
--- a/SCHEMA.sql
+++ b/SCHEMA.sql
@@ -3,3 +3,24 @@ CREATE TABLE users (
nick TEXT,
created INT
);
+CREATE TABLE questions (
+ id SERIAL PRIMARY KEY,
+ question TEXT,
+ neg_answer TEXT,
+ pos_answer TEXT,
+ created INT
+);
+CREATE TABLE answers (
+ id SERIAL PRIMARY KEY,
+ user_id INT,
+ question_id INT,
+ answer INT,
+ created INT
+);
+CREATE TABLE questionaires (
+ id SERIAL PRIMARY KEY,
+ questionset INT[] DEFAULT '{}',
+ creator_id INT,
+ ready BOOLEAN DEFAULT FALSE,
+ created INT
+);
diff --git a/app.py b/app.py
index 3e99f07..b5f381c 100644
--- a/app.py
+++ b/app.py
@@ -8,4 +8,6 @@ app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres@localhost/postgre
db.init_app(app)
import routes
-import nick \ No newline at end of file
+import nick
+import quiz
+import question
diff --git a/db_actions.py b/db_actions.py
index dc59d00..26f69df 100644
--- a/db_actions.py
+++ b/db_actions.py
@@ -28,3 +28,50 @@ def user_exists(nick):
FROM users \
WHERE nick=(:nick);"
return db.session.execute(text(sql), { "nick":nick }).scalar()
+
+def question_new( question, neg_ans, pos_ans ):
+ sql = "INSERT \
+ INTO questions (question, neg_answer, pos_answer, created) \
+ VALUES (:question, :neg_answer, :pos_answer, :created) \
+ RETURNING id ;"
+ result = db.session.execute(
+ text(sql), {
+ "question":question,
+ "neg_answer":neg_ans,
+ "pos_answer":pos_ans,
+ "created":int(time()) }
+ )
+ db.session.commit()
+ return result.fetchone()[0]
+
+def quiz_new(user_id):
+ sql = "INSERT \
+ INTO questionaires (creator_id, created) \
+ VALUES (:creator_id, :created) \
+ RETURNING id ;"
+ result = db.session.execute( text(sql),
+ { "creator_id":user_id, "created":int(time()) } )
+ db.session.commit()
+ return result.fetchone()[0]
+
+def quiz_add( quiz_id, question_id ):
+ sql = "UPDATE questionaires \
+ SET questionset = ARRAY_APPEND(questionset, :question_id) \
+ WHERE id=:quiz_id;"
+ db.session.execute(text(sql), {
+ "quiz_id":quiz_id,
+ "question_id":question_id
+ })
+ db.session.commit()
+
+def answer_new(user_id, question_id, answer):
+ sql = "INSERT \
+ INTO answers (user_id, question_id, answer, created) \
+ VALUES (:user_id, :question_id, :answer, :created);"
+ db.session.execute( text(sql), {
+ "user_id":user_id,
+ "question_id":question_id,
+ "answer":answer,
+ "created":int(time())
+ } )
+ db.session.commit()
diff --git a/question.py b/question.py
new file mode 100644
index 0000000..e7494bc
--- /dev/null
+++ b/question.py
@@ -0,0 +1,38 @@
+from app import app
+from flask import render_template, session, request, redirect
+import db_actions as D
+
+
+def validate_answer(ans):
+ return True
+
+def validate_question(question):
+ 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 virheellinen"
+ elif not validate_answer(pos_ans):
+ msg = "Oikea selite 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
new file mode 100644
index 0000000..a136209
--- /dev/null
+++ b/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("/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
index b38cf3c..7e7f2ca 100644
--- a/routes.py
+++ b/routes.py
@@ -1,5 +1,6 @@
from app import app
from flask import render_template,session
+import db_actions as D
def get_alert():
if "alert" in session:
@@ -7,6 +8,11 @@ def get_alert():
del session["alert"]
return f"{alert}"
return ""
+
+def get_nick():
+ if "id" in session.keys():
+ return D.user_get_nick(session["id"])
+ return "(ei nimimerkkiä)"
@app.route("/")
def index():
@@ -14,30 +20,53 @@ def index():
@app.route("/pages/info.html")
def info():
- return render_template("info.html", alert=get_alert() )
+ return render_template("info.html",
+ alert=get_alert()
+ )
@app.route("/pages/create.html")
def create():
- if "id" not in session:
+ if "id" not in session.keys():
return "redirect = #nick"
- return render_template("create.html", alert=get_alert() )
+ 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:
return "redirect = #nick"
- return render_template("answer.html", alert=get_alert() )
+ return render_template("answer.html",
+ alert=get_alert(),
+ nick=get_nick()
+ )
@app.route("/pages/analyse.html")
def analyse():
if "id" not in session:
return "redirect = #nick"
- return render_template("analyse.html", alert=get_alert() )
+ 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() )
+ 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/static/index.html b/static/index.html
index c651142..d726587 100644
--- a/static/index.html
+++ b/static/index.html
@@ -10,5 +10,18 @@
<link rel="icon" type="image/svg+xml" href="kyselma.svg">
<script src="menu.js"></script>
</head>
-<body></body>
+<body>
+<noscript>
+<H1>Kyselmä</H1>
+<a href="pages/info.html">Pääsivu</a><br>
+<a href="pages/create.html">Luo kysely</a><br>
+<a href="pages/answer.html">Vastaa kyselyyn</a><br>
+<a href="pages/analyse.html">Tutki vastauksia</a><br>
+<a href="pages/moderate.html">Moderoi sivustoa</a><br>
+<a href="pages/nick.html">Anna nimimerkki</a><br>
+<hr>
+Tämä sivu tarvitsee JavaScriptin toimiakseen. Voit kumminkin yrittää käyttää
+sivua ylläolevien linkkien kautta.
+</noscript>
+</body>
</html> \ No newline at end of file
diff --git a/static/pages.json b/static/pages.json
index 4a18ca9..618bc7a 100644
--- a/static/pages.json
+++ b/static/pages.json
@@ -32,6 +32,16 @@
"pageTitle": "Anna nimimerkki",
"URL": "pages/nick.html",
"hidden": "yes"
+ },
+ "question": {
+ "pageTitle": "Lisää kysymys",
+ "URL": "pages/question.html",
+ "hidden": "yes"
+ },
+ "quiz": {
+ "pageTitle": "Rakenna kysely",
+ "URL": "pages/quiz.html",
+ "hidden": "yes"
}
}
}
diff --git a/templates/analyse.html b/templates/analyse.html
index 3220412..6970764 100644
--- a/templates/analyse.html
+++ b/templates/analyse.html
@@ -1,2 +1,4 @@
+<div id="nickbox">{{ nick }}</div>
<div id="alertbox">{{ alert }}</div>
<h1>analyse</h1>
+
diff --git a/templates/answer.html b/templates/answer.html
index 808dde8..3870e6b 100644
--- a/templates/answer.html
+++ b/templates/answer.html
@@ -1,2 +1,3 @@
+<div id="nickbox">{{ nick }}</div>
<div id="alertbox">{{ alert }}</div>
<h1>answer</h1>
diff --git a/templates/create.html b/templates/create.html
index 15af509..0ba8164 100644
--- a/templates/create.html
+++ b/templates/create.html
@@ -1,2 +1,5 @@
+<div id="nickbox">{{ nick }}</div>
<div id="alertbox">{{ alert }}</div>
<h1>create</h1>
+<a href="#question">Lisää kysymys</a>
+</form>
diff --git a/templates/question.html b/templates/question.html
new file mode 100644
index 0000000..90d3abc
--- /dev/null
+++ b/templates/question.html
@@ -0,0 +1,9 @@
+<div id="alertbox">{{ alert }}</div>
+<form action="/new_question" method="POST">
+Kysymys:<input type="text" name="question" rows="1" cols="120"><br>
+<input type="text" name="neg_ans" rows="1" cols="40"> : valinnat :
+<input type="text" name="pos_ans" rows="1" cols="40"><br>
+<input type="range" min="-1000" max="1000" value="0"
+ class="slider" name="answer" class="answerSlider"><br>
+<input type="submit" value="Lähetä">
+</form>
diff --git a/templates/quiz.html b/templates/quiz.html
new file mode 100644
index 0000000..4c1871d
--- /dev/null
+++ b/templates/quiz.html
@@ -0,0 +1,4 @@
+<div id="alertbox">{{ alert }}</div>
+<form action="/new_quiz" method="POST">
+<input type="submit" value="Aloita uusi kyselmä">
+</form>