33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import String, DateTime, Boolean, ForeignKey, func
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy import JSON
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class View(Base):
|
|
__tablename__ = "views"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
table_id: Mapped[int] = mapped_column(
|
|
ForeignKey("tables.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
type: Mapped[str] = mapped_column(String(50), default="grid") # grid, gallery, form
|
|
config: Mapped[Optional[dict]] = mapped_column(
|
|
JSON, nullable=True
|
|
) # filter, sort, columns
|
|
is_default: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
created_by: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=func.now())
|
|
|
|
# Relationships
|
|
table = relationship("Table", back_populates="views")
|
|
created_by_user = relationship("User", back_populates="views")
|
|
|
|
def __repr__(self):
|
|
return f"<View(id={self.id}, name={self.name}, type={self.type})>"
|