refactor: better session check
This commit is contained in:
1 parent
819b9a13dd
commit
32cd1922f5
2 files changed
+45
-32
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:
|
||||
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")
|
||||
def datenschutz():
|
||||
return render_template("datenschutz.html")
|
||||
@@ -35,45 +56,41 @@ def agree_dse():
|
||||
return redirect(url_for("main.index"))
|
||||
|
||||
@bp.route("/login")
|
||||
@session_required(participant=False)
|
||||
def login():
|
||||
if "name" in session:
|
||||
if "pid" in session:
|
||||
return redirect(url_for("main.index"))
|
||||
elif "dse_agreed" not in session:
|
||||
return redirect(url_for("main.datenschutz"))
|
||||
return render_template("login.html")
|
||||
|
||||
|
||||
@bp.route("/", methods=["GET", "POST"])
|
||||
def index():
|
||||
if request.method == "POST":
|
||||
name = request.form.get("name", "").strip()[:60]
|
||||
if not name:
|
||||
return redirect(url_for("main.login"))
|
||||
session.clear() # neuer Teilnehmer = frischer Stand
|
||||
session["name"] = name
|
||||
session["pid"] = create_participant(name)
|
||||
#return redirect(url_for("main.test"))
|
||||
if "pid" not in session:
|
||||
@bp.route("/start", methods=["POST"])
|
||||
@session_required(participant=False)
|
||||
def start():
|
||||
name = request.form.get("name", "").strip()[:60]
|
||||
if not name:
|
||||
return redirect(url_for("main.login"))
|
||||
dse_at = session.get("dse_agreed_at")
|
||||
session.clear()
|
||||
session["dse_agreed"] = True
|
||||
session["dse_agreed_at"] = dse_at
|
||||
session["name"] = name
|
||||
session["pid"] = create_participant(name)
|
||||
return redirect(url_for("main.index"))
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
@session_required()
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
|
||||
@bp.route("/test")
|
||||
@session_required()
|
||||
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"])
|
||||
|
||||
@bp.route("/api/getRandomSong")
|
||||
@session_required(api=True)
|
||||
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", [])
|
||||
#print(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"])
|
||||
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"))
|
||||
|
||||
@session_required(api=True)
|
||||
def api_rate():
|
||||
data = request.get_json(silent=True) or {}
|
||||
choice = data.get("choice")
|
||||
if choice == "gleich": # beide Schreibweisen akzeptieren
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<h1>Willkommen</h1>
|
||||
<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>
|
||||
<input id="name" name="name" type="text" autocomplete="name" required autofocus>
|
||||
<button type="submit">Starten</button>
|
||||
|
||||
Reference in new issue
Block a user