Setting cookies to remember users works now, but not yet with sessions.

This commit is contained in:
Jan 2024-03-15 16:44:25 +01:00
parent 9d54ae67db
commit 955726bae0

View File

@ -1,17 +1,20 @@
import sys
from flask import Flask, redirect, url_for, request
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 datetime import datetime
import uuid
#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"
# create the model for the response table
class Response(db.Model):
@ -31,6 +34,18 @@ with app.app_context():
@app.route("/", methods=["GET", "POST"])
def testpage():
user_cookie = request.cookies.get('sleaformcookie')
if user_cookie:
# If the cookie exists
return f'Hello, returning user! Your cookie value is {user_cookie}'
else:
# If the cookie doesn't exist
response = make_response('Hello, new user! Setting cookie...')
print('Hello, new user! Setting cookie...')
user_id = str(uuid.uuid4())
print('Setting cookie with your user id: {user_id}'.format(user_id=user_id))
response.set_cookie('sleaformcookie', user_id)
return response
if request.method == "POST":
likert_score = request.form["likertscale"]
text_input = request.form["feedback"]