178 lines
5.7 KiB
Python
178 lines
5.7 KiB
Python
"""账号自动学习任务的立即开始与停止。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from datetime import datetime
|
|
|
|
from euniv.study_window import month_key, window_open
|
|
|
|
|
|
class StudyControlError(ValueError):
|
|
"""任务控制参数不符合要求。"""
|
|
|
|
|
|
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
|
|
|
|
|
|
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)
|
|
|
|
|
|
def schedule_enabled(account) -> bool:
|
|
if not hasattr(account, "study_schedule_enabled"):
|
|
return True
|
|
value = getattr(account, "study_schedule_enabled", True)
|
|
return False if value is False else True
|
|
|
|
|
|
def is_paused(account, now: datetime | None = None) -> bool:
|
|
if not bool(getattr(account, "study_paused", False)):
|
|
return False
|
|
recorded = str(getattr(account, "study_stop_month", "") or "")
|
|
return (not recorded) or recorded == month_key(now)
|
|
|
|
|
|
def ignores_window(account) -> bool:
|
|
return bool(getattr(account, "study_ignore_window", False))
|
|
|
|
|
|
def month_minutes(value: object) -> int:
|
|
match = re.search(r"(\d+)", str(value or ""))
|
|
return int(match.group(1)) if match else 0
|
|
|
|
|
|
def stamp_hours(account, hours: str, now: datetime | None = None) -> None:
|
|
account.month_hours = hours
|
|
account.hours_month = month_key(now)
|
|
|
|
|
|
def hours_met(account, now: datetime | None = None) -> bool:
|
|
target = int(getattr(account, "monthly_target", 0) or 0)
|
|
if target <= 0:
|
|
return False
|
|
recorded = str(getattr(account, "hours_month", "") or "")
|
|
if recorded and recorded != month_key(now):
|
|
return False
|
|
return month_minutes(getattr(account, "month_hours", "")) >= target
|
|
|
|
|
|
def expire_pause(account, now: datetime | None = None) -> bool:
|
|
"""本月的停止和立即开始都不带到下月。遗留的无月份暂停记到当月。"""
|
|
key = month_key(now)
|
|
recorded = str(getattr(account, "study_stop_month", "") or "")
|
|
changed = False
|
|
if ignores_window(account) and recorded and recorded != key:
|
|
account.study_ignore_window = False
|
|
changed = True
|
|
if not bool(getattr(account, "study_paused", False)):
|
|
if changed:
|
|
account.study_stop_month = ""
|
|
return changed
|
|
if not recorded:
|
|
account.study_stop_month = key
|
|
return True
|
|
if recorded != key:
|
|
account.study_paused = False
|
|
account.study_stop_month = ""
|
|
account.study_ignore_window = False
|
|
return True
|
|
return changed
|
|
|
|
|
|
def eligible(account, now: datetime | None = None) -> bool:
|
|
if is_paused(account, now):
|
|
return False
|
|
if hours_met(account, now):
|
|
return False
|
|
if ignores_window(account):
|
|
return True
|
|
if not schedule_enabled(account):
|
|
return False
|
|
return window_open(account, now)
|
|
|
|
|
|
def view_of(account, now: datetime | None = None) -> dict:
|
|
paused = is_paused(account, now)
|
|
ignore = ignores_window(account)
|
|
opened = window_open(account, now)
|
|
met = hours_met(account, now)
|
|
scheduled = schedule_enabled(account)
|
|
started = (not paused) and (not met) and (ignore or (scheduled and opened))
|
|
return {
|
|
"paused": paused,
|
|
"ignore_window": ignore,
|
|
"window_open": opened,
|
|
"hours_met": met,
|
|
"schedule_enabled": scheduled,
|
|
"started": started,
|
|
}
|
|
|
|
|
|
def assemble(account, live: dict | None = None, now: datetime | None = None) -> dict:
|
|
payload = view_of(account, now)
|
|
info = live if isinstance(live, dict) else {}
|
|
payload["state"] = str(info.get("state") or "")
|
|
payload["course"] = str(info.get("course") or "")
|
|
payload["message"] = str(info.get("message") or "")
|
|
workers = info.get("workers") or []
|
|
payload["workers"] = workers if isinstance(workers, list) else []
|
|
from euniv.autostudy import work_running
|
|
|
|
eid = str(getattr(account, "employee_id", "") or "")
|
|
running = bool(eid) and work_running(eid)
|
|
payload["active"] = running
|
|
payload["started"] = running
|
|
cached_hours = str(info.get("month_hours") or "")
|
|
payload["month_hours"] = cached_hours or str(getattr(account, "month_hours", "") or "")
|
|
try:
|
|
payload["monthly_target"] = int(getattr(account, "monthly_target", 0) or 0)
|
|
except (TypeError, ValueError):
|
|
payload["monthly_target"] = 0
|
|
return payload
|
|
|
|
|
|
def settle(account, live: dict | None = None, now: datetime | None = None) -> bool:
|
|
"""跨月恢复定时;达标且当前没在学时,收回立即开始标记。"""
|
|
changed = expire_pause(account, now)
|
|
payload = assemble(account, live, now)
|
|
if payload["hours_met"] and ignores_window(account) and not payload["active"]:
|
|
account.study_ignore_window = False
|
|
changed = True
|
|
return changed
|
|
|
|
|
|
def payload_for(employee_id: str) -> dict:
|
|
from euniv.autostudy import autostudy_status
|
|
from euniv.db import SessionLocal
|
|
from euniv.models import Account
|
|
|
|
key = (employee_id or "").strip()
|
|
empty = {
|
|
"started": False,
|
|
"paused": True,
|
|
"hours_met": False,
|
|
"schedule_enabled": True,
|
|
"workers": [],
|
|
"active": False,
|
|
"message": "",
|
|
}
|
|
if not key:
|
|
return empty
|
|
with SessionLocal() as db:
|
|
account = db.get(Account, key)
|
|
if account is None:
|
|
return empty
|
|
status = autostudy_status()
|
|
live = dict((status.get("users") or {}).get(key) or {})
|
|
if not live.get("message"):
|
|
live["message"] = str(status.get("message") or "")
|
|
if settle(account, live):
|
|
db.commit()
|
|
return assemble(account, live)
|