Trial page: throwaway ring poll to test a screen with a phone, swept after 2 h
This commit is contained in:
@@ -415,6 +415,16 @@ async def create_poll(request: Request):
|
||||
return JSONResponse({"id": pid, "admin_key": admin_key, "screen_key": screen_key})
|
||||
|
||||
|
||||
async def create_trial(request: Request):
|
||||
# no password even when creation is locked: a trial only enrolls, nobody gets its admin key
|
||||
db.purge_trials(config.TRIAL_TTL_MS)
|
||||
if db.trial_count() >= config.MAX_TRIALS:
|
||||
return err("too_many_trials", 429)
|
||||
pid, _, screen_key = db.create_poll("Essai", 500, 5, False, None, 200, 0, "ring", trial=True)
|
||||
db.update_poll(pid, enrollment_open=1)
|
||||
return JSONResponse({"id": pid, "screen_key": screen_key})
|
||||
|
||||
|
||||
async def admin_get(request: Request):
|
||||
pid = request.path_params["pid"]
|
||||
require_admin(request, pid)
|
||||
@@ -845,10 +855,12 @@ routes = [
|
||||
Route("/", page("index.html")),
|
||||
Route("/admin/{pid}", page("admin.html")),
|
||||
Route("/screen/{pid}", page("screen.html")),
|
||||
Route("/essai", page("essai.html")),
|
||||
Route("/v/{pid}", page("vote.html")),
|
||||
Route("/V/{pid}", lambda r: RedirectResponse(f"/v/{r.path_params['pid'].lower()}", status_code=301)),
|
||||
Route("/api/config", public_config, methods=["GET"]),
|
||||
Route("/api/polls", create_poll, methods=["POST"]),
|
||||
Route("/api/trial", create_trial, methods=["POST"]),
|
||||
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"]),
|
||||
|
||||
@@ -29,6 +29,10 @@ HEARTBEAT_S = float(os.environ.get("HEARTBEAT_S", "25"))
|
||||
DEFAULT_VOTE_SCANS = 3
|
||||
MAX_VOTE_SCANS = 5
|
||||
|
||||
# throwaway polls from /essai: deleted after this, and only so many alive at once
|
||||
TRIAL_TTL_MS = 2 * 3600 * 1000
|
||||
MAX_TRIALS = int(os.environ.get("MAX_TRIALS", "50"))
|
||||
|
||||
MIN_INTERVAL_MS = 300
|
||||
MAX_INTERVAL_MS = 5000
|
||||
MIN_SCANS = 2
|
||||
|
||||
+19
-3
@@ -87,6 +87,7 @@ class DB:
|
||||
("voters", "via_code", "INTEGER NOT NULL DEFAULT 0"),
|
||||
("access_codes", "rescue", "INTEGER NOT NULL DEFAULT 0"),
|
||||
("polls", "code_style", "TEXT NOT NULL DEFAULT 'qr'"),
|
||||
("polls", "trial", "INTEGER NOT NULL DEFAULT 0"),
|
||||
):
|
||||
cols = {r[1] for r in self.c.execute(f"PRAGMA table_info({table})")}
|
||||
if col not in cols:
|
||||
@@ -110,19 +111,34 @@ class DB:
|
||||
|
||||
|
||||
def create_poll(self, title, interval_ms, scans_required, require_code, expected, max_voters, vote_scans=0,
|
||||
code_style="qr"):
|
||||
code_style="qr", trial=False):
|
||||
pid = new_id()
|
||||
while self.one("SELECT 1 FROM polls WHERE id=?", pid):
|
||||
pid = new_id()
|
||||
admin_key, screen_key = new_key(), new_key()
|
||||
self.c.execute(
|
||||
"INSERT INTO polls(id,title,admin_hash,screen_hash,secret,interval_ms,scans_required,"
|
||||
"require_code,expected,max_voters,vote_scans,code_style,created_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
||||
"require_code,expected,max_voters,vote_scans,code_style,trial,created_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
||||
(pid, title, sha(admin_key), sha(screen_key), secrets.token_bytes(32), interval_ms,
|
||||
scans_required, int(require_code), expected, max_voters, vote_scans, code_style, now_ms()),
|
||||
scans_required, int(require_code), expected, max_voters, vote_scans, code_style, int(trial), now_ms()),
|
||||
)
|
||||
return pid, admin_key, screen_key
|
||||
|
||||
def purge_trials(self, max_age_ms):
|
||||
old = [r["id"] for r in self.all("SELECT id FROM polls WHERE trial=1 AND created_at<?", now_ms() - max_age_ms)]
|
||||
with self.tx() as c:
|
||||
for pid in old:
|
||||
qids = "SELECT id FROM questions WHERE poll_id=?"
|
||||
c.execute(f"DELETE FROM ballots WHERE question_id IN ({qids})", (pid,))
|
||||
c.execute(f"DELETE FROM participation WHERE question_id IN ({qids})", (pid,))
|
||||
for table, col in (("questions", "poll_id"), ("voters", "poll_id"), ("access_codes", "poll_id"),
|
||||
("polls", "id")):
|
||||
c.execute(f"DELETE FROM {table} WHERE {col}=?", (pid,))
|
||||
return old
|
||||
|
||||
def trial_count(self):
|
||||
return self.one("SELECT COUNT(*) n FROM polls WHERE trial=1")["n"]
|
||||
|
||||
def poll(self, pid):
|
||||
return self.one("SELECT * FROM polls WHERE id=?", pid)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user