重构共享逻辑并修复值班提醒回执去重
This commit is contained in:
+20
-16
@@ -1,5 +1,4 @@
|
||||
"""值班提醒回执;独立 SQLite 保存每人每轮的发送次数和确认状态。"""
|
||||
import hashlib
|
||||
"""值班提醒回执、确认表单nonce、邮件按钮测试和旧缺勤记录兼容。"""
|
||||
import json
|
||||
import secrets
|
||||
import sqlite3
|
||||
@@ -7,6 +6,8 @@ import time
|
||||
from contextlib import closing
|
||||
from html import escape
|
||||
|
||||
from .security import token_digest
|
||||
|
||||
|
||||
def connect(path):
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
@@ -22,8 +23,11 @@ def connect(path):
|
||||
def ensure(con, day, slot, name, email):
|
||||
# 导入旧版成功发送记录;新版已有回执后不重复导入。
|
||||
count, last = 0, 0
|
||||
if email and con.execute("SELECT 1 FROM sqlite_master WHERE name='deliveries'").fetchone():
|
||||
rows = con.execute("SELECT updated FROM deliveries WHERE email=? AND status='sent' AND day IN (?,?)", (email, day, f"{day}#before:{slot}")).fetchall()
|
||||
exists = con.execute("SELECT 1 FROM duty_receipts WHERE day=? AND slot=? AND name=?", (day, slot, name)).fetchone()
|
||||
if not exists and email and con.execute("SELECT 1 FROM sqlite_master WHERE name='deliveries'").fetchone():
|
||||
# 只有当前任务已发送的记录才能算作既有回执;旧表可能由当前调度器
|
||||
# 预先写入 sending/failed,不能因为新行刚刚创建就把 slot 判成已发过。
|
||||
rows = con.execute("SELECT updated FROM deliveries WHERE email=? AND status='sent' AND updated > 0 AND day IN (?,?)", (email, day, f"{day}#before:{slot}")).fetchall()
|
||||
count = len(rows)
|
||||
last = max((r[0] for r in rows), default=0)
|
||||
con.execute("INSERT OR IGNORE INTO duty_receipts(day,slot,name,attempts,sent_count,last_attempt,last_sent) VALUES (?,?,?,?,?,?,?)", (day, slot, name, count, count, last, last))
|
||||
@@ -54,7 +58,7 @@ def reserve(path, day, names, email, expires, now):
|
||||
return None, {}
|
||||
token = secrets.token_urlsafe(32)
|
||||
targets = [(day, slot, name) for name, slots in selected.items() for slot in slots]
|
||||
con.execute("INSERT INTO duty_confirmations VALUES (?,?,?)", (hashlib.sha256(token.encode()).hexdigest(), json.dumps(targets), expires))
|
||||
con.execute("INSERT INTO duty_confirmations VALUES (?,?,?)", (token_digest(token), json.dumps(targets), expires))
|
||||
con.commit()
|
||||
return token, selected
|
||||
|
||||
@@ -81,21 +85,21 @@ def confirm_on_site(path, day, slot, name, email, now, end_at):
|
||||
|
||||
def issue_confirmation_form(path, token, now):
|
||||
with closing(connect(path)) as con:
|
||||
digest = hashlib.sha256(token.encode()).hexdigest()
|
||||
digest = token_digest(token)
|
||||
row = con.execute("SELECT expires FROM duty_confirmations WHERE digest=?", (digest,)).fetchone()
|
||||
if row is None or row[0] <= now:
|
||||
return None
|
||||
nonce = secrets.token_urlsafe(32)
|
||||
con.execute("INSERT OR REPLACE INTO duty_confirmation_forms VALUES (?,?)", (digest, hashlib.sha256(nonce.encode()).hexdigest()))
|
||||
con.execute("INSERT OR REPLACE INTO duty_confirmation_forms VALUES (?,?)", (digest, token_digest(nonce)))
|
||||
con.commit()
|
||||
return nonce
|
||||
|
||||
|
||||
def issue_activity_form(path, token, namespace='activity'):
|
||||
digest = hashlib.sha256((namespace + ':' + token).encode()).hexdigest()
|
||||
digest = token_digest(namespace + ':' + token)
|
||||
nonce = secrets.token_urlsafe(32)
|
||||
with closing(connect(path)) as con:
|
||||
con.execute("INSERT OR REPLACE INTO duty_confirmation_forms VALUES (?,?)", (digest, hashlib.sha256(nonce.encode()).hexdigest()))
|
||||
con.execute("INSERT OR REPLACE INTO duty_confirmation_forms VALUES (?,?)", (digest, token_digest(nonce)))
|
||||
con.commit()
|
||||
return nonce
|
||||
|
||||
@@ -103,21 +107,21 @@ def issue_activity_form(path, token, namespace='activity'):
|
||||
def validate_activity_form(path, token, nonce, namespace='activity'):
|
||||
if not nonce:
|
||||
return False
|
||||
digest = hashlib.sha256((namespace + ':' + token).encode()).hexdigest()
|
||||
digest = token_digest(namespace + ':' + token)
|
||||
with closing(connect(path)) as con:
|
||||
row = con.execute("SELECT nonce_hash FROM duty_confirmation_forms WHERE digest=?", (digest,)).fetchone()
|
||||
return bool(row and secrets.compare_digest(row[0], hashlib.sha256(nonce.encode()).hexdigest()))
|
||||
return bool(row and secrets.compare_digest(row[0], token_digest(nonce)))
|
||||
|
||||
|
||||
def confirmation(path, token, now, consume=False, form_token=None, require_form=False):
|
||||
with closing(connect(path)) as con:
|
||||
con.execute("BEGIN IMMEDIATE")
|
||||
row = con.execute("SELECT * FROM duty_confirmations WHERE digest=?", (hashlib.sha256(token.encode()).hexdigest(),)).fetchone()
|
||||
row = con.execute("SELECT * FROM duty_confirmations WHERE digest=?", (token_digest(token),)).fetchone()
|
||||
if row is None or row["expires"] <= now:
|
||||
return None
|
||||
if consume and require_form:
|
||||
form = con.execute("SELECT nonce_hash FROM duty_confirmation_forms WHERE digest=?", (hashlib.sha256(token.encode()).hexdigest(),)).fetchone()
|
||||
if form is None or not form_token or not secrets.compare_digest(form[0], hashlib.sha256(form_token.encode()).hexdigest()):
|
||||
form = con.execute("SELECT nonce_hash FROM duty_confirmation_forms WHERE digest=?", (token_digest(token),)).fetchone()
|
||||
if form is None or not form_token or not secrets.compare_digest(form[0], token_digest(form_token)):
|
||||
return None
|
||||
targets = json.loads(row["targets"])
|
||||
if consume:
|
||||
@@ -142,13 +146,13 @@ def reserve_test(path, email, now):
|
||||
def create_button_test(path, kind, now):
|
||||
token = secrets.token_urlsafe(32)
|
||||
with closing(connect(path)) as con:
|
||||
con.execute("INSERT INTO mail_button_tests(digest,kind,expires) VALUES (?,?,?)", (hashlib.sha256(token.encode()).hexdigest(), kind, now + 86400))
|
||||
con.execute("INSERT INTO mail_button_tests(digest,kind,expires) VALUES (?,?,?)", (token_digest(token), kind, now + 86400))
|
||||
con.commit()
|
||||
return token
|
||||
|
||||
|
||||
def button_test(path, token, now, consume=False):
|
||||
digest = hashlib.sha256(token.encode()).hexdigest()
|
||||
digest = token_digest(token)
|
||||
with closing(connect(path)) as con:
|
||||
row = con.execute("SELECT * FROM mail_button_tests WHERE digest=? AND expires>?", (digest, now)).fetchone()
|
||||
if not row:
|
||||
|
||||
Reference in New Issue
Block a user