104 lines
3.3 KiB
Python
104 lines
3.3 KiB
Python
"""公告的发布和读取。"""
|
|
|
|
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}
|