Source/SourceKind、UserRole、SubscriptionMatch 三个 enum 改用 values_callable 保证 PG 看到 'rss' / 'owner' / 'any' 等小写 value,而非 'RSS' 大写 name。 seed_sources 同步改为显式字符串。
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""用户模型。
|
|
|
|
Phase 1 仅 owner + member 两级,后续扩展。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import enum
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import Boolean, DateTime, Enum, String, func
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class UserRole(str, enum.Enum):
|
|
OWNER = "owner"
|
|
MEMBER = "member"
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
username: Mapped[str] = mapped_column(String(64), unique=True, index=True, nullable=False)
|
|
email: Mapped[str | None] = mapped_column(String(255), unique=True, index=True)
|
|
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
role: Mapped[UserRole] = mapped_column(
|
|
Enum(
|
|
UserRole,
|
|
name="user_role",
|
|
values_callable=lambda x: [e.value for e in x],
|
|
),
|
|
default=UserRole.MEMBER,
|
|
nullable=False,
|
|
)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
|
display_name: Mapped[str | None] = mapped_column(String(128))
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|
|
last_login_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<User id={self.id} username={self.username} role={self.role.value}>"
|