feat: 检查全部时刷新本月学时
This commit is contained in:
+20
-3
@@ -110,6 +110,15 @@ def _require(db: Session, employee_id: str) -> Account:
|
|||||||
return 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:
|
def _checked_profile(client) -> dict:
|
||||||
reader = getattr(client, "checked_profile", None)
|
reader = getattr(client, "checked_profile", None)
|
||||||
if not callable(reader):
|
if not callable(reader):
|
||||||
@@ -120,7 +129,7 @@ def _checked_profile(client) -> dict:
|
|||||||
return payload
|
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 import bind_url, is_proxy_failure, url_of, uses_pool
|
||||||
from euniv.proxy_pool import MAX_SWITCHES, retire_if_dead
|
from euniv.proxy_pool import MAX_SWITCHES, retire_if_dead
|
||||||
|
|
||||||
@@ -135,6 +144,7 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
|||||||
result = "检查失败"
|
result = "检查失败"
|
||||||
valid: bool | None = None
|
valid: bool | None = None
|
||||||
profile: dict = {}
|
profile: dict = {}
|
||||||
|
hours = ""
|
||||||
try:
|
try:
|
||||||
for _ in range(MAX_SWITCHES + 1):
|
for _ in range(MAX_SWITCHES + 1):
|
||||||
proxy = bind_url(account) if pooled else url_of(account)
|
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 "已失效"
|
result = "有效" if alive else "已失效"
|
||||||
valid = alive
|
valid = alive
|
||||||
profile = _checked_profile(client) if alive else {}
|
profile = _checked_profile(client) if alive else {}
|
||||||
|
if alive and refresh_hours:
|
||||||
|
hours = _checked_hours(client)
|
||||||
break
|
break
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
profile = {}
|
profile = {}
|
||||||
|
hours = ""
|
||||||
if pooled and proxy and is_proxy_failure(exc):
|
if pooled and proxy and is_proxy_failure(exc):
|
||||||
retire_if_dead(employee_id, proxy, str(exc)[:200])
|
retire_if_dead(employee_id, proxy, str(exc)[:200])
|
||||||
client.close()
|
client.close()
|
||||||
@@ -172,6 +185,10 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
|||||||
from euniv.service import _remember_profile
|
from euniv.service import _remember_profile
|
||||||
|
|
||||||
_remember_profile(db, employee_id, 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()
|
db.commit()
|
||||||
payload = {"employee_id": employee_id, "result": result}
|
payload = {"employee_id": employee_id, "result": result}
|
||||||
if result == "检查失败":
|
if result == "检查失败":
|
||||||
@@ -183,7 +200,7 @@ def _check_saved(db: Session, employee_ids: list[str]) -> list[dict]:
|
|||||||
results = []
|
results = []
|
||||||
for employee_id in employee_ids:
|
for employee_id in employee_ids:
|
||||||
try:
|
try:
|
||||||
results.append(check_employee(db, employee_id))
|
results.append(check_employee(db, employee_id, refresh_hours=True))
|
||||||
except HTTPException as exc:
|
except HTTPException as exc:
|
||||||
if exc.status_code == 404:
|
if exc.status_code == 404:
|
||||||
continue
|
continue
|
||||||
@@ -215,7 +232,7 @@ def _run_check(employee_ids: list[str]) -> None:
|
|||||||
for employee_id in employee_ids:
|
for employee_id in employee_ids:
|
||||||
with SessionLocal() as db:
|
with SessionLocal() as db:
|
||||||
try:
|
try:
|
||||||
results.append(check_employee(db, employee_id))
|
results.append(check_employee(db, employee_id, refresh_hours=True))
|
||||||
except HTTPException as exc:
|
except HTTPException as exc:
|
||||||
if exc.status_code != 404:
|
if exc.status_code != 404:
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -544,12 +544,25 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
|
|||||||
_add_account("a-user")
|
_add_account("a-user")
|
||||||
_add_account("b-user")
|
_add_account("b-user")
|
||||||
_add_account("c-user", credential=None)
|
_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 = []
|
calls = []
|
||||||
|
|
||||||
|
class _NoHours(_Alive):
|
||||||
|
def month_hours(self) -> str:
|
||||||
|
return ""
|
||||||
|
|
||||||
def factory(login_name: str, password: str, proxy: str | None = None):
|
def factory(login_name: str, password: str, proxy: str | None = None):
|
||||||
calls.append(login_name)
|
calls.append(login_name)
|
||||||
if login_name == "b-user":
|
if login_name == "b-user":
|
||||||
raise RuntimeError("网络失败")
|
raise RuntimeError("网络失败")
|
||||||
|
if login_name == "kept-user":
|
||||||
|
return _NoHours(login_name, password)
|
||||||
return _Alive(login_name, password)
|
return _Alive(login_name, password)
|
||||||
|
|
||||||
monkeypatch.setattr(users, "build_client", factory)
|
monkeypatch.setattr(users, "build_client", factory)
|
||||||
@@ -561,15 +574,23 @@ def test_check_all_reports_each_saved_account(monkeypatch) -> None:
|
|||||||
assert response.json() == [
|
assert response.json() == [
|
||||||
{"employee_id": "a-user", "result": "有效"},
|
{"employee_id": "a-user", "result": "有效"},
|
||||||
{"employee_id": "b-user", "result": "检查失败", "detail": "检查失败"},
|
{"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:
|
with SessionLocal() as db:
|
||||||
failed = db.get(Account, "b-user")
|
failed = db.get(Account, "b-user")
|
||||||
untouched = db.get(Account, "c-user")
|
untouched = db.get(Account, "c-user")
|
||||||
checked = db.get(Account, "a-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 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 is not None and failed.credential_valid is None
|
||||||
assert failed.user_name == "" and failed.company == ""
|
assert failed.user_name == "" and failed.company == ""
|
||||||
|
assert failed.month_hours == ""
|
||||||
assert failed.last_refresh_at is not None
|
assert failed.last_refresh_at is not None
|
||||||
assert untouched is not None and untouched.last_refresh_at is None
|
assert untouched is not None and untouched.last_refresh_at is None
|
||||||
assert untouched.user_name == "" and untouched.company == ""
|
assert untouched.user_name == "" and untouched.company == ""
|
||||||
|
|||||||
Reference in New Issue
Block a user