feat: 用户列表显示任务开启时间
This commit is contained in:
@@ -8,7 +8,7 @@ import re
|
||||
import secrets
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from types import SimpleNamespace
|
||||
|
||||
from euniv.study_window import month_key, window_open
|
||||
@@ -89,16 +89,41 @@ def stamp_frame(employee_id: str, payload: dict) -> dict:
|
||||
return stamped
|
||||
|
||||
|
||||
def _aware(now: datetime | None) -> datetime:
|
||||
moment = now or datetime.now(timezone.utc)
|
||||
if moment.tzinfo is None:
|
||||
return moment.replace(tzinfo=timezone.utc)
|
||||
return moment
|
||||
|
||||
|
||||
def start_now(account, now: datetime | None = None) -> None:
|
||||
account.study_paused = False
|
||||
account.study_stop_month = month_key(now)
|
||||
account.study_ignore_window = True
|
||||
account.study_opened_at = _aware(now)
|
||||
|
||||
|
||||
def stop_now(account, now: datetime | None = None) -> None:
|
||||
account.study_paused = True
|
||||
account.study_ignore_window = False
|
||||
account.study_stop_month = month_key(now)
|
||||
account.study_opened_at = None
|
||||
|
||||
|
||||
def remember_study_opened(employee_id: str, now: datetime | None = None) -> None:
|
||||
"""定时任务第一次真正启动时补记开启时间。已经记过的不改,避免重启把时间刷掉。"""
|
||||
from euniv.db import SessionLocal
|
||||
from euniv.models import Account
|
||||
|
||||
key = (employee_id or "").strip()
|
||||
if not key:
|
||||
return
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, key)
|
||||
if account is None or account.study_opened_at is not None:
|
||||
return
|
||||
account.study_opened_at = _aware(now)
|
||||
db.commit()
|
||||
|
||||
|
||||
def schedule_enabled(account) -> bool:
|
||||
@@ -146,6 +171,7 @@ def expire_pause(account, now: datetime | None = None) -> bool:
|
||||
changed = False
|
||||
if ignores_window(account) and recorded and recorded != key:
|
||||
account.study_ignore_window = False
|
||||
account.study_opened_at = None
|
||||
changed = True
|
||||
if not bool(getattr(account, "study_paused", False)):
|
||||
if changed:
|
||||
@@ -158,6 +184,7 @@ def expire_pause(account, now: datetime | None = None) -> bool:
|
||||
account.study_paused = False
|
||||
account.study_stop_month = ""
|
||||
account.study_ignore_window = False
|
||||
account.study_opened_at = None
|
||||
return True
|
||||
return changed
|
||||
|
||||
|
||||
Reference in New Issue
Block a user