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
|
||||
|
||||
@@ -544,12 +544,25 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
|
||||
_add_account("a-user")
|
||||
_add_account("b-user")
|
||||
_add_account("c-user", credential=None)
|
||||
_add_account("kept-user")
|
||||
with SessionLocal() as db:
|
||||
kept = db.get(Account, "kept-user")
|
||||
assert kept is not None
|
||||
kept.month_hours = "12"
|
||||
kept.hours_month = "2026-10"
|
||||
db.commit()
|
||||
calls = []
|
||||
|
||||
class _NoHours(_Alive):
|
||||
def month_hours(self) -> str:
|
||||
return ""
|
||||
|
||||
def factory(login_name: str, password: str, proxy: str | None = None):
|
||||
calls.append(login_name)
|
||||
if login_name == "b-user":
|
||||
raise RuntimeError("网络失败")
|
||||
if login_name == "kept-user":
|
||||
return _NoHours(login_name, password)
|
||||
return _Alive(login_name, password)
|
||||
|
||||
monkeypatch.setattr(users, "build_client", factory)
|
||||
@@ -561,15 +574,23 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
|
||||
assert response.json() == [
|
||||
{"employee_id": "a-user", "result": "有效"},
|
||||
{"employee_id": "b-user", "result": "检查失败", "detail": "检查失败"},
|
||||
{"employee_id": "kept-user", "result": "有效"},
|
||||
]
|
||||
assert calls == ["a-user", "b-user"]
|
||||
assert calls == ["a-user", "b-user", "kept-user"]
|
||||
from euniv.study_window import month_key
|
||||
|
||||
with SessionLocal() as db:
|
||||
failed = db.get(Account, "b-user")
|
||||
untouched = db.get(Account, "c-user")
|
||||
checked = db.get(Account, "a-user")
|
||||
kept = db.get(Account, "kept-user")
|
||||
assert checked is not None and checked.user_name == "莫英达" and checked.company == "广州瀚信广州处"
|
||||
assert checked.month_hours == "660"
|
||||
assert checked.hours_month == month_key()
|
||||
assert kept is not None and kept.month_hours == "12" and kept.hours_month == "2026-10"
|
||||
assert failed is not None and failed.credential_valid is None
|
||||
assert failed.user_name == "" and failed.company == ""
|
||||
assert failed.month_hours == ""
|
||||
assert failed.last_refresh_at is not None
|
||||
assert untouched is not None and untouched.last_refresh_at is None
|
||||
assert untouched.user_name == "" and untouched.company == ""
|
||||
|
||||
Reference in New Issue
Block a user