企業內部新聞彙整與分析系統 - 自動新聞抓取 (Digitimes, 經濟日報, 工商時報) - AI 智慧摘要 (OpenAI/Claude/Ollama) - 群組管理與訂閱通知 - 已清理 Python 快取檔案 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
60 lines
2.7 KiB
Python
60 lines
2.7 KiB
Python
"""
|
|
用戶與角色資料模型
|
|
"""
|
|
from datetime import datetime
|
|
from sqlalchemy import String, Boolean, ForeignKey, Text, Enum as SQLEnum
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from typing import Optional, List
|
|
import enum
|
|
|
|
from app.db.session import Base
|
|
|
|
|
|
class AuthType(str, enum.Enum):
|
|
"""認證類型"""
|
|
AD = "ad"
|
|
LOCAL = "local"
|
|
|
|
|
|
class Role(Base):
|
|
"""角色表"""
|
|
__tablename__ = "roles"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
code: Mapped[str] = mapped_column(String(20), unique=True, nullable=False, comment="角色代碼")
|
|
name: Mapped[str] = mapped_column(String(50), nullable=False, comment="角色名稱")
|
|
description: Mapped[Optional[str]] = mapped_column(String(200), comment="角色描述")
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# 關聯
|
|
users: Mapped[List["User"]] = relationship(back_populates="role")
|
|
|
|
|
|
class User(Base):
|
|
"""用戶表"""
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
username: Mapped[str] = mapped_column(String(50), unique=True, nullable=False, index=True, comment="用戶帳號")
|
|
password_hash: Mapped[Optional[str]] = mapped_column(String(255), comment="密碼雜湊")
|
|
display_name: Mapped[str] = mapped_column(String(100), nullable=False, comment="顯示名稱")
|
|
email: Mapped[Optional[str]] = mapped_column(String(100), comment="電子郵件")
|
|
auth_type: Mapped[AuthType] = mapped_column(SQLEnum(AuthType), default=AuthType.LOCAL, nullable=False, comment="認證類型")
|
|
role_id: Mapped[int] = mapped_column(ForeignKey("roles.id"), nullable=False, comment="角色ID")
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True, comment="是否啟用")
|
|
last_login_at: Mapped[Optional[datetime]] = mapped_column(comment="最後登入時間")
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# 關聯
|
|
role: Mapped["Role"] = relationship(back_populates="users")
|
|
subscriptions: Mapped[List["Subscription"]] = relationship(back_populates="user", cascade="all, delete-orphan")
|
|
favorites: Mapped[List["Favorite"]] = relationship(back_populates="user", cascade="all, delete-orphan")
|
|
comments: Mapped[List["Comment"]] = relationship(back_populates="user")
|
|
notes: Mapped[List["Note"]] = relationship(back_populates="user", cascade="all, delete-orphan")
|
|
|
|
|
|
# 避免循環引入
|
|
from app.models.interaction import Subscription, Favorite, Comment, Note
|