print_queue.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class PrintQueueItem(Base):
  6. """Print queue item for scheduled/queued prints."""
  7. __tablename__ = "print_queue"
  8. id: Mapped[int] = mapped_column(primary_key=True)
  9. # Links
  10. printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"))
  11. archive_id: Mapped[int] = mapped_column(ForeignKey("print_archives.id", ondelete="CASCADE"))
  12. project_id: Mapped[int | None] = mapped_column(ForeignKey("projects.id", ondelete="SET NULL"), nullable=True)
  13. # Scheduling
  14. position: Mapped[int] = mapped_column(Integer, default=0) # Queue order
  15. scheduled_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # None = ASAP
  16. manual_start: Mapped[bool] = mapped_column(Boolean, default=False) # Requires manual trigger to start
  17. # Conditions
  18. require_previous_success: Mapped[bool] = mapped_column(Boolean, default=False)
  19. # Power management
  20. auto_off_after: Mapped[bool] = mapped_column(Boolean, default=False) # Power off printer after print
  21. # AMS mapping: JSON array of global tray IDs for each filament slot
  22. # Format: "[5, -1, 2, -1]" where position = slot_id-1, value = global tray ID (-1 = unused)
  23. ams_mapping: Mapped[str | None] = mapped_column(Text, nullable=True)
  24. # Status: pending, printing, completed, failed, skipped, cancelled
  25. status: Mapped[str] = mapped_column(String(20), default="pending")
  26. # Tracking
  27. started_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  28. completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  29. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  30. # Timestamps
  31. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  32. # Relationships
  33. printer: Mapped["Printer"] = relationship()
  34. archive: Mapped["PrintArchive"] = relationship()
  35. project: Mapped["Project | None"] = relationship(back_populates="queue_items")
  36. from backend.app.models.archive import PrintArchive # noqa: E402
  37. from backend.app.models.printer import Printer # noqa: E402
  38. from backend.app.models.project import Project # noqa: E402