Archived
- web/routers/index.py:GET / 首页,用 Jinja2 以 Flask 同口径渲染 (keep_trailing_newline=False)——输出与 Flask render_template 逐字节一致 - web/app.py:挂载 /static 静态资源(site_auth.js 等,与 Flask 同目录) - web/routers/auth_status.py:/api/v1/auth/me 认证状态 · user/duration_text/announcement/smtp_configured/register_credits 逐字段对齐 - core/repo.py:补 get_announcement / get_smtp_config / smtp_configured 异步读 契约奇偶校验:28 项全 OK(新增 index 逐字节、auth:me); 验证:pytest 51/51;ruff All checks passed;pyright 0 errors; uvicorn 实跑 / 与 /static 与 /api/v1/* 均 200。
84 lines
2.9 KiB
Python
84 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""站点数据异步读写仓储(FastAPI 栈用)。
|
|
|
|
直接读写 auth_system 建立的同一份 SQLite(同一 DB_PATH / 同一 schema),
|
|
但走 core.db 的 aiosqlite 异步层——读并发、写串行、WAL 读写同时进行。
|
|
|
|
设计约束:本模块**不改动** auth_system 既有 schema,只做读写;字段口径与
|
|
auth_system 保持一致(键名、默认值)。迁移期两条栈共存、共库。
|
|
"""
|
|
from typing import Any
|
|
|
|
from core import db
|
|
|
|
# ---------- settings ----------
|
|
|
|
async def get_setting(key: str, default: str = '') -> str:
|
|
"""读单个站点设置(对应 auth_system.get_setting)。"""
|
|
row = await db.fetch_one('SELECT value FROM settings WHERE key = ?', (key,))
|
|
return row['value'] if row is not None else default
|
|
|
|
|
|
async def set_setting(key: str, value: str) -> None:
|
|
"""写站点设置(upsert)。"""
|
|
await db.execute(
|
|
'INSERT INTO settings (key, value) VALUES (?, ?) '
|
|
'ON CONFLICT(key) DO UPDATE SET value = excluded.value',
|
|
(key, value),
|
|
)
|
|
|
|
|
|
async def all_settings() -> dict[str, str]:
|
|
rows = await db.fetch_all('SELECT key, value FROM settings')
|
|
return {r['key']: r['value'] for r in rows}
|
|
|
|
|
|
# ---------- announcements ----------
|
|
|
|
async def list_announcements(limit: int = 20) -> list[dict[str, Any]]:
|
|
rows = await db.fetch_all(
|
|
'SELECT * FROM announcements ORDER BY rowid DESC LIMIT ?', (limit,))
|
|
return [dict(r) for r in rows]
|
|
|
|
|
|
# ---------- users(只读统计;写操作请走 auth_system 的事务口径) ----------
|
|
|
|
async def count_users(role: str | None = None) -> int:
|
|
if role:
|
|
return int(await db.fetch_val('SELECT COUNT(*) FROM users WHERE role = ?', (role,), 0))
|
|
return int(await db.fetch_val('SELECT COUNT(*) FROM users', (), 0))
|
|
|
|
|
|
async def get_user_by_name(username: str) -> dict[str, Any] | None:
|
|
row = await db.fetch_one('SELECT * FROM users WHERE username = ?', (username,))
|
|
return dict(row) if row is not None else None
|
|
|
|
|
|
async def get_user_by_id(uid: int) -> dict[str, Any] | None:
|
|
row = await db.fetch_one('SELECT * FROM users WHERE id = ?', (uid,))
|
|
return dict(row) if row is not None else None
|
|
|
|
|
|
# ---------- 公告 / SMTP 配置(读) ----------
|
|
|
|
async def get_announcement() -> dict[str, Any]:
|
|
row = await db.fetch_one('SELECT * FROM announcements WHERE id = 1')
|
|
return dict(row) if row else {'title': '', 'content': '', 'popup_mode': 'off'}
|
|
|
|
|
|
async def get_smtp_config() -> dict[str, Any]:
|
|
row = await db.fetch_one('SELECT * FROM smtp_config WHERE id = 1')
|
|
return dict(row) if row else {}
|
|
|
|
|
|
async def smtp_configured() -> bool:
|
|
cfg = await get_smtp_config()
|
|
return bool(cfg.get('host') and cfg.get('sender') and cfg.get('password'))
|
|
|
|
|
|
# ---------- billing ledger(只读对账) ----------
|
|
|
|
async def ledger_open_count() -> int:
|
|
return int(await db.fetch_val(
|
|
"SELECT COUNT(*) FROM billing_ledger WHERE status = 'open'", (), 0))
|