修复值班邮件跨站确认,独立记录未确认缺勤并与活动统计隔离
This commit is contained in:
+55
-1
@@ -6,6 +6,7 @@ import sqlite3
|
||||
import time
|
||||
from contextlib import closing
|
||||
from html import escape
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
|
||||
def connect(path):
|
||||
@@ -14,6 +15,9 @@ def connect(path):
|
||||
con.row_factory = sqlite3.Row
|
||||
con.execute("CREATE TABLE IF NOT EXISTS duty_receipts (day TEXT, slot TEXT, name TEXT, attempts INTEGER DEFAULT 0, sent_count INTEGER DEFAULT 0, last_attempt REAL DEFAULT 0, last_sent REAL DEFAULT 0, confirmed REAL DEFAULT 0, PRIMARY KEY(day,slot,name))")
|
||||
con.execute("CREATE TABLE IF NOT EXISTS duty_confirmations (digest TEXT PRIMARY KEY, targets TEXT NOT NULL, expires REAL NOT NULL)")
|
||||
con.execute("CREATE TABLE IF NOT EXISTS duty_confirmation_forms (digest TEXT PRIMARY KEY, nonce_hash TEXT NOT NULL)")
|
||||
con.execute("CREATE TABLE IF NOT EXISTS duty_absences (day TEXT, slot TEXT, name TEXT, end_at REAL NOT NULL, recorded_at REAL NOT NULL, PRIMARY KEY(day,slot,name))")
|
||||
con.execute("CREATE TABLE IF NOT EXISTS duty_audit_state (key TEXT PRIMARY KEY, value TEXT NOT NULL)")
|
||||
return con
|
||||
|
||||
|
||||
@@ -65,12 +69,28 @@ def mark_sent(path, day, names, now):
|
||||
con.commit()
|
||||
|
||||
|
||||
def confirmation(path, token, now, consume=False):
|
||||
def issue_confirmation_form(path, token, now):
|
||||
with closing(connect(path)) as con:
|
||||
digest = hashlib.sha256(token.encode()).hexdigest()
|
||||
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.commit()
|
||||
return 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()
|
||||
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()):
|
||||
return None
|
||||
targets = json.loads(row["targets"])
|
||||
if consume:
|
||||
for day, slot, name in targets:
|
||||
@@ -91,6 +111,40 @@ def reserve_test(path, email, now):
|
||||
return True
|
||||
|
||||
|
||||
def sync_absences(path, schedule, now):
|
||||
from .duty import BEIJING, duties_on
|
||||
now = now.astimezone(BEIJING)
|
||||
with closing(connect(path)) as con:
|
||||
con.execute("BEGIN IMMEDIATE")
|
||||
cursor = con.execute("SELECT value FROM duty_audit_state WHERE key='day'").fetchone()
|
||||
first = max(date.fromisoformat(schedule["anchor_date"]), date.fromisoformat(cursor[0]) if cursor else now.date())
|
||||
if schedule["enabled"]:
|
||||
day = first
|
||||
while day <= now.date():
|
||||
_, assignments = duties_on(schedule, day)
|
||||
for name, slots in assignments.items():
|
||||
for slot in slots:
|
||||
clock = schedule.get("slot_times", {}).get(slot)
|
||||
if not clock:
|
||||
continue
|
||||
end = datetime.combine(day, datetime.strptime(clock, '%H:%M').time(), BEIJING) + timedelta(minutes=schedule.get('duration_minutes', 105))
|
||||
if end > now:
|
||||
continue
|
||||
ensure(con, day.isoformat(), slot, name, None)
|
||||
row = con.execute("SELECT confirmed FROM duty_receipts WHERE day=? AND slot=? AND name=?", (day.isoformat(), slot, name)).fetchone()
|
||||
if not row[0] or row[0] > end.timestamp():
|
||||
con.execute("INSERT OR IGNORE INTO duty_absences VALUES (?,?,?,?,?)", (day.isoformat(), slot, name, end.timestamp(), now.timestamp()))
|
||||
day += timedelta(days=1)
|
||||
con.execute("INSERT OR REPLACE INTO duty_audit_state VALUES ('day',?)", (now.date().isoformat(),))
|
||||
con.commit()
|
||||
|
||||
|
||||
def absence_records(path, limit=50, offset=0):
|
||||
with closing(connect(path)) as con:
|
||||
rows = con.execute("SELECT * FROM duty_absences ORDER BY day DESC,end_at DESC,name LIMIT ? OFFSET ?", (limit, offset)).fetchall()
|
||||
return [{"day": r['day'], "slot": r['slot'], "name": r['name'], "end_at": int(r['end_at'] * 1000), "recorded_at": int(r['recorded_at'] * 1000), "reason": "未确认缺勤"} for r in rows]
|
||||
|
||||
|
||||
async def send_notice(path, day, names, email, expires, send_email, subject, body, base_url, now=None):
|
||||
import asyncio
|
||||
now = time.time() if now is None else now
|
||||
|
||||
Reference in New Issue
Block a user