You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.7 KiB
43 lines
1.7 KiB
|
4 weeks ago
|
"""用户权限 ORM 模型 - User / Session"""
|
||
|
|
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
from sqlalchemy import Boolean, DateTime, Integer, String
|
||
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
||
|
|
|
||
|
|
from app.database import Base
|
||
|
|
|
||
|
|
|
||
|
|
class User(Base):
|
||
|
|
"""用户表。"""
|
||
|
|
|
||
|
|
__tablename__ = "users"
|
||
|
|
|
||
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||
|
|
username: Mapped[str] = mapped_column(String(50), unique=True, nullable=False, index=True)
|
||
|
|
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
||
|
|
email: Mapped[str | None] = mapped_column(String(100), unique=True, nullable=True)
|
||
|
|
role: Mapped[str] = mapped_column(String(20), nullable=False, default="user")
|
||
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
||
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||
|
|
last_login: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
||
|
|
|
||
|
|
def __repr__(self) -> str:
|
||
|
|
return f"<User(id={self.id}, username='{self.username}', role='{self.role}')>"
|
||
|
|
|
||
|
|
|
||
|
|
class Session(Base):
|
||
|
|
"""用户会话表。"""
|
||
|
|
|
||
|
|
__tablename__ = "sessions"
|
||
|
|
|
||
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||
|
|
user_id: Mapped[int] = mapped_column(Integer, nullable=False, index=True)
|
||
|
|
token: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
|
||
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||
|
|
expires_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
|
||
|
|
is_valid: Mapped[bool] = mapped_column(Boolean, default=True)
|
||
|
|
|
||
|
|
def __repr__(self) -> str:
|
||
|
|
return f"<Session(id={self.id}, user_id={self.user_id})>"
|