停止记录和补记值班缺勤,移除缺勤入口并保留历史数据
This commit is contained in:
+7
-32
@@ -6,7 +6,6 @@ import sqlite3
|
||||
import time
|
||||
from contextlib import closing
|
||||
from html import escape
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
|
||||
def connect(path):
|
||||
@@ -16,8 +15,6 @@ def connect(path):
|
||||
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)")
|
||||
con.execute("CREATE TABLE IF NOT EXISTS mail_button_tests (digest TEXT PRIMARY KEY, kind TEXT NOT NULL, expires REAL NOT NULL, confirmed REAL NOT NULL DEFAULT 0)")
|
||||
return con
|
||||
|
||||
@@ -162,36 +159,14 @@ def button_test(path, token, now, consume=False):
|
||||
return row['kind']
|
||||
|
||||
|
||||
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:
|
||||
"""只读兼容历史缺勤记录,不再创建、生成或补记缺勤。"""
|
||||
if not path.exists():
|
||||
return []
|
||||
with closing(sqlite3.connect(path.resolve().as_uri() + '?mode=ro', uri=True)) as con:
|
||||
con.row_factory = sqlite3.Row
|
||||
if not con.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name='duty_absences'").fetchone():
|
||||
return []
|
||||
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]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user