Initial commit: Rentman Clone - Phase 0-6 (T001-T023)

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
This commit is contained in:
Agent Zero
2026-05-31 20:36:42 +00:00
commit 7f7da15965
135 changed files with 18980 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
"""Database models."""
from app.models.account import Account
from app.models.user import User
from app.models.role import Role
from app.models.contact import Contact
from app.models.tag import Tag
from app.models.equipment import Equipment
from app.models.stock_location import StockLocation
from app.models.equipment_group import EquipmentGroup
from app.models.crew import Crew, CrewAvailability
from app.models.vehicle import Vehicle, VehicleAssignment
from app.models.project import Project, SubProject, ProjectFunctionGroup, ProjectFunction
__all__ = [
"Account",
"User",
"Role",
"Contact",
"Tag",
"Equipment",
"StockLocation",
"EquipmentGroup",
"Crew",
"CrewAvailability",
"Vehicle",
"VehicleAssignment",
"Project",
"SubProject",
"ProjectFunctionGroup",
"ProjectFunction",
]
+34
View File
@@ -0,0 +1,34 @@
"""Account (Tenant) model."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class Account(Base):
"""Represents a tenant/company account."""
__tablename__ = "accounts"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
# Relationships
users: Mapped[list["User"]] = relationship("User", back_populates="account")
roles: Mapped[list["Role"]] = relationship("Role", back_populates="account")
contacts: Mapped[list["Contact"]] = relationship("Contact", back_populates="account")
tags: Mapped[list["Tag"]] = relationship("Tag", back_populates="account")
equipment: Mapped[list["Equipment"]] = relationship("Equipment", back_populates="account")
stock_locations: Mapped[list["StockLocation"]] = relationship("StockLocation", back_populates="account")
equipment_groups: Mapped[list["EquipmentGroup"]] = relationship("EquipmentGroup", back_populates="account")
crew_members: Mapped[list["Crew"]] = relationship("Crew", back_populates="account")
projects: Mapped[list["Project"]] = relationship("Project", back_populates="account")
+85
View File
@@ -0,0 +1,85 @@
"""Contact model for companies and persons."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, ForeignKey, func, Table, Column
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
# Many-to-many association table: contacts ↔ tags
contact_tags = Table(
"contact_tags",
Base.metadata,
Column(
"contact_id",
String(36),
ForeignKey("contacts.id", ondelete="CASCADE"),
primary_key=True,
),
Column(
"tag_id",
String(36),
ForeignKey("tags.id", ondelete="CASCADE"),
primary_key=True,
),
)
class Contact(Base):
"""Represents a contact (company or person) within a tenant account."""
__tablename__ = "contacts"
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
)
type: Mapped[str] = mapped_column(
String(10), nullable=False
) # 'company' or 'person'
company_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
first_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
last_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
email: Mapped[str | None] = mapped_column(String(255), nullable=True)
phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
mobile: Mapped[str | None] = mapped_column(String(50), nullable=True)
website: Mapped[str | None] = mapped_column(String(500), nullable=True)
# Billing address
billing_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
billing_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
billing_postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
billing_city: Mapped[str | None] = mapped_column(String(100), nullable=True)
billing_country: Mapped[str | None] = mapped_column(String(100), nullable=True)
# Shipping address
shipping_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
shipping_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
shipping_postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
shipping_city: Mapped[str | None] = mapped_column(String(100), nullable=True)
shipping_country: Mapped[str | None] = mapped_column(String(100), nullable=True)
# Additional fields from design.md
tax_number: Mapped[str | None] = mapped_column(String(50), nullable=True)
note: Mapped[str | None] = mapped_column(String, nullable=True)
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="contacts")
tags: Mapped[list["Tag"]] = relationship(
"Tag", secondary=contact_tags, back_populates="contacts"
)
# projects_as_customer relationship will be added in Phase 3 when Project model is created
+88
View File
@@ -0,0 +1,88 @@
"""Crew and CrewAvailability models for managing personnel."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, ForeignKey, func, Float, Text, Boolean
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class Crew(Base):
"""Represents a crew member within a tenant account."""
__tablename__ = "crew"
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
)
first_name: Mapped[str] = mapped_column(String(100), nullable=False)
last_name: Mapped[str] = mapped_column(String(100), nullable=False)
email: Mapped[str | None] = mapped_column(String(255), nullable=True)
phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
role_title: Mapped[str | None] = mapped_column(String(100), nullable=True)
hourly_rate: Mapped[float | None] = mapped_column(Float, nullable=True)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
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="crew_members")
availabilities: Mapped[list["CrewAvailability"]] = relationship(
"CrewAvailability", back_populates="crew_member", cascade="all, delete-orphan"
)
def __repr__(self) -> str:
return f"<Crew {self.first_name} {self.last_name}>"
class CrewAvailability(Base):
"""Tracks crew availability time periods."""
__tablename__ = "crew_availabilities"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
crew_id: Mapped[str] = mapped_column(
String(36), ForeignKey("crew.id", ondelete="CASCADE"), nullable=False
)
start_date: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
end_date: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
status: Mapped[str] = mapped_column(
String(20), nullable=False, default="available"
) # available, booked, unavailable
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
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
crew_member: Mapped["Crew"] = relationship("Crew", back_populates="availabilities")
def __repr__(self) -> str:
return f"<CrewAvailability {self.crew_id}: {self.start_date}-{self.end_date}>"
+70
View File
@@ -0,0 +1,70 @@
"""Equipment model for the rental inventory catalog."""
import uuid
from datetime import datetime
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 Equipment(Base):
"""Represents a piece of rental equipment within a tenant account."""
__tablename__ = "equipment"
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)
category: Mapped[str | None] = mapped_column(String(100), nullable=True)
brand: Mapped[str | None] = mapped_column(String(255), nullable=True)
serial_number: Mapped[str | None] = mapped_column(String(100), nullable=True, unique=True)
barcode: Mapped[str | None] = mapped_column(String(100), nullable=True, unique=True)
qr_code: Mapped[str | None] = mapped_column(String(500), nullable=True)
status: Mapped[str] = mapped_column(
String(20), nullable=False, default="available"
) # available, rented, maintenance, retired
purchase_price: Mapped[float | None] = mapped_column(Float, nullable=True)
current_value: Mapped[float | None] = mapped_column(Float, nullable=True)
weight_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
dimensions: Mapped[str | None] = mapped_column(String(255), nullable=True)
power_watt: Mapped[float | None] = mapped_column(Float, nullable=True)
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
# Location reference
location_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("stock_locations.id", ondelete="SET NULL"), nullable=True
)
# Supplier reference (optional contact)
supplier_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("contacts.id", ondelete="SET NULL"), nullable=True
)
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="equipment")
location: Mapped["StockLocation"] = relationship(
"StockLocation", back_populates="equipment_items"
)
supplier: Mapped["Contact"] = relationship("Contact", foreign_keys=[supplier_id])
def __repr__(self) -> str:
return f"<Equipment {self.name} ({self.status})>"
+74
View File
@@ -0,0 +1,74 @@
"""EquipmentGroup (Bundle) model for grouping equipment items."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, ForeignKey, func, Float, Text, Table, Column
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
# Many-to-many association table: equipment_groups ↔ equipment
equipment_group_items = Table(
"equipment_group_items",
Base.metadata,
Column(
"group_id",
String(36),
ForeignKey("equipment_groups.id", ondelete="CASCADE"),
primary_key=True,
),
Column(
"equipment_id",
String(36),
ForeignKey("equipment.id", ondelete="CASCADE"),
primary_key=True,
),
Column(
"quantity",
Float,
nullable=False,
default=1.0,
),
)
class EquipmentGroup(Base):
"""Represents a bundle/group of equipment items."""
__tablename__ = "equipment_groups"
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)
daily_rate: Mapped[float | None] = mapped_column(Float, nullable=True)
default_location_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("stock_locations.id", ondelete="SET NULL"), nullable=True
)
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="equipment_groups")
default_location: Mapped["StockLocation"] = relationship(
"StockLocation", back_populates="equipment_groups"
)
items: Mapped[list["Equipment"]] = relationship(
"Equipment", secondary=equipment_group_items, backref="groups"
)
def __repr__(self) -> str:
return f"<EquipmentGroup {self.name}>"
+149
View File
@@ -0,0 +1,149 @@
"""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}>"
+28
View File
@@ -0,0 +1,28 @@
"""Role model for RBAC."""
import uuid
from sqlalchemy import String, ForeignKey, JSON
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class Role(Base):
"""Represents a role with permissions within an account."""
__tablename__ = "roles"
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(100), nullable=False)
description: Mapped[str | None] = mapped_column(String(500), nullable=True)
permissions: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
# Relationships
account: Mapped["Account"] = relationship("Account", back_populates="roles")
users: Mapped[list["User"]] = relationship("User", back_populates="role")
+47
View File
@@ -0,0 +1,47 @@
"""Stock location model for equipment storage locations."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, ForeignKey, func, Boolean
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class StockLocation(Base):
"""Represents a physical storage location for equipment."""
__tablename__ = "stock_locations"
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)
address: Mapped[str | None] = mapped_column(String(500), nullable=True)
is_default: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
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="stock_locations")
equipment_items: Mapped[list["Equipment"]] = relationship(
"Equipment", back_populates="location"
)
equipment_groups: Mapped[list["EquipmentGroup"]] = relationship(
"EquipmentGroup", back_populates="default_location"
)
def __repr__(self) -> str:
return f"<StockLocation {self.name}>"
+34
View File
@@ -0,0 +1,34 @@
"""Tag model for categorizing entities."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, ForeignKey, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
from app.models.contact import contact_tags
class Tag(Base):
"""Represents a tag within a tenant account for categorizing entities."""
__tablename__ = "tags"
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(100), nullable=False)
color: Mapped[str | None] = mapped_column(String(7), nullable=True) # hex color
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
# Relationships
account: Mapped["Account"] = relationship("Account", back_populates="tags")
contacts: Mapped[list["Contact"]] = relationship(
"Contact", secondary=contact_tags, back_populates="tags"
)
+39
View File
@@ -0,0 +1,39 @@
"""User model."""
import uuid
from datetime import datetime
from sqlalchemy import String, Boolean, DateTime, ForeignKey, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class User(Base):
"""Represents a user within a tenant account."""
__tablename__ = "users"
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
)
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False)
full_name: Mapped[str] = mapped_column(String(255), nullable=False)
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
role_id: Mapped[str | None] = mapped_column(
String(36), ForeignKey("roles.id", ondelete="SET NULL"), nullable=True
)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
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="users")
role: Mapped["Role | None"] = relationship("Role", back_populates="users")
+95
View File
@@ -0,0 +1,95 @@
"""Vehicle and VehicleAssignment models for fleet management."""
import uuid
from datetime import datetime
from sqlalchemy import String, DateTime, ForeignKey, func, Float, Text, Boolean
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class Vehicle(Base):
"""Represents a vehicle within a tenant account."""
__tablename__ = "vehicles"
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)
license_plate: Mapped[str | None] = mapped_column(String(50), nullable=True, unique=True)
brand: Mapped[str | None] = mapped_column(String(100), nullable=True)
model: Mapped[str | None] = mapped_column(String(100), nullable=True)
year: Mapped[int | None] = mapped_column(nullable=True)
color: Mapped[str | None] = mapped_column(String(50), nullable=True)
vehicle_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
payload_capacity_kg: Mapped[float | None] = mapped_column(Float, nullable=True)
load_volume_m3: Mapped[float | None] = mapped_column(Float, nullable=True)
fuel_type: Mapped[str | None] = mapped_column(String(30), nullable=True)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
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="vehicles")
assignments: Mapped[list["VehicleAssignment"]] = relationship(
"VehicleAssignment", back_populates="vehicle", cascade="all, delete-orphan"
)
def __repr__(self) -> str:
return f"<Vehicle {self.name} ({self.license_plate or 'no plate'})>"
class VehicleAssignment(Base):
"""Tracks vehicle assignment to projects/events."""
__tablename__ = "vehicle_assignments"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
vehicle_id: Mapped[str] = mapped_column(
String(36), ForeignKey("vehicles.id", ondelete="CASCADE"), nullable=False
)
project_id: Mapped[str | None] = mapped_column(
String(36), nullable=True
)
start_date: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
end_date: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
status: Mapped[str] = mapped_column(
String(20), nullable=False, default="assigned"
) # assigned, in_use, returned
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
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
vehicle: Mapped["Vehicle"] = relationship("Vehicle", back_populates="assignments")
def __repr__(self) -> str:
return f"<VehicleAssignment {self.vehicle_id}: {self.start_date}-{self.end_date}>"