From 4c5fd0076b4903320ba470ebf0cd454e5f7a2995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristina=20H=C3=A4nninen?= Date: Tue, 21 Nov 2023 12:52:55 +0200 Subject: Initial anwsering form and related --- app.py | 1 + db_actions.py | 11 +++++++++ routes/set/answers.py | 32 ++++++++++++++++++++++++ static/answer.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ static/create.js | 1 - templates/answer.html | 4 ++- 6 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 routes/set/answers.py create mode 100644 static/answer.js diff --git a/app.py b/app.py index 996be9b..b01f013 100644 --- a/app.py +++ b/app.py @@ -11,4 +11,5 @@ import routes.base import routes.set.nick import routes.set.quiz import routes.set.question +import routes.set.answers import routes.get.quiz diff --git a/db_actions.py b/db_actions.py index b604870..5a76b00 100644 --- a/db_actions.py +++ b/db_actions.py @@ -85,4 +85,15 @@ def get_questions(quiz_id): WHERE a.question_id = q.id AND quiz.id = (:quiz_id);" return db.session.execute( text(sql), { "quiz_id":quiz_id } ).fetchall() + +def get_user_answer(user_id, question_id): + sql = "SELECT answer \ + FROM answers \ + WHERE question_id = (:question_id) AND user_id = (:user_id);" + result = db.session.execute( text(sql), { + 'question_id': question_id, + 'user_id': user_id + } ).fetchone() + return result[0] if result else result + \ No newline at end of file diff --git a/routes/set/answers.py b/routes/set/answers.py new file mode 100644 index 0000000..859d65c --- /dev/null +++ b/routes/set/answers.py @@ -0,0 +1,32 @@ +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/static/answer.js b/static/answer.js new file mode 100644 index 0000000..61882a8 --- /dev/null +++ b/static/answer.js @@ -0,0 +1,68 @@ +var questions = {} + +createQuestionDiv = ( question ) => { + const questionDiv = document.createElement('div') + questionDiv.className = 'kysQuestion' + + const qDiv = document.createElement('div') + qDiv.appendChild( document.createTextNode( question.q ) ) + qDiv.className = 'kysText' + questionDiv.appendChild( qDiv ) + + const npDiv = document.createElement('div') + npDiv.className = 'kysScale' + + const nDiv = document.createElement('div') + nDiv.appendChild( document.createTextNode( question.n ) ) + nDiv.className = 'kysNegative' + npDiv.appendChild( nDiv ) + + const sDiv = document.createElement('div') + sDiv.className = 'kysScaleSpacer' + npDiv.appendChild( sDiv ) + + const pDiv = document.createElement('div') + pDiv.appendChild( document.createTextNode( question.p ) ) + pDiv.className = 'kysPositive' + npDiv.appendChild( pDiv ) + + questionDiv.appendChild( npDiv ) + + const aInput = document.createElement('input') + aInput.className = 'kysAnswer' + aInput.type = 'range' + aInput.min = 0 + aInput.max = 999 + aInput.value = 500 + aInput.name = question.i + questionDiv.appendChild( aInput ) + + return questionDiv +} + +createQuestions = () => { + const kysForm = document.getElementById('questionForm') + const questionsDiv = document.createElement('div') + Object.keys(questions).forEach(k => { + questionsDiv.appendChild( createQuestionDiv( questions[k] ) ) + }) + kysForm.appendChild( questionsDiv ) + const submitInput = document.createElement('input') + submitInput.type='submit' + submitInput.value='Vastaa kyselyyn' + submitInput.className = 'kysSubmitAnswers' + kysForm.appendChild( submitInput ) +} + +loadQuestions = async() => { + await fetch( 'get/quiz_creator' ) + .then( response => response.json() ) + .then( json => questions = json ) + .catch( error => { + alert("dkd") + } ) + + createQuestions() +} + +loadQuestions() diff --git a/static/create.js b/static/create.js index d64073f..d34a2da 100644 --- a/static/create.js +++ b/static/create.js @@ -29,7 +29,6 @@ createQuestionDiv = ( question ) => { questionDiv.appendChild( npDiv ) const aDiv = document.createElement('input') - aDiv.appendChild( document.createTextNode( question.a ) ) aDiv.className = 'kysAnswer' aDiv.type = 'range' aDiv.min = 0 diff --git a/templates/answer.html b/templates/answer.html index 3870e6b..0ed0a05 100644 --- a/templates/answer.html +++ b/templates/answer.html @@ -1,3 +1,5 @@ +
{{ nick }}
{{ alert }}
-

answer

+

answer NOW!

+
-- cgit v1.2.3