print_batch.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from datetime import datetime
  2. from sqlalchemy import DateTime, ForeignKey, Index, 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. __table_args__ = (Index("uq_print_batches_external", "external_source", "external_ref", unique=True),)
  18. id: Mapped[int] = mapped_column(primary_key=True)
  19. name: Mapped[str] = mapped_column(String(255))
  20. # Source file (one of these)
  21. archive_id: Mapped[int | None] = mapped_column(ForeignKey("print_archives.id", ondelete="SET NULL"), nullable=True)
  22. library_file_id: Mapped[int | None] = mapped_column(
  23. ForeignKey("library_files.id", ondelete="SET NULL"), nullable=True
  24. )
  25. # Total requested quantity (for display — actual items may differ if cancelled)
  26. quantity: Mapped[int] = mapped_column(Integer, default=1)
  27. # Status: active, completed, cancelled
  28. status: Mapped[str] = mapped_column(String(20), default="active")
  29. # Optional link to a Project, which owns the heavier planning metadata
  30. # (BOM, attachments, tags). The batch keeps only the two fields that are
  31. # useless without it — a date and free text — so an order doesn't force
  32. # the user to create a Project first.
  33. project_id: Mapped[int | None] = mapped_column(ForeignKey("projects.id", ondelete="SET NULL"), nullable=True)
  34. due_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  35. notes: Mapped[str | None] = mapped_column(Text, nullable=True)
  36. # Link to the record in another system that asked for this batch, e.g. a
  37. # shop order an integration turned into prints. ``external_ref`` identifies
  38. # this batch within ``external_source`` and is unique there, so a client
  39. # that retries a create after a lost response gets a 409 instead of a
  40. # second batch printing the same order twice. Both set or both null.
  41. external_source: Mapped[str | None] = mapped_column(String(32), nullable=True)
  42. external_ref: Mapped[str | None] = mapped_column(String(255), nullable=True)
  43. # Timestamps
  44. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  45. completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  46. # User tracking
  47. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  48. # Relationships
  49. archive: Mapped["PrintArchive | None"] = relationship()
  50. library_file: Mapped["LibraryFile | None"] = relationship()
  51. created_by: Mapped["User | None"] = relationship()
  52. queue_items: Mapped[list["PrintQueueItem"]] = relationship(back_populates="batch")
  53. plates: Mapped[list["PrintBatchPlate"]] = relationship(
  54. back_populates="batch",
  55. cascade="all, delete-orphan",
  56. order_by="PrintBatchPlate.sort_order",
  57. )
  58. class PrintBatchPlate(Base):
  59. """How many runs of one plate a batch still owes.
  60. ``plate_id`` is the plate index within the source 3MF, or NULL for a
  61. single-plate file / whole-file print — the same convention
  62. ``PrintQueueItem.plate_id`` uses, so progress can be derived by grouping
  63. the batch's items on that column.
  64. """
  65. __tablename__ = "print_batch_plates"
  66. __table_args__ = (UniqueConstraint("batch_id", "plate_id", name="uq_batch_plate"),)
  67. id: Mapped[int] = mapped_column(primary_key=True)
  68. batch_id: Mapped[int] = mapped_column(
  69. ForeignKey("print_batches.id", ondelete="CASCADE"), nullable=False, index=True
  70. )
  71. plate_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
  72. plate_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
  73. # How many runs of this plate the order wants. Zero is legal — a plate the
  74. # user explicitly marked "not required" keeps its row so it can be raised
  75. # later without re-creating the order.
  76. quantity_target: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
  77. # Display order; mirrors the plate order in the source file.
  78. sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
  79. batch: Mapped["PrintBatch"] = relationship(back_populates="plates")
  80. from backend.app.models.archive import PrintArchive # noqa: E402
  81. from backend.app.models.library import LibraryFile # noqa: E402
  82. from backend.app.models.print_queue import PrintQueueItem # noqa: E402
  83. from backend.app.models.user import User # noqa: E402