inventory_mode.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Which table holds a printer's slot assignments.
  2. Bambuddy keeps AMS slot assignments in two places: ``spool_assignment`` for the
  3. built-in inventory and ``spoolman_slot_assignments`` for Spoolman. Exactly one
  4. of them describes reality at any moment, and which one is a user setting.
  5. Until #2812 the two were kept from overlapping by emptying the inactive table
  6. whenever the mode toggled, which made merely looking at the other mode destroy
  7. the configuration you had. Nothing is deleted now, so both tables can hold rows
  8. at once and every reader has to say which one it means.
  9. This is deliberately a module of its own rather than a helper on the settings
  10. routes: the readers are services, and importing an API route module from a
  11. service to answer a one-key question invites an import cycle. Several call
  12. sites already carry their own private copy of this predicate for that reason
  13. (``filament_deficit``, ``print_scheduler``, ``inventory``); those are unchanged
  14. and correct, and are only worth folding in here if they are touched anyway.
  15. """
  16. import logging
  17. from sqlalchemy.ext.asyncio import AsyncSession
  18. logger = logging.getLogger(__name__)
  19. async def spoolman_owns_assignments(db: AsyncSession) -> bool:
  20. """True when ``spoolman_slot_assignments`` is the table that counts.
  21. Fails closed to the built-in inventory: a setting that cannot be read is
  22. not evidence that the user switched modes, and treating an unreadable
  23. setting as "Spoolman" would make a built-in install look as though every
  24. tray were unassigned.
  25. """
  26. try:
  27. from backend.app.api.routes.settings import get_setting
  28. value = await get_setting(db, "spoolman_enabled")
  29. return bool(value) and value.lower() == "true"
  30. except Exception as exc: # noqa: BLE001 — a mode probe must not raise into its callers
  31. logger.debug("Could not read spoolman_enabled, assuming built-in inventory: %s", exc)
  32. return False