refactor: better session check
This commit is contained in:
1 parent
819b9a13dd
commit
32cd1922f5
2 files changed
+40
-27
No files matched your search
@@ -24,6 +24,27 @@ def load_songs_list():
|
|||||||
with open(BASE_DIR / "data" / "songs.json", encoding="utf-8") as f:
|
with open(BASE_DIR / "data" / "songs.json", encoding="utf-8") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
|
def session_required(api=False, participant=True):
|
||||||
|
"""Prüft Datenschutz-Zustimmung und (optional) angelegten Teilnehmer.
|
||||||
|
|
||||||
|
api=True -> JSON-Fehler statt Redirect
|
||||||
|
participant=False -> nur Datenschutz prüfen (z. B. für die Namenseingabe)
|
||||||
|
"""
|
||||||
|
def decorator(view):
|
||||||
|
@wraps(view)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
if "dse_agreed" not in session:
|
||||||
|
if api:
|
||||||
|
return jsonify({"error": "privacy policy not accepted"}), 403
|
||||||
|
return redirect(url_for("main.datenschutz"))
|
||||||
|
if participant and "pid" not in session:
|
||||||
|
if api:
|
||||||
|
return jsonify({"error": "no session detected"}), 401
|
||||||
|
return redirect(url_for("main.login"))
|
||||||
|
return view(*args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
return decorator
|
||||||
|
|
||||||
@bp.route("/datenschutz")
|
@bp.route("/datenschutz")
|
||||||
def datenschutz():
|
def datenschutz():
|
||||||
return render_template("datenschutz.html")
|
return render_template("datenschutz.html")
|
||||||
@@ -35,45 +56,41 @@ def agree_dse():
|
|||||||
return redirect(url_for("main.index"))
|
return redirect(url_for("main.index"))
|
||||||
|
|
||||||
@bp.route("/login")
|
@bp.route("/login")
|
||||||
|
@session_required(participant=False)
|
||||||
def login():
|
def login():
|
||||||
if "name" in session:
|
if "pid" in session:
|
||||||
return redirect(url_for("main.index"))
|
return redirect(url_for("main.index"))
|
||||||
elif "dse_agreed" not in session:
|
|
||||||
return redirect(url_for("main.datenschutz"))
|
|
||||||
return render_template("login.html")
|
return render_template("login.html")
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/", methods=["GET", "POST"])
|
@bp.route("/start", methods=["POST"])
|
||||||
def index():
|
@session_required(participant=False)
|
||||||
if request.method == "POST":
|
def start():
|
||||||
name = request.form.get("name", "").strip()[:60]
|
name = request.form.get("name", "").strip()[:60]
|
||||||
if not name:
|
if not name:
|
||||||
return redirect(url_for("main.login"))
|
return redirect(url_for("main.login"))
|
||||||
session.clear() # neuer Teilnehmer = frischer Stand
|
dse_at = session.get("dse_agreed_at")
|
||||||
|
session.clear()
|
||||||
|
session["dse_agreed"] = True
|
||||||
|
session["dse_agreed_at"] = dse_at
|
||||||
session["name"] = name
|
session["name"] = name
|
||||||
session["pid"] = create_participant(name)
|
session["pid"] = create_participant(name)
|
||||||
#return redirect(url_for("main.test"))
|
return redirect(url_for("main.index"))
|
||||||
if "pid" not in session:
|
|
||||||
return redirect(url_for("main.login"))
|
|
||||||
|
@bp.route("/")
|
||||||
|
@session_required()
|
||||||
|
def index():
|
||||||
return render_template("index.html")
|
return render_template("index.html")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/test")
|
@bp.route("/test")
|
||||||
|
@session_required()
|
||||||
def test():
|
def test():
|
||||||
if "name" not in session:
|
|
||||||
return redirect(url_for("main.index"))
|
|
||||||
elif "dse_agreed" not in session:
|
|
||||||
return redirect(url_for("main.datenschutz"))
|
|
||||||
return render_template("test.html", name=session["name"])
|
return render_template("test.html", name=session["name"])
|
||||||
|
|
||||||
@bp.route("/api/getRandomSong")
|
@bp.route("/api/getRandomSong")
|
||||||
|
@session_required(api=True)
|
||||||
def api_getRandomSong():
|
def api_getRandomSong():
|
||||||
if "name" not in session:
|
|
||||||
return jsonify({"error": "no session detected"}), 401
|
|
||||||
elif "dse_agreed" not in session:
|
|
||||||
return jsonify({"error": "no agreement on privacy policy detected"}), 401
|
|
||||||
|
|
||||||
done = session.get("done", [])
|
done = session.get("done", [])
|
||||||
#print(done)
|
#print(done)
|
||||||
remaining = [s for s in load_songs_list() if s["id"] not in done]
|
remaining = [s for s in load_songs_list() if s["id"] not in done]
|
||||||
@@ -104,12 +121,8 @@ def api_getRandomSong():
|
|||||||
})
|
})
|
||||||
|
|
||||||
@bp.route("/api/rate", methods=["POST"])
|
@bp.route("/api/rate", methods=["POST"])
|
||||||
|
@session_required(api=True)
|
||||||
def api_rate():
|
def api_rate():
|
||||||
if "pid" not in session or "current" not in session:
|
|
||||||
return jsonify({"error": "no session detected"}), 401
|
|
||||||
elif "dse_agreed" not in session:
|
|
||||||
return redirect(url_for("main.datenschutz"))
|
|
||||||
|
|
||||||
data = request.get_json(silent=True) or {}
|
data = request.get_json(silent=True) or {}
|
||||||
choice = data.get("choice")
|
choice = data.get("choice")
|
||||||
if choice == "gleich": # beide Schreibweisen akzeptieren
|
if choice == "gleich": # beide Schreibweisen akzeptieren
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
<h1>Willkommen</h1>
|
<h1>Willkommen</h1>
|
||||||
<p class="login-hint">Bitte gib deinen Namen ein, um den Test zu starten.</p>
|
<p class="login-hint">Bitte gib deinen Namen ein, um den Test zu starten.</p>
|
||||||
|
|
||||||
<form method="post" action="{{ url_for('main.index') }}">
|
<form method="post" action="{{ url_for('main.start') }}">
|
||||||
<label for="name">Name</label>
|
<label for="name">Name</label>
|
||||||
<input id="name" name="name" type="text" autocomplete="name" required autofocus>
|
<input id="name" name="name" type="text" autocomplete="name" required autofocus>
|
||||||
<button type="submit">Starten</button>
|
<button type="submit">Starten</button>
|
||||||
|
|||||||
Reference in new issue
Block a user