Viel aufgeräumter und es funktioniert scheinbar
This commit is contained in:
parent
6a0d9cfd69
commit
4527b62444
@ -1,3 +1,47 @@
|
||||
import sys
|
||||
import json
|
||||
import random
|
||||
from flask import Flask, redirect, url_for, request, session, make_response
|
||||
from flask import render_template
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy import Integer, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
random_order = True
|
||||
|
||||
#create the app
|
||||
app = Flask(__name__)
|
||||
# configure the database, give it a path (it will be in the instances folder)
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
|
||||
db = SQLAlchemy(app)
|
||||
#set the secret key (TODO change this for final deployment)
|
||||
app.secret_key = b"29fe9e8edd407c5491d4f1c05632d9fa33e26ed8734a3f5e080ebac3772a555a"
|
||||
|
||||
#open the json file with the config
|
||||
configfile = open("singleformconfig.json")
|
||||
configfile2 = open("pairwiseformconfig.json")
|
||||
#convert it to dict
|
||||
config = json.load(configfile)
|
||||
config2 = json.load(configfile2)
|
||||
configfile.close()
|
||||
configfile2.close()
|
||||
# get the questions:
|
||||
questions = list(config)
|
||||
|
||||
# create the model for the response table
|
||||
class Response(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
session_user_id = db.Column(db.String(36))
|
||||
likert_result = db.Column(db.Integer, nullable=False)
|
||||
notes = db.Column(db.String(200))
|
||||
date_created = db.Column(db.DateTime, default=datetime.today())
|
||||
def __repr__(self) -> str:
|
||||
return "<Response %r>" % self.id
|
||||
|
||||
#test
|
||||
@app.route("/test", methods=["GET", "POST"])
|
||||
def testpage():
|
||||
|
@ -45,9 +45,9 @@ class Response(db.Model):
|
||||
|
||||
|
||||
class User(db.Model):
|
||||
#user_id = db.Column(UUID(as_uuid=True), primary_key=True)
|
||||
#device_id = db.Column(UUID(as_uuid=True), primary_key=True)
|
||||
#question_order = db.Column(db.String(60))
|
||||
user_id = db.Column(db.UUID(as_uuid=True), primary_key=True, nullable=False)
|
||||
device_id = db.Column(UUID(as_uuid=True), primary_key=True)
|
||||
question_order = db.Column(db.String(60))
|
||||
date_created = db.Column(db.DateTime, default=datetime.today())
|
||||
def __repr__(self) -> str:
|
||||
return "<User %r>" % self.user_id
|
||||
@ -81,11 +81,14 @@ def formpage():
|
||||
def startpage():
|
||||
if not "slaeform_device_id" in session:
|
||||
# If this device was not seen, remember it.
|
||||
new_device_id = str(uuid.uuid4())
|
||||
new_device_id = uuid.uuid4()
|
||||
session["slaeform_device_id"] = new_device_id
|
||||
session["agreed_to_tos"] = False
|
||||
|
||||
if request.method == "POST":
|
||||
#right now if a user that has an active session goes to the startpage again and accepts tos
|
||||
#it will just start another session and discard the previous session
|
||||
|
||||
# get a random question order
|
||||
if random_order:
|
||||
order = random.shuffle(questions)
|
||||
@ -93,24 +96,23 @@ def startpage():
|
||||
order = questions
|
||||
|
||||
|
||||
#if the user accepts, save the new user to the database
|
||||
new_user_id = str(uuid.uuid4())
|
||||
#save the new user to the database and the session
|
||||
session["agreed_to_tos"] = True
|
||||
new_user_id = uuid.uuid4()
|
||||
session["slaeform_user_id"] = new_user_id
|
||||
session_user_id = new_user_id
|
||||
|
||||
device_id = session["slaeform_device_id"]
|
||||
user_id = session["slaeform_user_id"]
|
||||
question_order = str(order)
|
||||
new_user = User(user_id=user_id,question_order=question_order)
|
||||
new_user = User(user_id=user_id, device_id=device_id, question_order=question_order)
|
||||
|
||||
try:
|
||||
db.session.add(new_user)
|
||||
print("add was ok")
|
||||
db.session.commit()
|
||||
return redirect("/start") #url_for("datapage")
|
||||
print("commit was ok")
|
||||
return redirect("/form")
|
||||
except:
|
||||
return "There was a problem while adding the user to the Database"
|
||||
|
||||
session["agreed_to_tos"] = True
|
||||
return redirect("/form")
|
||||
return "There was a problem while adding the user to the Database"
|
||||
|
||||
return render_template(
|
||||
"startpage.html"
|
||||
@ -120,10 +122,6 @@ def startpage():
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@app.route("/data")
|
||||
def datapage():
|
||||
responses = Response.query.order_by(Response.date_created).all()
|
||||
|
Loading…
Reference in New Issue
Block a user