labels.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. """Spool label printing routes (#809).
  2. Two endpoints, one per inventory backend:
  3. - ``POST /inventory/labels`` — local-DB spools
  4. - ``POST /spoolman/labels`` — Spoolman-backed spools
  5. Both accept ``{spool_ids: [int], template: str}`` and return a PDF stream.
  6. The QR code on each label deep-links to ``/inventory?spool=<id>`` so a phone
  7. scan jumps straight back into Bambuddy at that spool's row.
  8. """
  9. from __future__ import annotations
  10. import io
  11. import logging
  12. from typing import Literal
  13. from fastapi import APIRouter, Depends, HTTPException, Request
  14. from fastapi.responses import StreamingResponse
  15. from pydantic import BaseModel, Field
  16. from sqlalchemy import select
  17. from sqlalchemy.ext.asyncio import AsyncSession
  18. from backend.app.api.routes.settings import get_setting
  19. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  20. from backend.app.core.database import get_db
  21. from backend.app.core.permissions import Permission
  22. from backend.app.models.spool import Spool
  23. from backend.app.models.user import User
  24. from backend.app.services.label_renderer import LabelData, TemplateName, render_labels
  25. from backend.app.services.spoolman import get_spoolman_client
  26. from backend.app.utils.http import build_content_disposition
  27. logger = logging.getLogger(__name__)
  28. router = APIRouter(tags=["labels"])
  29. _VALID_TEMPLATES: tuple[TemplateName, ...] = (
  30. "ams_30x15",
  31. "box_62x29",
  32. "avery_5160",
  33. "avery_l7160",
  34. )
  35. # Cap how many labels can be requested in one go. Sane upper bound for the
  36. # largest realistic batch (an Avery sheet at 30/page × ~10 pages).
  37. MAX_LABELS_PER_REQUEST = 500
  38. class LabelRequest(BaseModel):
  39. spool_ids: list[int] = Field(..., min_length=1, max_length=MAX_LABELS_PER_REQUEST)
  40. template: Literal["ams_30x15", "box_62x29", "avery_5160", "avery_l7160"]
  41. def _split_extra_colors(raw: str | None) -> list[str] | None:
  42. """Parse ``Spool.extra_colors`` (comma-separated hex tokens) into a list."""
  43. if not raw:
  44. return None
  45. parts = [p.strip().lstrip("#") for p in raw.split(",") if p.strip()]
  46. return parts or None
  47. async def _resolve_deeplink_base(request: Request, db: AsyncSession) -> str:
  48. """Where the QR codes should point. Prefers `external_url` when set so a
  49. phone scan reaches the user's public Bambuddy URL rather than an internal
  50. address; falls back to the request's own scheme+host when no setting is
  51. configured.
  52. """
  53. external = (await get_setting(db, "external_url") or "").strip().rstrip("/")
  54. if external:
  55. return external
  56. return f"{request.url.scheme}://{request.url.netloc}"
  57. def _spool_to_label_data(spool: Spool, deeplink_base: str) -> LabelData:
  58. name = spool.color_name or spool.slicer_filament_name or f"{spool.brand or ''} {spool.material}".strip()
  59. return LabelData(
  60. spool_id=spool.id,
  61. name=name or spool.material,
  62. material=spool.material,
  63. brand=spool.brand,
  64. subtype=spool.subtype,
  65. rgba=spool.rgba,
  66. extra_colors=_split_extra_colors(spool.extra_colors),
  67. storage_location=getattr(spool, "storage_location", None),
  68. deeplink_url=f"{deeplink_base}/inventory?spool={spool.id}",
  69. )
  70. def _spoolman_dict_to_label_data(s: dict, deeplink_base: str) -> LabelData:
  71. """Build LabelData from a raw Spoolman /spool response dict.
  72. Spoolman models don't have a native 'spool name' — we derive it from the
  73. embedded filament. Material and brand come from filament/vendor.
  74. """
  75. filament = s.get("filament") or {}
  76. vendor = filament.get("vendor") or {}
  77. fname = filament.get("name") or ""
  78. material = filament.get("material") or ""
  79. brand = vendor.get("name")
  80. color_hex = filament.get("color_hex")
  81. rgba = color_hex.lstrip("#") if isinstance(color_hex, str) else None
  82. multi_colors = filament.get("multi_color_hexes")
  83. extra: list[str] | None = None
  84. if isinstance(multi_colors, str) and multi_colors.strip():
  85. extra = [tok.strip().lstrip("#") for tok in multi_colors.split(",") if tok.strip()]
  86. elif isinstance(multi_colors, list):
  87. extra = [str(t).strip().lstrip("#") for t in multi_colors if str(t).strip()]
  88. return LabelData(
  89. spool_id=int(s.get("id", 0)),
  90. name=fname or material or "Spool",
  91. material=material or "",
  92. brand=brand,
  93. subtype=None,
  94. rgba=rgba,
  95. extra_colors=extra,
  96. storage_location=s.get("location"),
  97. deeplink_url=f"{deeplink_base}/inventory?spool={int(s.get('id', 0))}",
  98. )
  99. def _stream_pdf(pdf: bytes, filename: str) -> StreamingResponse:
  100. return StreamingResponse(
  101. io.BytesIO(pdf),
  102. media_type="application/pdf",
  103. headers={
  104. "Content-Disposition": build_content_disposition(filename, disposition="inline"),
  105. "Content-Length": str(len(pdf)),
  106. # PDFs are deterministic per request; tell the browser not to cache
  107. # so re-printing after edits picks up the new data.
  108. "Cache-Control": "no-store",
  109. },
  110. )
  111. @router.post("/inventory/labels")
  112. async def render_local_inventory_labels(
  113. body: LabelRequest,
  114. request: Request,
  115. db: AsyncSession = Depends(get_db),
  116. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  117. ) -> StreamingResponse:
  118. """Render labels for spools in the local inventory."""
  119. if body.template not in _VALID_TEMPLATES:
  120. raise HTTPException(400, f"Unknown template: {body.template}")
  121. result = await db.execute(select(Spool).where(Spool.id.in_(body.spool_ids)))
  122. spools = list(result.scalars().all())
  123. found_ids = {s.id for s in spools}
  124. missing = [sid for sid in body.spool_ids if sid not in found_ids]
  125. if missing:
  126. raise HTTPException(404, f"Spool(s) not found: {missing}")
  127. # Preserve caller's order so an Avery sheet print matches the on-screen list.
  128. ordered = sorted(spools, key=lambda s: body.spool_ids.index(s.id))
  129. deeplink_base = await _resolve_deeplink_base(request, db)
  130. data_list = [_spool_to_label_data(s, deeplink_base) for s in ordered]
  131. pdf = render_labels(body.template, data_list)
  132. filename = f"bambuddy-labels-{body.template}.pdf"
  133. return _stream_pdf(pdf, filename)
  134. @router.post("/spoolman/labels")
  135. async def render_spoolman_labels(
  136. body: LabelRequest,
  137. request: Request,
  138. db: AsyncSession = Depends(get_db),
  139. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  140. ) -> StreamingResponse:
  141. """Render labels for spools tracked in Spoolman.
  142. The Spoolman client doesn't expose a per-id endpoint, so this fetches the
  143. full spool list and filters in-memory. For typical libraries (~50 spools)
  144. that's negligible; for very large libraries this is the trade-off until
  145. Spoolman gains a bulk filter.
  146. """
  147. if body.template not in _VALID_TEMPLATES:
  148. raise HTTPException(400, f"Unknown template: {body.template}")
  149. spoolman_on = (await get_setting(db, "spoolman_enabled") or "").lower() == "true"
  150. if not spoolman_on:
  151. raise HTTPException(400, "Spoolman integration is not enabled")
  152. client = await get_spoolman_client()
  153. if client is None or not client.is_connected:
  154. raise HTTPException(503, "Spoolman not reachable")
  155. try:
  156. all_spools = await client.get_spools()
  157. except Exception as exc:
  158. logger.warning("Spoolman fetch failed during label render: %s", exc)
  159. raise HTTPException(502, "Failed to fetch spools from Spoolman") from exc
  160. by_id = {int(s.get("id", 0)): s for s in all_spools if s.get("id") is not None}
  161. missing = [sid for sid in body.spool_ids if sid not in by_id]
  162. if missing:
  163. raise HTTPException(404, f"Spool(s) not found in Spoolman: {missing}")
  164. deeplink_base = await _resolve_deeplink_base(request, db)
  165. data_list = [_spoolman_dict_to_label_data(by_id[sid], deeplink_base) for sid in body.spool_ids]
  166. pdf = render_labels(body.template, data_list)
  167. filename = f"bambuddy-labels-spoolman-{body.template}.pdf"
  168. return _stream_pdf(pdf, filename)