spool.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, Float, 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. id: Mapped[int] = mapped_column(primary_key=True)
  9. material: Mapped[str] = mapped_column(String(50)) # PLA, PETG, ABS, etc.
  10. subtype: Mapped[str | None] = mapped_column(String(50)) # Basic, Matte, Silk, etc.
  11. color_name: Mapped[str | None] = mapped_column(String(100)) # "Jade White"
  12. rgba: Mapped[str | None] = mapped_column(String(8)) # RRGGBBAA hex
  13. brand: Mapped[str | None] = mapped_column(String(100)) # "Polymaker"
  14. label_weight: Mapped[int] = mapped_column(Integer, default=1000) # Advertised net weight (g)
  15. core_weight: Mapped[int] = mapped_column(Integer, default=250) # Empty spool weight (g)
  16. core_weight_catalog_id: Mapped[int | None] = mapped_column(
  17. Integer
  18. ) # Reference to spool_catalog entry for core weight
  19. weight_used: Mapped[float] = mapped_column(Float, default=0) # Consumed grams
  20. weight_locked: Mapped[bool] = mapped_column(Boolean, default=False) # Lock weight from AMS auto-sync
  21. last_scale_weight: Mapped[int | None] = mapped_column(Integer) # Last gross weight from scale (g)
  22. last_weighed_at: Mapped[datetime | None] = mapped_column(DateTime) # When last weighed
  23. slicer_filament: Mapped[str | None] = mapped_column(String(50)) # Preset ID (e.g. "GFL99")
  24. slicer_filament_name: Mapped[str | None] = mapped_column(String(100)) # Preset name for slicer
  25. nozzle_temp_min: Mapped[int | None] = mapped_column() # Override min temp
  26. nozzle_temp_max: Mapped[int | None] = mapped_column() # Override max temp
  27. note: Mapped[str | None] = mapped_column(String(500))
  28. added_full: Mapped[bool | None] = mapped_column() # Whether spool was added as full (unused)
  29. # User-defined category (e.g. "Production", "Prototype", "Client A") for
  30. # filtering and per-group low-stock thresholds (#729). Free text — the
  31. # form autocompletes from categories already present on other spools.
  32. category: Mapped[str | None] = mapped_column(String(50))
  33. # Per-spool override of the global inventory low-stock threshold (%).
  34. # NULL falls back to the `low_stock_threshold` setting. Lets users mark
  35. # production spools with a higher threshold (alert earlier) and prototype
  36. # spools with a lower one without changing the global default.
  37. low_stock_threshold_pct: Mapped[int | None] = mapped_column(Integer)
  38. # Cost tracking
  39. cost_per_kg: Mapped[float | None] = mapped_column(Float) # Cost per kilogram
  40. last_used: Mapped[datetime | None] = mapped_column(DateTime) # Last time this spool was used in a print
  41. encode_time: Mapped[datetime | None] = mapped_column(DateTime) # When spool was encoded/written to tag
  42. tag_uid: Mapped[str | None] = mapped_column(String(16)) # RFID tag UID (16 hex chars)
  43. tray_uuid: Mapped[str | None] = mapped_column(String(32)) # Bambu Lab spool UUID (32 hex chars)
  44. data_origin: Mapped[str | None] = mapped_column(String(20)) # How data was populated: manual, rfid_auto, nfc_link
  45. tag_type: Mapped[str | None] = mapped_column(String(20)) # Tag vendor: bambulab, generic, etc.
  46. archived_at: Mapped[datetime | None] = mapped_column(DateTime) # NULL = active
  47. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  48. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  49. k_profiles: Mapped[list["SpoolKProfile"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
  50. assignments: Mapped[list["SpoolAssignment"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
  51. from backend.app.models.spool_assignment import SpoolAssignment # noqa: E402
  52. from backend.app.models.spool_k_profile import SpoolKProfile # noqa: E402