Files
2026-11-18 18:06:22 +05:30

77 lines
4.1 KiB
Python

from datetime import date, datetime
from typing import Optional
from sqlalchemy import Boolean, Date, DateTime, ForeignKey, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base
from ..core.time_utils import utc_now
class Attachment(Base):
__tablename__ = "attachments"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
entity_type: Mapped[str] = mapped_column(String(60), index=True, nullable=False)
entity_id: Mapped[int] = mapped_column(Integer, index=True, nullable=False)
original_name: Mapped[str] = mapped_column(String(255), nullable=False)
stored_name: Mapped[str] = mapped_column(String(255), nullable=False)
mime_type: Mapped[str] = mapped_column(String(120), nullable=False)
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False)
uploaded_by_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
uploaded_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False)
class Notification(Base):
__tablename__ = "notifications"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
type: Mapped[str] = mapped_column(String(60), index=True, nullable=False)
severity: Mapped[str] = mapped_column(String(20), nullable=False)
title: Mapped[str] = mapped_column(String(200), nullable=False)
message: Mapped[str] = mapped_column(Text, nullable=False)
entity_type: Mapped[str] = mapped_column(String(60), nullable=False)
entity_id: Mapped[int] = mapped_column(Integer, nullable=False)
due_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
target_role: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
target_user_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), nullable=True)
dedupe_key: Mapped[str] = mapped_column(String(200), unique=True, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, index=True, nullable=False)
class NotificationRead(Base):
__tablename__ = "notification_reads"
__table_args__ = (
UniqueConstraint("notification_id", "user_id", name="uq_notification_user_read"),
)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
notification_id: Mapped[int] = mapped_column(ForeignKey("notifications.id"), nullable=False)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
read_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False)
class AuditLog(Base):
__tablename__ = "audit_logs"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
entity_type: Mapped[str] = mapped_column(String(60), index=True, nullable=False)
entity_id: Mapped[int] = mapped_column(Integer, index=True, nullable=False)
action: Mapped[str] = mapped_column(String(50), nullable=False)
changes_json: Mapped[str] = mapped_column(Text, nullable=False)
by_user_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), index=True, nullable=True)
at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, index=True, nullable=False)
class AISummary(Base):
__tablename__ = "ai_summaries"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
section: Mapped[str] = mapped_column(String(50), index=True, nullable=False)
filters_hash: Mapped[str] = mapped_column(String(64), index=True, nullable=False)
data_hash: Mapped[str] = mapped_column(String(64), index=True, nullable=False)
source: Mapped[str] = mapped_column(String(30), nullable=False)
model: Mapped[Optional[str]] = mapped_column(String(120), nullable=True)
validated: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
payload_json: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, index=True, nullable=False)
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), index=True, nullable=True)