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
This commit is contained in:
+35
-2
@@ -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"]),
|
||||
|
||||
+6
-2
@@ -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))
|
||||
|
||||
@@ -133,8 +133,16 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card share-list" id="share-card">
|
||||
<div class="share-row">
|
||||
<div><b>Lien des résultats</b><span class="muted" id="share-summary"></span></div>
|
||||
<div class="code-actions" id="share-actions"></div>
|
||||
</div>
|
||||
<div id="share-qs"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
||||
<section id="codes-sheet" class="card" hidden>
|
||||
<div class="no-print actions"><button id="codes-close" class="small" type="button">Fermer</button><button id="codes-do-print" class="small primary" type="button">Imprimer</button></div>
|
||||
<h2 id="codes-title"></h2>
|
||||
|
||||
@@ -79,3 +79,9 @@
|
||||
font: .82rem/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace; white-space: pre; tab-size: 4;
|
||||
}
|
||||
#ex-preview.empty { white-space: normal; color: var(--ink-3); font-family: var(--sans); text-align: center; padding: 32px 16px; }
|
||||
|
||||
#share-qs { border-top: 1.5px solid var(--bg); padding: 6px 0 10px; }
|
||||
#share-qs:empty { display: none; }
|
||||
.share-q { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; font-weight: 500; cursor: pointer; }
|
||||
.share-q input { margin-top: 3px; flex: none; }
|
||||
.share-none { margin: 8px 0; font-size: .85rem; }
|
||||
|
||||
@@ -65,6 +65,7 @@ function render() {
|
||||
|
||||
renderSettings(p);
|
||||
renderQuestions();
|
||||
renderShare(p);
|
||||
}
|
||||
|
||||
function renderSettings(p) {
|
||||
@@ -134,6 +135,7 @@ function questionCard(q) {
|
||||
|
||||
const badges = [h("span", { class: `badge ${q.state}` }, STATE_LABEL[q.state])];
|
||||
if (q.results_public) badges.push(h("span", { class: "badge public" }, "Publiés"));
|
||||
if (q.shared && state.poll.share_key) badges.push(h("span", { class: "badge public" }, "Partagés"));
|
||||
if (focused) badges.push(h("span", { class: "focus-mark" }, icon("screen", 14), "À l'écran"));
|
||||
if (q.kind === "multi") badges.push(h("span", { class: "badge" }, `${q.max_choices} choix max`));
|
||||
|
||||
@@ -306,6 +308,47 @@ $("#export").addEventListener("click", async () => {
|
||||
} catch (e) { toast(errMsg(e), "bad"); }
|
||||
});
|
||||
|
||||
// read-only results link, the admin ticks which closed questions it shows
|
||||
|
||||
const shareUrl = (k) => `${location.origin}/r/${pid}#${k}`;
|
||||
|
||||
function renderShare(p) {
|
||||
const btn = (label, onclick, cls = "small") => h("button", { class: cls, type: "button", onclick }, label);
|
||||
const rotate = async (msg) => {
|
||||
if (msg && !confirm(msg)) return;
|
||||
try { const r = await call("POST", "/share-key"); await copy(shareUrl(r.share_key), "Lien des résultats"); }
|
||||
catch (e) { toast(errMsg(e), "bad"); }
|
||||
};
|
||||
const off = async () => {
|
||||
if (!confirm("Désactiver le lien des résultats ?")) return;
|
||||
try { await call("DELETE", "/share-key"); } catch (e) { toast(errMsg(e), "bad"); }
|
||||
};
|
||||
const closed = state.questions.filter((q) => q.state === "closed").sort((a, b) => a.id - b.id);
|
||||
const n = closed.filter((q) => q.shared).length;
|
||||
$("#share-summary").textContent = p.share_key ? `${n} question${n > 1 ? "s" : ""} visible${n > 1 ? "s" : ""}` : "Lecture seule";
|
||||
$("#share-actions").replaceChildren(...(p.share_key
|
||||
? [btn("Copier", () => copy(shareUrl(p.share_key), "Lien des résultats")),
|
||||
btn("Nouveau lien", () => rotate("Créer un nouveau lien ? L'ancien cessera de fonctionner.")),
|
||||
btn("Désactiver", off, "small danger")]
|
||||
: [btn("Créer", () => rotate())]));
|
||||
if (!p.share_key) return $("#share-qs").replaceChildren();
|
||||
$("#share-qs").replaceChildren(...(closed.length
|
||||
? closed.map((q) => h("label", { class: "share-q" },
|
||||
h("input", {
|
||||
type: "checkbox", checked: q.shared,
|
||||
onchange: async (e) => {
|
||||
const on = e.target.checked;
|
||||
if (on && !q.shown && !confirm("Partager ces résultats ? Le vote ne pourra plus être rouvert.")) {
|
||||
e.target.checked = false;
|
||||
return;
|
||||
}
|
||||
try { await call("POST", `/questions/${q.id}/${on ? "share" : "unshare"}`); }
|
||||
catch (err) { e.target.checked = !on; toast(errMsg(err), "bad"); }
|
||||
},
|
||||
}), h("span", {}, q.text)))
|
||||
: [h("p", { class: "muted share-none" }, "Aucune question close.")]));
|
||||
}
|
||||
|
||||
// text export, built in the browser from the closed tallies the socket already sends
|
||||
|
||||
const exDlg = $("#export-dlg");
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { $, h, api, pollId, hashKey, resultsView } from "./common.js";
|
||||
|
||||
const pid = pollId();
|
||||
// the key stays in the address bar so the link can be passed on as is
|
||||
const key = hashKey();
|
||||
|
||||
async function load() {
|
||||
let data;
|
||||
try { data = await api("GET", `/api/r/${pid}`, undefined, key); }
|
||||
catch {
|
||||
$("#denied").hidden = false;
|
||||
$("#list").replaceChildren();
|
||||
return;
|
||||
}
|
||||
$("#denied").hidden = true;
|
||||
$("#title").textContent = data.title;
|
||||
document.title = `Résultats · ${data.title}`;
|
||||
const qs = data.questions.sort((a, b) => a.id - b.id);
|
||||
$("#list").replaceChildren(...(qs.length
|
||||
? qs.map((q) => h("article", { class: "q" }, h("div", { class: "q-text" }, q.text), resultsView(q)))
|
||||
: [h("p", { class: "muted" }, "Aucun résultat pour l'instant.")]));
|
||||
}
|
||||
|
||||
load();
|
||||
// questions get added as the meeting goes on
|
||||
setInterval(() => { if (!document.hidden) load(); }, 15000);
|
||||
document.addEventListener("visibilitychange", () => { if (!document.hidden) load(); });
|
||||
@@ -0,0 +1,21 @@
|
||||
<!doctype html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>RotatyVote · Résultats</title>
|
||||
<link rel="icon" href="/static/img/mark.svg" type="image/svg+xml">
|
||||
<link rel="stylesheet" href="/static/css/app.css">
|
||||
<script type="module" src="/static/js/results.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<main class="wrap narrow results-page">
|
||||
<div class="topbar">
|
||||
<span class="brand"><img src="/static/img/mark.svg" alt=""><span>rotaty<b>vote</b></span></span>
|
||||
</div>
|
||||
<h1 id="title">Résultats</h1>
|
||||
<p id="denied" class="notice bad" hidden>Lien invalide ou désactivé.</p>
|
||||
<div id="list"></div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user