diff --git a/routes.py b/routes.py index cf85796..4bccc8c 100644 --- a/routes.py +++ b/routes.py @@ -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 diff --git a/templates/login.html b/templates/login.html index d975d46..b32d6c1 100644 --- a/templates/login.html +++ b/templates/login.html @@ -7,7 +7,7 @@

Willkommen

Bitte gib deinen Namen ein, um den Test zu starten.

-
+