7f7da15965
Completed: - Phase 0: Project Setup (T001-T003) - Docker Compose, FastAPI skeleton, React SPA - Phase 1: Auth System (T004-T008) - DB models, JWT auth, RBAC middleware, user management - Phase 2: Contacts & Tags (T009-T011) - CRUD API + UI - Phase 3: Equipment Catalog (T012-T014) - Models, API, UI with barcode/QR - Phase 4: Crew Management (T015-T017) - Models, availability, UI - Phase 5: Vehicle Fleet (T018-T020) - Models, assignments, UI - Phase 6: Projects (T021-T023) - Project hierarchy models, CRUD API, list/detail UI
150 lines
5.5 KiB
Python
150 lines
5.5 KiB
Python
"""Project models for event/rental project management."""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import String, DateTime, ForeignKey, func, Float, Integer, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.db.base import Base
|
|
|
|
|
|
class Project(Base):
|
|
"""Represents a rental/event project within a tenant account."""
|
|
|
|
__tablename__ = "projects"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
account_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("accounts.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
status: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="draft"
|
|
) # draft, confirmed, in_progress, completed, cancelled
|
|
start_date: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
end_date: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
budget: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
total_costs: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
custom_fields: Mapped[str | None] = mapped_column(
|
|
Text, nullable=True
|
|
) # JSON string for flexible custom fields
|
|
custom_field_defs: Mapped[str | None] = mapped_column(
|
|
Text, nullable=True
|
|
) # JSON string for custom field definitions
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
server_default=func.now(),
|
|
onupdate=func.now(),
|
|
nullable=False,
|
|
)
|
|
|
|
# Relationships
|
|
account: Mapped["Account"] = relationship("Account", back_populates="projects")
|
|
sub_projects: Mapped[list["SubProject"]] = relationship(
|
|
"SubProject", back_populates="project", cascade="all, delete-orphan"
|
|
)
|
|
function_groups: Mapped[list["ProjectFunctionGroup"]] = relationship(
|
|
"ProjectFunctionGroup", back_populates="project", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<Project {self.name} ({self.status})>"
|
|
|
|
|
|
class SubProject(Base):
|
|
"""Represents a sub-project with self-referential parent hierarchy."""
|
|
|
|
__tablename__ = "sub_projects"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
project_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("projects.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
parent_id: Mapped[str | None] = mapped_column(
|
|
String(36), ForeignKey("sub_projects.id", ondelete="CASCADE"), nullable=True
|
|
)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
|
|
# Relationships
|
|
project: Mapped["Project"] = relationship("Project", back_populates="sub_projects")
|
|
parent: Mapped[Optional["SubProject"]] = relationship(
|
|
"SubProject", remote_side="SubProject.id", back_populates="children"
|
|
)
|
|
children: Mapped[list["SubProject"]] = relationship(
|
|
"SubProject", back_populates="parent", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<SubProject {self.name}>"
|
|
|
|
|
|
class ProjectFunctionGroup(Base):
|
|
"""Groups project functions under a project."""
|
|
|
|
__tablename__ = "project_function_groups"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
project_id: Mapped[str] = mapped_column(
|
|
String(36), ForeignKey("projects.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
|
|
# Relationships
|
|
project: Mapped["Project"] = relationship(
|
|
"Project", back_populates="function_groups"
|
|
)
|
|
functions: Mapped[list["ProjectFunction"]] = relationship(
|
|
"ProjectFunction", back_populates="function_group", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<ProjectFunctionGroup {self.name}>"
|
|
|
|
|
|
class ProjectFunction(Base):
|
|
"""Individual function/line-item within a function group."""
|
|
|
|
__tablename__ = "project_functions"
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
|
|
)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
function_group_id: Mapped[str] = mapped_column(
|
|
String(36),
|
|
ForeignKey("project_function_groups.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
)
|
|
quantity: Mapped[float] = mapped_column(Float, nullable=False, default=1.0)
|
|
daily_costs: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
total_costs: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
|
|
# Relationships
|
|
function_group: Mapped["ProjectFunctionGroup"] = relationship(
|
|
"ProjectFunctionGroup", back_populates="functions"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<ProjectFunction {self.name} x{self.quantity}>"
|