feat: 增加公告栏并让用户自选学时判断

This commit is contained in:
Nixevol
2026-11-19 04:08:30 +08:00
parent 3fe6d4c111
commit 4d4fe224bc
20 changed files with 789 additions and 64 deletions
+14
View File
@@ -84,6 +84,7 @@ class StudyBundleBody(BaseModel):
heartbeat_video_seconds: int | None = Field(default=None, strict=True, ge=0, le=300)
heartbeat_document_seconds: int | None = Field(default=None, strict=True, ge=0, le=600)
heartbeat_scorm_seconds: int | None = Field(default=None, strict=True, ge=0, le=600)
hours_source: str | None = None
class StudyControlBody(BaseModel):
@@ -327,6 +328,14 @@ def _signed_in(request: Request, db: Session) -> str:
return employee_id
@app.get("/api/announcements")
def announcements(request: Request, db: Session = Depends(get_db)) -> list[dict]:
from admin.notices import list_notices
_signed_in(request, db)
return list_notices(db)
@app.get("/api/home/summary")
def home_summary(request: Request, db: Session = Depends(get_db)) -> dict:
try:
@@ -446,6 +455,11 @@ def _apply_study_bundle(account: Account, body: StudyBundleBody) -> None:
body.heartbeat_document_seconds,
body.heartbeat_scorm_seconds,
)
if body.hours_source is not None:
chosen = str(body.hours_source).strip().lower()
if chosen not in {"api", "history"}:
raise HTTPException(status_code=400, detail="学时判断须为官方统计或历史统计")
account.hours_source = chosen
except (StudyWindowError, CourseWorkersError, HeartbeatError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
+4 -8
View File
@@ -889,9 +889,9 @@ class HoursWatcher:
self.user_stop.set()
return True
try:
from admin.settings import hours_source_of
from admin.settings import source_for_employee
source = hours_source_of()
source = source_for_employee(self.employee_id)
official = ""
if source == "history":
current = int(await self.session.month_history_minutes())
@@ -2104,8 +2104,6 @@ class AutoStudyWorker:
async def _run_account(self, job: tuple, course_list, generation: int) -> None:
emp_id, target, pw, cred, proxy, workers, use_pool = job
try:
from admin.settings import hours_source_of
if int(target or 0) <= 0:
if use_pool:
await asyncio.to_thread(_release_pool, emp_id, True)
@@ -2115,10 +2113,9 @@ class AutoStudyWorker:
if use_pool:
await asyncio.to_thread(_release_pool, emp_id, True)
return
hours_source = await asyncio.to_thread(hours_source_of)
await self._study_user(
emp_id, target, pw, cred, proxy, workers, use_pool,
course_list, hours_source,
course_list,
)
except Exception as exc:
log.warning("用户 %s 自动学习失败: %s", emp_id, exc)
@@ -2139,8 +2136,7 @@ class AutoStudyWorker:
async def _study_user(self, employee_id: str, target: int,
password: str, credential: dict, proxy: str | None,
workers: int, use_pool: bool, course_list: list,
hours_source: str) -> None:
workers: int, use_pool: bool, course_list: list) -> None:
assert self._async_stop is not None
user_stop = asyncio.Event()
with _halt_lock:
+3
View File
@@ -56,11 +56,14 @@ def _stored(value: object, default: int, high: int) -> int:
def view_of(account) -> dict:
from admin.settings import account_hours_source
return {
"heartbeat_mode": mode_of(account),
"heartbeat_video_seconds": video_of(account),
"heartbeat_document_seconds": document_of(account),
"heartbeat_scorm_seconds": scorm_of(account),
"hours_source": account_hours_source(account),
}
+13
View File
@@ -78,6 +78,7 @@ class Account(Base):
study_opened_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
study_schedule_enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=sql_false())
heartbeat_mode: Mapped[str] = mapped_column(String(16), nullable=False, default="auto", server_default="auto")
hours_source: Mapped[str] = mapped_column(String(16), nullable=False, default="api", server_default="api")
heartbeat_video_seconds: Mapped[int] = mapped_column(nullable=False, default=30, server_default="30")
heartbeat_document_seconds: Mapped[int] = mapped_column(nullable=False, default=120, server_default="120")
heartbeat_scorm_seconds: Mapped[int] = mapped_column(nullable=False, default=120, server_default="120")
@@ -87,3 +88,15 @@ class Account(Base):
password_login_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, server_default=text("CURRENT_TIMESTAMP")
)
class Announcement(Base):
"""面向用户的公告。时间精确到分钟。"""
__tablename__ = "announcements"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(80), nullable=False, default="")
body: Mapped[str] = mapped_column(Text, nullable=False, default="")
link_url: Mapped[str] = mapped_column(String(300), nullable=False, default="", server_default=text("''"))
published_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
+12 -5
View File
@@ -135,7 +135,14 @@ def _persist_locked(db: Session, employee_id: str, password: str | None, client:
client.authorize(profile)
account = db.get(Account, employee_id)
if account is None:
account = Account(employee_id=employee_id, password_cipher="", credential_cipher="")
from admin.settings import hours_source_of
account = Account(
employee_id=employee_id,
password_cipher="",
credential_cipher="",
hours_source=hours_source_of(),
)
db.add(account)
if password is None:
stored = ""
@@ -411,10 +418,10 @@ def fetch_study_history(db: Session, employee_id: str, client_factory=EunivClien
def _apply_hours_source(client, summary: dict, items: list | None = None) -> dict:
from admin.settings import hours_source_of
from admin.settings import source_for_employee
data = dict(summary)
source = hours_source_of()
source = source_for_employee(str(getattr(client, "login_name", "") or ""))
data["hours_source"] = source
data["official_month_hours"] = data.get("month_hours")
if source != "history":
@@ -839,9 +846,9 @@ def start_credential_refresh() -> _RefreshGate:
def _month_hours(client) -> str:
from admin.settings import hours_source_of
from admin.settings import source_for_employee
if hours_source_of() == "history":
if source_for_employee(str(getattr(client, "login_name", "") or "")) == "history":
reader = getattr(client, "month_history_minutes", None)
if reader is None:
return ""