Files
bambuddy/backend/app/models/spool.py
T
maziggy a34beaa599 feat(inventory): multi-colour gradients, transparency, visual effects (#1154)
Spool and color_catalog rows carry extra_colors (comma-separated hex
  stops) and effect_type (14 visual variants: surface effects, sheen,
  structural). The shared FilamentSwatch component renders gradient,
  conic, effect overlay, and alpha-checkerboard consistently across the
  inventory grid, table, group banner, card, ColorSection preview, and
  catalog editor. Catalog hex_color accepts #RRGGBBAA so catalog entries
  can carry transparency too.

  The paste field accepts the exact format 3dfilamentprofiles.com puts on
  its filament details pages, so users can copy a multi-colour combo
  directly. The effect dropdown spans the full filament-variant
  vocabulary -- surface effects (sparkle/wood/marble/glow/matte), sheen
  variants (silk/galaxy/rainbow/metal/translucent), and structural
  variants (gradient/dual-color/tri-color/multicolor). None of these
  fields touch MQTT/firmware -- pure visual hint.

  Spool group-key extended to include extra_colors + effect_type so
  "Group similar" no longer collapses visually distinct spools.

  Migrations: 4 idempotent ALTER TABLE ADD COLUMN (Postgres-safe), plus
  ALTER COLUMN hex_color TYPE VARCHAR(9) on Postgres only (SQLite ignores
  VARCHAR length).

  Tests: 42 new backend (35 unit + 7 integration), 20 new frontend (14
  FilamentSwatch + 3 ColorCatalogSettings + 3 InventoryPageGrouping
  regression). 3522 backend + 1582 frontend tests pass; ruff clean.
  Localised across all 8 UI locales.
2026-04-29 09:13:33 +02:00

73 lines
4.5 KiB
Python

from datetime import datetime
from sqlalchemy import Boolean, DateTime, Float, Integer, String, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from backend.app.core.database import Base
class Spool(Base):
"""Spool inventory item for tracking filament spools and their properties."""
__tablename__ = "spool"
id: Mapped[int] = mapped_column(primary_key=True)
material: Mapped[str] = mapped_column(String(50)) # PLA, PETG, ABS, etc.
subtype: Mapped[str | None] = mapped_column(String(50)) # Basic, Matte, Silk, etc.
color_name: Mapped[str | None] = mapped_column(String(100)) # "Jade White"
rgba: Mapped[str | None] = mapped_column(String(8)) # RRGGBBAA hex
# Multi-colour gradient stops for filaments with more than one colour
# (e.g. tri-colour, multi-colour). Stored as comma-separated 6- or 8-char
# hex tokens without `#`. Empty/NULL means solid (uses `rgba`). Up to 8
# stops; combination mode is driven by `subtype` (Gradient, Multicolor).
extra_colors: Mapped[str | None] = mapped_column(String(255))
# Visual effect overlay independent of subtype: sparkle, wood, marble,
# glow, matte. Purely a rendering hint — does not affect MQTT/firmware.
effect_type: Mapped[str | None] = mapped_column(String(20))
brand: Mapped[str | None] = mapped_column(String(100)) # "Polymaker"
label_weight: Mapped[int] = mapped_column(Integer, default=1000) # Advertised net weight (g)
core_weight: Mapped[int] = mapped_column(Integer, default=250) # Empty spool weight (g)
core_weight_catalog_id: Mapped[int | None] = mapped_column(
Integer
) # Reference to spool_catalog entry for core weight
weight_used: Mapped[float] = mapped_column(Float, default=0) # Consumed grams
weight_locked: Mapped[bool] = mapped_column(Boolean, default=False) # Lock weight from AMS auto-sync
last_scale_weight: Mapped[int | None] = mapped_column(Integer) # Last gross weight from scale (g)
last_weighed_at: Mapped[datetime | None] = mapped_column(DateTime) # When last weighed
slicer_filament: Mapped[str | None] = mapped_column(String(50)) # Preset ID (e.g. "GFL99")
slicer_filament_name: Mapped[str | None] = mapped_column(String(100)) # Preset name for slicer
nozzle_temp_min: Mapped[int | None] = mapped_column() # Override min temp
nozzle_temp_max: Mapped[int | None] = mapped_column() # Override max temp
note: Mapped[str | None] = mapped_column(String(500))
added_full: Mapped[bool | None] = mapped_column() # Whether spool was added as full (unused)
# User-defined category (e.g. "Production", "Prototype", "Client A") for
# filtering and per-group low-stock thresholds (#729). Free text — the
# form autocompletes from categories already present on other spools.
category: Mapped[str | None] = mapped_column(String(50))
# Per-spool override of the global inventory low-stock threshold (%).
# NULL falls back to the `low_stock_threshold` setting. Lets users mark
# production spools with a higher threshold (alert earlier) and prototype
# spools with a lower one without changing the global default.
low_stock_threshold_pct: Mapped[int | None] = mapped_column(Integer)
# Cost tracking
cost_per_kg: Mapped[float | None] = mapped_column(Float) # Cost per kilogram
last_used: Mapped[datetime | None] = mapped_column(DateTime) # Last time this spool was used in a print
encode_time: Mapped[datetime | None] = mapped_column(DateTime) # When spool was encoded/written to tag
tag_uid: Mapped[str | None] = mapped_column(String(16)) # RFID tag UID (16 hex chars)
tray_uuid: Mapped[str | None] = mapped_column(String(32)) # Bambu Lab spool UUID (32 hex chars)
data_origin: Mapped[str | None] = mapped_column(String(20)) # How data was populated: manual, rfid_auto, nfc_link
tag_type: Mapped[str | None] = mapped_column(String(20)) # Tag vendor: bambulab, generic, etc.
archived_at: Mapped[datetime | None] = mapped_column(DateTime) # NULL = active
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
k_profiles: Mapped[list["SpoolKProfile"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
assignments: Mapped[list["SpoolAssignment"]] = relationship(back_populates="spool", cascade="all, delete-orphan")
from backend.app.models.spool_assignment import SpoolAssignment # noqa: E402
from backend.app.models.spool_k_profile import SpoolKProfile # noqa: E402