spool.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, String, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class Spool(Base):
  6. """Spool inventory item for tracking filament spools and their properties."""
  7. __tablename__ = "spool"
  8. # A freed id must not pass to a new spool: its usage history would follow.
  9. __table_args__ = {"sqlite_autoincrement": True}
  10. id: Mapped[int] = mapped_column(primary_key=True)
  11. material: Mapped[str] = mapped_column(String(50)) # PLA, PETG, ABS, etc.
  12. subtype: Mapped[str | None] = mapped_column(String(50)) # Basic, Matte, Silk, etc.
  13. color_name: Mapped[str | None] = mapped_column(String(100)) # "Jade White"
  14. rgba: Mapped[str | None] = mapped_column(String(8)) # RRGGBBAA hex
  15. # Multi-colour gradient stops for filaments with more than one colour
  16. # (e.g. tri-colour, multi-colour). Stored as comma-separated 6- or 8-char
  17. # hex tokens without `#`. Empty/NULL means solid (uses `rgba`). Up to 8
  18. # stops; combination mode is driven by `subtype` (Gradient, Multicolor).
  19. extra_colors: Mapped[str | None] = mapped_column(String(255))
  20. # Visual effect overlay independent of subtype: sparkle, wood, marble,
  21. # glow, matte. Purely a rendering hint — does not affect MQTT/firmware.
  22. effect_type: Mapped[str | None] = mapped_column(String(20))
  23. brand: Mapped[str | None] = mapped_column(String(100)) # "Polymaker"
  24. label_weight: Mapped[int] = mapped_column(Integer, default=1000) # Advertised net weight (g)
  25. core_weight: Mapped[int] = mapped_column(Integer, default=250) # Empty spool weight (g)
  26. core_weight_catalog_id: Mapped[int | None] = mapped_column(
  27. Integer
  28. ) # Reference to spool_catalog entry for core weight
  29. weight_used: Mapped[float] = mapped_column(Float, default=0) # Consumed grams
  30. # Anchor for the resettable "Total Consumed" stat. The displayed counter
  31. # is `weight_used - weight_used_baseline`; the Inventory page's "Reset
  32. # usage to 0" action stamps baseline = weight_used so the counter zeroes
  33. # without disturbing remaining (= label_weight - weight_used). Matches
  34. # Spoolman's split between used_weight and remaining_weight (#1390).
  35. weight_used_baseline: Mapped[float] = mapped_column(Float, default=0, server_default="0")
  36. weight_locked: Mapped[bool] = mapped_column(Boolean, default=False) # Lock weight from AMS auto-sync
  37. last_scale_weight: Mapped[int | None] = mapped_column(Integer) # Last gross weight from scale (g)
  38. last_weighed_at: Mapped[datetime | None] = mapped_column(DateTime) # When last weighed
  39. slicer_filament: Mapped[str | None] = mapped_column(String(50)) # Preset ID (e.g. "GFL99")
  40. slicer_filament_name: Mapped[str | None] = mapped_column(String(100)) # Preset name for slicer
  41. nozzle_temp_min: Mapped[int | None] = mapped_column() # Override min temp
  42. nozzle_temp_max: Mapped[int | None] = mapped_column() # Override max temp
  43. note: Mapped[str | None] = mapped_column(String(500))
  44. added_full: Mapped[bool | None] = mapped_column() # Whether spool was added as full (unused)
  45. # User-defined category (e.g. "Production", "Prototype", "Client A") for
  46. # filtering and per-group low-stock thresholds (#729). Free text — the
  47. # form autocompletes from categories already present on other spools.
  48. category: Mapped[str | None] = mapped_column(String(50))
  49. # Per-spool override of the global inventory low-stock threshold (%).
  50. # NULL falls back to the `low_stock_threshold` setting. Lets users mark
  51. # production spools with a higher threshold (alert earlier) and prototype
  52. # spools with a lower one without changing the global default.
  53. low_stock_threshold_pct: Mapped[int | None] = mapped_column(Integer)
  54. # Internal material / article number (#2870): the identifier a business
  55. # purchases and costs by (e.g. "15" = Bambu Lab PLA Basic), distinct from
  56. # `category` (production grouping) and `note` (free text). Free text, no
  57. # uniqueness — several spools of the same product share the number, which
  58. # is exactly what makes it a sort/filter/statistics key. New spools of a
  59. # matching product inherit it on creation (services/material_number.py,
  60. # applied by the spool create routes and the RFID auto-add).
  61. material_number: Mapped[str | None] = mapped_column(String(64))
  62. # Cost tracking
  63. cost_per_kg: Mapped[float | None] = mapped_column(Float) # Cost per kilogram
  64. storage_location: Mapped[str | None] = mapped_column(String(255)) # User-editable storage location
  65. location_id: Mapped[int | None] = mapped_column(ForeignKey("locations.id"), index=True)
  66. last_used: Mapped[datetime | None] = mapped_column(DateTime) # Last time this spool was used in a print
  67. # Last drying (#2863): stamped when an AMS drying cycle that held this spool
  68. # ends having run at least half its length, or set by hand in the spool
  69. # form for an external dryer. Temperature and hours are only known for
  70. # cycles Bambuddy watched or started, and a hand-set date clears both.
  71. last_dried_at: Mapped[datetime | None] = mapped_column(DateTime)
  72. last_dried_temp: Mapped[int | None] = mapped_column(Integer) # Target °C
  73. last_dried_hours: Mapped[float | None] = mapped_column(Float) # Time actually spent drying
  74. encode_time: Mapped[datetime | None] = mapped_column(DateTime) # When spool was encoded/written to tag
  75. tag_uid: Mapped[str | None] = mapped_column(String(32)) # RFID tag UID (up to 32 hex chars)
  76. tray_uuid: Mapped[str | None] = mapped_column(String(32)) # Bambu Lab spool UUID (32 hex chars)
  77. data_origin: Mapped[str | None] = mapped_column(String(20)) # How data was populated: manual, rfid_auto, nfc_link
  78. tag_type: Mapped[str | None] = mapped_column(String(20)) # Tag vendor: bambulab, generic, etc.
  79. archived_at: Mapped[datetime | None] = mapped_column(DateTime) # NULL = active
  80. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  81. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  82. k_profiles: Mapped[list["SpoolKProfile"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
  83. # Per-printer-model preset overrides. Deliberately NOT embedded in
  84. # SpoolResponse the way k_profiles is: the inventory list returns every
  85. # spool a user owns, and this list is only ever read by the spool form
  86. # and the assign path, both of which fetch it for one spool at a time.
  87. filament_presets: Mapped[list["SpoolFilamentPreset"]] = relationship(
  88. back_populates="spool", cascade="all, delete-orphan"
  89. )
  90. assignments: Mapped[list["SpoolAssignment"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
  91. location: Mapped["Location | None"] = relationship(back_populates="spools")
  92. # Supplier assignments (#2988): where this product can be bought, with
  93. # per-assignment article number / price and a purchase-source marker.
  94. # Default loader like every other relationship here — the handful of
  95. # routes that embed them ask for selectinload() at the query site.
  96. supplier_links: Mapped[list["SpoolSupplier"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
  97. from backend.app.models.location import Location # noqa: E402
  98. from backend.app.models.spool_assignment import SpoolAssignment # noqa: E402
  99. from backend.app.models.spool_filament_preset import SpoolFilamentPreset # noqa: E402
  100. from backend.app.models.spool_k_profile import SpoolKProfile # noqa: E402
  101. from backend.app.models.supplier import SpoolSupplier # noqa: E402