From 98d8199ffd974310274a9d072362effdbf009904 Mon Sep 17 00:00:00 2001 From: Showdown76py Date: Thu, 19 Nov 2026 01:11:05 +0100 Subject: [PATCH] Results share link: read-only /r page with a revocable key, the admin ticks which closed questions it shows (sharing locks reopen like publish), refreshes every 15 s --- rotatyvote/app.py | 37 +++++++++++++++++++++++++++++++++++-- rotatyvote/db.py | 8 ++++++-- static/admin.html | 8 ++++++++ static/css/admin.css | 6 ++++++ static/js/admin.js | 43 +++++++++++++++++++++++++++++++++++++++++++ static/js/results.js | 27 +++++++++++++++++++++++++++ static/results.html | 21 +++++++++++++++++++++ 7 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 static/js/results.js create mode 100644 static/results.html diff --git a/rotatyvote/app.py b/rotatyvote/app.py index 8bc0d0f..6b4ad09 100644 --- a/rotatyvote/app.py +++ b/rotatyvote/app.py @@ -1,5 +1,6 @@ import asyncio import contextlib +import hmac import json import logging from dataclasses import dataclass, field @@ -92,7 +93,8 @@ def q_public(q, with_results: bool): d = { "id": q["id"], "text": q["text"], "kind": q["kind"], "options": json.loads(q["options"]), "max_choices": q["max_choices"], "allow_blank": bool(q["allow_blank"]), "state": q["state"], - "results_public": bool(q["results_public"]), "shown": bool(q["shown"]), "participants": db.participants(q["id"]), + "results_public": bool(q["results_public"]), "shown": bool(q["shown"]), "shared": bool(q["shared"]), + "participants": db.participants(q["id"]), # how many were added by hand, never how they split while the vote is open "extra_ballots": db.extra(q)["ballots"], } @@ -112,7 +114,7 @@ def admin_state(pid): "max_voters": p["max_voters"], "expected": p["expected"], "require_code": bool(p["require_code"]), "focus_qid": p["focus_qid"], "vote_scans": p["vote_scans"], "code_style": p["code_style"], "entry_big": bool(p["entry_big"]), - "camera_pass": bool(p["camera_pass"]), + "camera_pass": bool(p["camera_pass"]), "share_key": p["share_key"], }, "voters": db.voter_count(pid), "codes": codes_summary(pid), @@ -553,6 +555,27 @@ async def admin_rotate_screen_key(request: Request): return JSONResponse({"screen_key": key}) +async def admin_share_key(request: Request): + pid = request.path_params["pid"] + require_admin(request, pid) + # POST makes a new link (the old one dies), DELETE turns the link off + key = new_key() if request.method == "POST" else None + db.update_poll(pid, share_key=key) + hub.refresh(pid) + return JSONResponse({"share_key": key}) + + +async def shared_results(request: Request): + pid = request.path_params["pid"] + p = get_poll(pid) + auth = request.headers.get("authorization", "") + key = auth[7:] if auth.lower().startswith("bearer ") else "" + if not p["share_key"] or not hmac.compare_digest(key.encode(), p["share_key"].encode()): + return err("bad_share_key", 403) + qs = [q_public(q, True) for q in db.questions(pid) if q["state"] == "closed" and q["shared"]] + return JSONResponse({"title": p["title"], "questions": qs}) + + async def admin_add_question(request: Request): pid = request.path_params["pid"] require_admin(request, pid) @@ -599,6 +622,13 @@ async def admin_question_action(request: Request): db.set_question(qid, shown=1) if action == "publish": db.update_poll(pid, focus_qid=qid) + elif action in ("share", "unshare"): + if st != "closed": + return err("not_closed") + db.set_question(qid, shared=int(action == "share")) + # people outside the room have seen the tally, same lock as publish + if action == "share": + db.set_question(qid, shown=1) elif action == "extra": if st == "draft": return err("not_open") @@ -1031,6 +1061,7 @@ routes = [ Route("/present/{pid}", page("screen.html")), Route("/essai", page("essai.html")), Route("/v/{pid}", page("vote.html")), + Route("/r/{pid}", page("results.html")), Route("/V/{pid}/{tok}", camera_pass), Route("/V/{pid}", lambda r: RedirectResponse(f"/v/{r.path_params['pid'].lower()}", status_code=301)), Route("/api/config", public_config, methods=["GET"]), @@ -1039,10 +1070,12 @@ routes = [ Route("/api/polls/{pid}", admin_get, methods=["GET"]), Route("/api/polls/{pid}", admin_patch, methods=["PATCH"]), Route("/api/polls/{pid}/screen-key", admin_rotate_screen_key, methods=["POST"]), + Route("/api/polls/{pid}/share-key", admin_share_key, methods=["POST", "DELETE"]), Route("/api/polls/{pid}/questions", admin_add_question, methods=["POST"]), Route("/api/polls/{pid}/questions/{qid:int}/{action}", admin_question_action, methods=["POST"]), Route("/api/polls/{pid}/codes", admin_codes, methods=["GET", "POST"]), Route("/api/polls/{pid}/export", admin_export, methods=["GET"]), + Route("/api/r/{pid}", shared_results, methods=["GET"]), Route("/api/v/{pid}/me", voter_me, methods=["GET"]), Route("/api/v/{pid}/scan", voter_scan, methods=["POST"]), Route("/api/v/{pid}/code", voter_code, methods=["POST"]), diff --git a/rotatyvote/db.py b/rotatyvote/db.py index 61a8bfd..0ff7afa 100644 --- a/rotatyvote/db.py +++ b/rotatyvote/db.py @@ -92,6 +92,9 @@ class DB: ("polls", "entry_big", "INTEGER NOT NULL DEFAULT 0"), ("questions", "extra", "TEXT"), ("polls", "camera_pass", "INTEGER NOT NULL DEFAULT 1"), + # read-only results link. kept in clear so the admin can copy it again, it only shows tallies + ("polls", "share_key", "TEXT"), + ("questions", "shared", "INTEGER NOT NULL DEFAULT 0"), ): cols = {r[1] for r in self.c.execute(f"PRAGMA table_info({table})")} if col not in cols: @@ -148,7 +151,8 @@ class DB: def update_poll(self, pid, **fields): allowed = {"title", "interval_ms", "scans_required", "enrollment_open", "max_voters", - "expected", "require_code", "focus_qid", "vote_scans", "code_style", "entry_big", "camera_pass"} + "expected", "require_code", "focus_qid", "vote_scans", "code_style", "entry_big", "camera_pass", + "share_key"} fields = {k: v for k, v in fields.items() if k in allowed} if not fields: return @@ -226,7 +230,7 @@ class DB: return self.all("SELECT * FROM questions WHERE poll_id=? ORDER BY position", pid) def set_question(self, qid, **fields): - allowed = {"state", "results_public", "shown", "extra"} + allowed = {"state", "results_public", "shown", "extra", "shared"} fields = {k: v for k, v in fields.items() if k in allowed} sets = ",".join(f"{k}=?" for k in fields) self.c.execute(f"UPDATE questions SET {sets} WHERE id=?", (*fields.values(), qid)) diff --git a/static/admin.html b/static/admin.html index 98c808a..5c380e3 100644 --- a/static/admin.html +++ b/static/admin.html @@ -133,8 +133,16 @@ +
+ +
+
+