feat: 检查全部时刷新本月学时
This commit is contained in:
+20
-3
@@ -110,6 +110,15 @@ def _require(db: Session, employee_id: str) -> Account:
|
||||
return account
|
||||
|
||||
|
||||
def _checked_hours(client) -> str:
|
||||
from euniv.service import _month_hours
|
||||
|
||||
try:
|
||||
return _month_hours(client)
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
def _checked_profile(client) -> dict:
|
||||
reader = getattr(client, "checked_profile", None)
|
||||
if not callable(reader):
|
||||
@@ -120,7 +129,7 @@ def _checked_profile(client) -> dict:
|
||||
return payload
|
||||
|
||||
|
||||
def check_employee(db: Session, employee_id: str) -> dict:
|
||||
def check_employee(db: Session, employee_id: str, *, refresh_hours: bool = False) -> dict:
|
||||
from euniv.proxy import bind_url, is_proxy_failure, url_of, uses_pool
|
||||
from euniv.proxy_pool import MAX_SWITCHES, retire_if_dead
|
||||
|
||||
@@ -135,6 +144,7 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
result = "检查失败"
|
||||
valid: bool | None = None
|
||||
profile: dict = {}
|
||||
hours = ""
|
||||
try:
|
||||
for _ in range(MAX_SWITCHES + 1):
|
||||
proxy = bind_url(account) if pooled else url_of(account)
|
||||
@@ -145,9 +155,12 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
result = "有效" if alive else "已失效"
|
||||
valid = alive
|
||||
profile = _checked_profile(client) if alive else {}
|
||||
if alive and refresh_hours:
|
||||
hours = _checked_hours(client)
|
||||
break
|
||||
except Exception as exc:
|
||||
profile = {}
|
||||
hours = ""
|
||||
if pooled and proxy and is_proxy_failure(exc):
|
||||
retire_if_dead(employee_id, proxy, str(exc)[:200])
|
||||
client.close()
|
||||
@@ -172,6 +185,10 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
from euniv.service import _remember_profile
|
||||
|
||||
_remember_profile(db, employee_id, profile)
|
||||
if valid is True and hours:
|
||||
from euniv.study_control import stamp_hours
|
||||
|
||||
stamp_hours(account, hours)
|
||||
db.commit()
|
||||
payload = {"employee_id": employee_id, "result": result}
|
||||
if result == "检查失败":
|
||||
@@ -183,7 +200,7 @@ def _check_saved(db: Session, employee_ids: list[str]) -> list[dict]:
|
||||
results = []
|
||||
for employee_id in employee_ids:
|
||||
try:
|
||||
results.append(check_employee(db, employee_id))
|
||||
results.append(check_employee(db, employee_id, refresh_hours=True))
|
||||
except HTTPException as exc:
|
||||
if exc.status_code == 404:
|
||||
continue
|
||||
@@ -215,7 +232,7 @@ def _run_check(employee_ids: list[str]) -> None:
|
||||
for employee_id in employee_ids:
|
||||
with SessionLocal() as db:
|
||||
try:
|
||||
results.append(check_employee(db, employee_id))
|
||||
results.append(check_employee(db, employee_id, refresh_hours=True))
|
||||
except HTTPException as exc:
|
||||
if exc.status_code != 404:
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user