from datetime import datetime, timezone from sqlalchemy import DateTime, Index, Integer, String, func from sqlalchemy.orm import Mapped, mapped_column from app.db import Base def _utcnow() -> datetime: return datetime.now(timezone.utc).replace(tzinfo=None) class AdminAuditLog(Base): __tablename__ = "admin_audit_log" __table_args__ = ( Index("ix_admin_audit_log_username", "username"), Index("ix_admin_audit_log_created_at", "created_at"), ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) user_id: Mapped[int | None] = mapped_column(Integer, nullable=True) username: Mapped[str | None] = mapped_column(String(64), nullable=True) method: Mapped[str] = mapped_column(String(16), nullable=False) path: Mapped[str] = mapped_column(String(255), nullable=False) status_code: Mapped[int] = mapped_column(Integer, nullable=False) ip: Mapped[str] = mapped_column(String(64), nullable=False, default="", server_default="") created_at: Mapped[datetime] = mapped_column( DateTime, nullable=False, default=_utcnow, server_default=func.now(), ) class AdminIpBlacklist(Base): __tablename__ = "admin_ip_blacklist" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) ip: Mapped[str] = mapped_column(String(64), unique=True, nullable=False) remark: Mapped[str] = mapped_column(String(255), nullable=False, default="", server_default="") created_at: Mapped[datetime] = mapped_column( DateTime, nullable=False, default=_utcnow, server_default=func.now(), )