feat: 增加公告栏并让用户自选学时判断
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
"""公告的发布和读取。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from euniv.models import Announcement
|
||||
from euniv.study_window import CN_TZ
|
||||
|
||||
|
||||
def _minute(value: datetime) -> datetime:
|
||||
if value.tzinfo is None:
|
||||
value = value.replace(tzinfo=CN_TZ)
|
||||
else:
|
||||
value = value.astimezone(CN_TZ)
|
||||
return value.replace(second=0, microsecond=0)
|
||||
|
||||
|
||||
def parse_published_at(raw: str) -> datetime:
|
||||
text = (raw or "").strip()
|
||||
if not text:
|
||||
raise HTTPException(status_code=400, detail="请填写公告时间")
|
||||
try:
|
||||
parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail="公告时间无法识别") from exc
|
||||
return _minute(parsed)
|
||||
|
||||
|
||||
def clean_notice(title: str, body: str, link_url: str) -> tuple[str, str, str]:
|
||||
title_text = (title or "").strip()
|
||||
body_text = (body or "").strip()
|
||||
link = (link_url or "").strip()
|
||||
if not title_text or len(title_text) > 80:
|
||||
raise HTTPException(status_code=400, detail="标题须为 1 到 80 个字")
|
||||
if not body_text or len(body_text) > 4000:
|
||||
raise HTTPException(status_code=400, detail="正文须为 1 到 4000 个字")
|
||||
if link:
|
||||
if len(link) > 300:
|
||||
raise HTTPException(status_code=400, detail="链接过长")
|
||||
parsed = urlparse(link)
|
||||
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
|
||||
raise HTTPException(status_code=400, detail="链接须以 http 或 https 开头")
|
||||
return title_text, body_text, link
|
||||
|
||||
|
||||
def _view(row: Announcement) -> dict:
|
||||
published = row.published_at
|
||||
if published.tzinfo is None:
|
||||
published = published.replace(tzinfo=CN_TZ)
|
||||
return {
|
||||
"id": row.id,
|
||||
"title": row.title,
|
||||
"body": row.body,
|
||||
"link_url": row.link_url or "",
|
||||
"published_at": published.isoformat(),
|
||||
}
|
||||
|
||||
|
||||
def list_notices(db: Session) -> list[dict]:
|
||||
rows = db.scalars(select(Announcement).order_by(Announcement.published_at.desc(), Announcement.id.desc())).all()
|
||||
return [_view(row) for row in rows]
|
||||
|
||||
|
||||
def create_notice(db: Session, title: str, body: str, link_url: str, published_at: str) -> dict:
|
||||
title_text, body_text, link = clean_notice(title, body, link_url)
|
||||
row = Announcement(
|
||||
title=title_text,
|
||||
body=body_text,
|
||||
link_url=link,
|
||||
published_at=parse_published_at(published_at),
|
||||
)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return _view(row)
|
||||
|
||||
|
||||
def update_notice(db: Session, notice_id: int, title: str, body: str, link_url: str, published_at: str) -> dict:
|
||||
row = db.get(Announcement, notice_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="公告不存在")
|
||||
title_text, body_text, link = clean_notice(title, body, link_url)
|
||||
row.title = title_text
|
||||
row.body = body_text
|
||||
row.link_url = link
|
||||
row.published_at = parse_published_at(published_at)
|
||||
db.commit()
|
||||
return _view(row)
|
||||
|
||||
|
||||
def delete_notice(db: Session, notice_id: int) -> dict:
|
||||
row = db.get(Announcement, notice_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="公告不存在")
|
||||
db.delete(row)
|
||||
db.commit()
|
||||
return {"ok": True}
|
||||
@@ -44,6 +44,13 @@ class SettingsBody(BaseModel):
|
||||
hours_source: str = "api"
|
||||
|
||||
|
||||
class NoticeBody(BaseModel):
|
||||
title: str
|
||||
body: str
|
||||
link_url: str = ""
|
||||
published_at: str
|
||||
|
||||
|
||||
class PauseBody(BaseModel):
|
||||
paused: bool
|
||||
|
||||
@@ -204,6 +211,34 @@ def put_users_study_settings(
|
||||
)
|
||||
|
||||
|
||||
@admin_router.get("/notices")
|
||||
def get_notices(db: Session = Depends(get_db), _: str = Depends(require_admin)) -> list[dict]:
|
||||
from admin.notices import list_notices
|
||||
|
||||
return list_notices(db)
|
||||
|
||||
|
||||
@admin_router.post("/notices")
|
||||
def post_notice(body: NoticeBody, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
|
||||
from admin.notices import create_notice
|
||||
|
||||
return create_notice(db, body.title, body.body, body.link_url, body.published_at)
|
||||
|
||||
|
||||
@admin_router.put("/notices/{notice_id}")
|
||||
def put_notice(notice_id: int, body: NoticeBody, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
|
||||
from admin.notices import update_notice
|
||||
|
||||
return update_notice(db, notice_id, body.title, body.body, body.link_url, body.published_at)
|
||||
|
||||
|
||||
@admin_router.delete("/notices/{notice_id}")
|
||||
def remove_notice(notice_id: int, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
|
||||
from admin.notices import delete_notice
|
||||
|
||||
return delete_notice(db, notice_id)
|
||||
|
||||
|
||||
@admin_router.get("/users/{employee_id}/credentials")
|
||||
def get_credentials(
|
||||
employee_id: str, response: Response, db: Session = Depends(get_db), _: str = Depends(require_admin)
|
||||
|
||||
@@ -144,7 +144,7 @@ def heartbeat_intervals() -> dict:
|
||||
|
||||
|
||||
def hours_source_of(row: SystemConfig | None = None) -> str:
|
||||
"""api:接口本月学时;history:学时历史本月分钟合计。"""
|
||||
"""管理端为新用户准备的默认依据。api:官方统计;history:历史统计。"""
|
||||
if row is None:
|
||||
from euniv.db import SessionLocal
|
||||
|
||||
@@ -154,6 +154,25 @@ def hours_source_of(row: SystemConfig | None = None) -> str:
|
||||
return value if value in ("api", "history") else "api"
|
||||
|
||||
|
||||
def account_hours_source(account) -> str:
|
||||
value = str(getattr(account, "hours_source", "") or "").strip().lower()
|
||||
return value if value in ("api", "history") else hours_source_of()
|
||||
|
||||
|
||||
def source_for_employee(employee_id: str) -> str:
|
||||
from euniv.db import SessionLocal
|
||||
from euniv.models import Account
|
||||
|
||||
key = (employee_id or "").strip()
|
||||
if not key:
|
||||
return hours_source_of()
|
||||
with SessionLocal() as db:
|
||||
account = db.get(Account, key)
|
||||
if account is None:
|
||||
return hours_source_of()
|
||||
return account_hours_source(account)
|
||||
|
||||
|
||||
def settings_view(row: SystemConfig) -> dict:
|
||||
return {
|
||||
"refresh_interval_minutes": int(row.refresh_interval_minutes),
|
||||
@@ -234,7 +253,6 @@ def save_settings(db: Session, payload: dict) -> SystemConfig:
|
||||
row = load_config(db)
|
||||
previous_interval = int(row.refresh_interval_minutes)
|
||||
previous_username = row.admin_username
|
||||
previous_source = hours_source_of(row)
|
||||
row.refresh_interval_minutes = interval
|
||||
row.show_manual_jigsaw = bool(payload.get("show_manual_jigsaw"))
|
||||
row.manual_preference = bool(payload.get("manual_preference"))
|
||||
@@ -261,8 +279,4 @@ def save_settings(db: Session, payload: dict) -> SystemConfig:
|
||||
from euniv.service import notify_refresh_interval_changed
|
||||
|
||||
notify_refresh_interval_changed()
|
||||
if source != previous_source:
|
||||
from euniv.service import notify_hours_source_changed
|
||||
|
||||
notify_hours_source_changed()
|
||||
return row
|
||||
|
||||
@@ -62,10 +62,9 @@ def _iso_time(value: datetime | None) -> str | None:
|
||||
|
||||
|
||||
def list_users(db: Session) -> list[dict]:
|
||||
from admin.settings import hours_source_of
|
||||
from admin.settings import account_hours_source
|
||||
from euniv.autostudy import autostudy_status, work_running
|
||||
|
||||
source = hours_source_of()
|
||||
accounts = db.scalars(select(Account).order_by(Account.employee_id)).all()
|
||||
live_users = autostudy_status().get("users") or {}
|
||||
rows = []
|
||||
@@ -85,7 +84,7 @@ def list_users(db: Session) -> list[dict]:
|
||||
"credential_valid": _optional_bool(account.credential_valid),
|
||||
"month_hours": account.month_hours or "",
|
||||
"official_month_hours": account.official_month_hours or "",
|
||||
"hours_source": source,
|
||||
"hours_source": account_hours_source(account),
|
||||
"monthly_target": account.monthly_target if account.monthly_target is not None else 600,
|
||||
"has_proxy": enabled(account),
|
||||
"proxy": summary_of(account),
|
||||
@@ -116,7 +115,7 @@ def _require(db: Session, employee_id: str) -> Account:
|
||||
|
||||
|
||||
def _checked_hours(client) -> tuple[str, str]:
|
||||
from admin.settings import hours_source_of
|
||||
from admin.settings import source_for_employee
|
||||
from euniv.service import _month_hours
|
||||
|
||||
try:
|
||||
@@ -124,7 +123,7 @@ def _checked_hours(client) -> tuple[str, str]:
|
||||
except Exception:
|
||||
hours = ""
|
||||
official = ""
|
||||
if hours and hours_source_of() == "history":
|
||||
if hours and source_for_employee(str(getattr(client, "login_name", "") or "")) == "history":
|
||||
reader = getattr(client, "month_hours", None)
|
||||
if callable(reader):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user