fix: 补上未进子任务的首页、课程筛选和客户端构造

This commit is contained in:
2026-11-16 11:54:10 +08:00
parent 86a7477fab
commit 0d7de2eac3
14 changed files with 185 additions and 136 deletions
+13 -4
View File
@@ -13,6 +13,8 @@ from sqlalchemy.orm import Session
from euniv.client import EUNIV
from euniv.models import Account, Course, CourseSync
UNCATEGORIZED = "__none__"
_lock = threading.Lock()
_running = False
_message = ""
@@ -65,19 +67,26 @@ def course_message() -> str:
return _message
def _like_pattern(value: str) -> str:
escaped = value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
return f"%{escaped}%"
def course_page(db: Session, query: str, offset: int, limit: int, kind: str = "") -> dict:
text = query.strip()
base = []
if text:
like = f"%{text}%"
base.append(or_(Course.title.like(like), Course.contributor.like(like)))
like = _like_pattern(text)
base.append(or_(Course.title.like(like, escape="\\"), Course.contributor.like(like, escape="\\")))
counts = db.execute(select(Course.kind, func.count()).where(*base).group_by(Course.kind)).all()
kinds = [
{"name": name or "未分类", "value": name, "count": int(count)}
{"name": name or "未分类", "value": name if name else UNCATEGORIZED, "count": int(count)}
for name, count in sorted(counts, key=lambda row: (_kind_rank(row[0]), row[0]))
]
filters = list(base)
if kind:
if kind == UNCATEGORIZED:
filters.append(Course.kind == "")
elif kind:
filters.append(Course.kind == kind)
total = db.scalar(select(func.count()).select_from(Course).where(*filters)) or 0
rows = db.scalars(