|
| 1 | +""" |
| 2 | +SQLAlchemy ORM models for projects and project files. |
| 3 | +""" |
| 4 | + |
| 5 | +import uuid |
| 6 | +from datetime import datetime, timezone |
| 7 | + |
| 8 | +from sqlalchemy import JSON, BigInteger, DateTime, ForeignKey, String, Text |
| 9 | +from sqlalchemy.orm import Mapped, mapped_column, relationship |
| 10 | + |
| 11 | +from server.database import Base |
| 12 | + |
| 13 | + |
| 14 | +def _utcnow() -> datetime: |
| 15 | + return datetime.now(timezone.utc) |
| 16 | + |
| 17 | + |
| 18 | +def _new_uuid() -> str: |
| 19 | + return str(uuid.uuid4()) |
| 20 | + |
| 21 | + |
| 22 | +class Project(Base): |
| 23 | + """A BIM validation project containing IFC/BCF/IDS files.""" |
| 24 | + |
| 25 | + __tablename__ = "projects" |
| 26 | + |
| 27 | + id: Mapped[str] = mapped_column( |
| 28 | + String(36), primary_key=True, default=_new_uuid |
| 29 | + ) |
| 30 | + name: Mapped[str] = mapped_column(String(255), nullable=False) |
| 31 | + description: Mapped[str | None] = mapped_column(Text, nullable=True) |
| 32 | + created_at: Mapped[datetime] = mapped_column( |
| 33 | + DateTime(timezone=True), default=_utcnow |
| 34 | + ) |
| 35 | + updated_at: Mapped[datetime] = mapped_column( |
| 36 | + DateTime(timezone=True), default=_utcnow, onupdate=_utcnow |
| 37 | + ) |
| 38 | + |
| 39 | + files: Mapped[list["ProjectFile"]] = relationship( |
| 40 | + back_populates="project", |
| 41 | + cascade="all, delete-orphan", |
| 42 | + lazy="selectin", |
| 43 | + ) |
| 44 | + |
| 45 | + def to_dict(self) -> dict: |
| 46 | + return { |
| 47 | + "id": self.id, |
| 48 | + "name": self.name, |
| 49 | + "description": self.description, |
| 50 | + "createdAt": self.created_at.isoformat(), |
| 51 | + "updatedAt": self.updated_at.isoformat(), |
| 52 | + "files": [f.to_dict() for f in self.files], |
| 53 | + } |
| 54 | + |
| 55 | + def to_summary(self) -> dict: |
| 56 | + return { |
| 57 | + "id": self.id, |
| 58 | + "name": self.name, |
| 59 | + "description": self.description, |
| 60 | + "createdAt": self.created_at.isoformat(), |
| 61 | + "updatedAt": self.updated_at.isoformat(), |
| 62 | + "fileCount": len(self.files), |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | +class ProjectFile(Base): |
| 67 | + """A file (IFC, BCF, or IDS) belonging to a project.""" |
| 68 | + |
| 69 | + __tablename__ = "project_files" |
| 70 | + |
| 71 | + id: Mapped[str] = mapped_column( |
| 72 | + String(36), primary_key=True, default=_new_uuid |
| 73 | + ) |
| 74 | + project_id: Mapped[str] = mapped_column( |
| 75 | + String(36), ForeignKey("projects.id", ondelete="CASCADE"), nullable=False |
| 76 | + ) |
| 77 | + file_type: Mapped[str] = mapped_column( |
| 78 | + String(10), nullable=False |
| 79 | + ) # 'ifc', 'bcf', 'ids' |
| 80 | + file_name: Mapped[str] = mapped_column(String(255), nullable=False) |
| 81 | + file_size: Mapped[int] = mapped_column(BigInteger, nullable=False) |
| 82 | + disk_path: Mapped[str] = mapped_column(String(512), nullable=False) |
| 83 | + uploaded_at: Mapped[datetime] = mapped_column( |
| 84 | + DateTime(timezone=True), default=_utcnow |
| 85 | + ) |
| 86 | + metadata_json: Mapped[dict | None] = mapped_column( |
| 87 | + "metadata", JSON, nullable=True |
| 88 | + ) |
| 89 | + |
| 90 | + project: Mapped["Project"] = relationship(back_populates="files") |
| 91 | + |
| 92 | + def to_dict(self) -> dict: |
| 93 | + return { |
| 94 | + "id": self.id, |
| 95 | + "projectId": self.project_id, |
| 96 | + "fileType": self.file_type, |
| 97 | + "fileName": self.file_name, |
| 98 | + "fileSize": self.file_size, |
| 99 | + "uploadedAt": self.uploaded_at.isoformat(), |
| 100 | + "metadata": self.metadata_json, |
| 101 | + } |
0 commit comments