""" Sharing models — cross-user access to heaps and folders. HeapShare grants another user read or read+write access to a heap. FolderShare does the same for a folder (or source root). """ import uuid from sqlalchemy import ( Column, DateTime, ForeignKey, Index, String, UniqueConstraint, func, ) from app.database import Base class HeapShare(Base): __tablename__ = "heap_shares" id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4())) heap_id = Column( String, ForeignKey("heaps.id", ondelete="CASCADE"), nullable=False, ) # Denormalized from heap.user_id for fast "shares I own" lookups. owner_id = Column(String, ForeignKey("users.id"), nullable=False) shared_with_id = Column(String, ForeignKey("users.id"), nullable=False) permission = Column(String, nullable=False, default="read") # 'read' | 'write' created_at = Column(DateTime, server_default=func.now()) __table_args__ = ( UniqueConstraint("heap_id", "shared_with_id", name="uq_heap_share"), Index("ix_heap_shares_shared_with", "shared_with_id"), Index("ix_heap_shares_heap_id", "heap_id"), ) class FolderShare(Base): __tablename__ = "folder_shares" id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4())) # Can reference either a Folder.id or a SourceRoot.id. folder_id = Column(String, nullable=False) folder_type = Column(String, nullable=False, default="folder") # 'folder' | 'source_root' owner_id = Column(String, ForeignKey("users.id"), nullable=False) shared_with_id = Column(String, ForeignKey("users.id"), nullable=False) permission = Column(String, nullable=False, default="read") # 'read' | 'write' created_at = Column(DateTime, server_default=func.now()) __table_args__ = ( UniqueConstraint("folder_id", "shared_with_id", name="uq_folder_share"), Index("ix_folder_shares_shared_with", "shared_with_id"), Index("ix_folder_shares_folder_id", "folder_id"), )