print_batch.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from datetime import datetime
  2. from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, UniqueConstraint, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class PrintBatch(Base):
  6. """Batch grouping for multiple queue items created from the same file.
  7. A batch carries the *intent* — how many of each plate are wanted — in its
  8. :class:`PrintBatchPlate` rows, while the queue items it spawned carry what
  9. was actually dispatched. Keeping the two apart is what lets a failed print
  10. still count as owed work: the plate row's ``quantity_target`` stays put
  11. while the failed item lands in the "failed" bucket, so ``remaining`` goes
  12. back up instead of the order silently under-delivering (#342).
  13. Batches created before plate rows existed simply have none; every consumer
  14. falls back to deriving progress from the queue items alone.
  15. """
  16. __tablename__ = "print_batches"
  17. id: Mapped[int] = mapped_column(primary_key=True)
  18. name: Mapped[str] = mapped_column(String(255))
  19. # Source file (one of these)
  20. archive_id: Mapped[int | None] = mapped_column(ForeignKey("print_archives.id", ondelete="SET NULL"), nullable=True)
  21. library_file_id: Mapped[int | None] = mapped_column(
  22. ForeignKey("library_files.id", ondelete="SET NULL"), nullable=True
  23. )
  24. # Total requested quantity (for display — actual items may differ if cancelled)
  25. quantity: Mapped[int] = mapped_column(Integer, default=1)
  26. # Status: active, completed, cancelled
  27. status: Mapped[str] = mapped_column(String(20), default="active")
  28. # Optional link to a Project, which owns the heavier planning metadata
  29. # (BOM, attachments, tags). The batch keeps only the two fields that are
  30. # useless without it — a date and free text — so an order doesn't force
  31. # the user to create a Project first.
  32. project_id: Mapped[int | None] = mapped_column(ForeignKey("projects.id", ondelete="SET NULL"), nullable=True)
  33. due_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  34. notes: Mapped[str | None] = mapped_column(Text, nullable=True)
  35. # Timestamps
  36. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  37. completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  38. # User tracking
  39. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  40. # Relationships
  41. archive: Mapped["PrintArchive | None"] = relationship()
  42. library_file: Mapped["LibraryFile | None"] = relationship()
  43. created_by: Mapped["User | None"] = relationship()
  44. queue_items: Mapped[list["PrintQueueItem"]] = relationship(back_populates="batch")
  45. plates: Mapped[list["PrintBatchPlate"]] = relationship(
  46. back_populates="batch",
  47. cascade="all, delete-orphan",
  48. order_by="PrintBatchPlate.sort_order",
  49. )
  50. class PrintBatchPlate(Base):
  51. """How many runs of one plate a batch still owes.
  52. ``plate_id`` is the plate index within the source 3MF, or NULL for a
  53. single-plate file / whole-file print — the same convention
  54. ``PrintQueueItem.plate_id`` uses, so progress can be derived by grouping
  55. the batch's items on that column.
  56. """
  57. __tablename__ = "print_batch_plates"
  58. __table_args__ = (UniqueConstraint("batch_id", "plate_id", name="uq_batch_plate"),)
  59. id: Mapped[int] = mapped_column(primary_key=True)
  60. batch_id: Mapped[int] = mapped_column(
  61. ForeignKey("print_batches.id", ondelete="CASCADE"), nullable=False, index=True
  62. )
  63. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  64. plate_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
  65. # How many runs of this plate the order wants. Zero is legal — a plate the
  66. # user explicitly marked "not required" keeps its row so it can be raised
  67. # later without re-creating the order.
  68. quantity_target: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
  69. # Display order; mirrors the plate order in the source file.
  70. sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
  71. batch: Mapped["PrintBatch"] = relationship(back_populates="plates")
  72. from backend.app.models.archive import PrintArchive # noqa: E402
  73. from backend.app.models.library import LibraryFile # noqa: E402
  74. from backend.app.models.print_queue import PrintQueueItem # noqa: E402
  75. from backend.app.models.user import User # noqa: E402