- alembic 0008:articles 加 is_short_news/external_id/source_ref/content_hash (UNIQUE);sources.kind 加 'api_push';api_tokens 加 purpose + source_id - SourceKind.API_PUSH enum;Article/ApiToken model 加新字段 - enrichment_article 短新闻跳过 format/image; enrichment_loop SQL 加 is_short_news 路径(并入'可 enrich' 条件) - 入库侧由 commit 2(ingest 接口)负责:写 body_zh_text=body_text, format/image/commentary_meituan_status='n/a', classify/commentary_status='pending'(带 tags 时 classify='ok') 无迁移爆炸半径:articles.url 保持 NOT NULL,短新闻合成 api-push:// 占位
40 lines
1.6 KiB
Python
40 lines
1.6 KiB
Python
"""API Token(给 Android + API Push 短新闻 ingest 用,可独立撤销)。"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, Integer, String, func
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.database import Base
|
|
|
|
|
|
# Token 用途
|
|
TOKEN_PURPOSE_MOBILE = "mobile" # Android 客户端
|
|
TOKEN_PURPOSE_INGEST = "ingest" # API Push 短新闻 /api/v1/ingest
|
|
|
|
|
|
class ApiToken(Base):
|
|
__tablename__ = "api_tokens"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(
|
|
ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
name: Mapped[str] = mapped_column(String(64), nullable=False) # "Xiaomi-14"
|
|
token_hash: Mapped[str] = mapped_column(String(128), unique=True, nullable=False, index=True)
|
|
# 只存 hash,原始 token 一次性返回给用户
|
|
purpose: Mapped[str] = mapped_column(
|
|
String(16), default=TOKEN_PURPOSE_MOBILE, nullable=False, index=True
|
|
)
|
|
# ingest 专用:绑定的 source_id(purpose=ingest 时使用,mobile 时为 NULL)
|
|
source_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("sources.id", ondelete="CASCADE"), nullable=True, index=True
|
|
)
|
|
last_used_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
revoked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|