print_queue.py 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  1. """API routes for print queue management."""
  2. import json
  3. import logging
  4. import zipfile
  5. from datetime import datetime, timezone
  6. from pathlib import Path
  7. import defusedxml.ElementTree as ET
  8. from fastapi import APIRouter, Depends, HTTPException, Query
  9. from sqlalchemy import and_, func, inspect, or_, select, update
  10. from sqlalchemy.exc import IntegrityError
  11. from sqlalchemy.ext.asyncio import AsyncSession
  12. from sqlalchemy.orm import selectinload
  13. from backend.app.api.routes.library_variants import normalize_model_name, resolve_variant_model
  14. from backend.app.core.auth import (
  15. ApiKeyActor,
  16. QueueReviewRequired,
  17. RequestActor,
  18. RequestPrinterScope,
  19. RequirePermissionIfAuthEnabled,
  20. RequirePrinterPermissionIfAuthEnabled,
  21. may_start_queue_item,
  22. require_ownership_permission,
  23. )
  24. from backend.app.core.config import settings
  25. from backend.app.core.database import get_db
  26. from backend.app.core.permissions import Permission
  27. from backend.app.core.printer_scope import PrinterScope, ensure_model_target_allowed
  28. from backend.app.models.archive import PrintArchive
  29. from backend.app.models.library import LibraryFile
  30. from backend.app.models.print_batch import PrintBatch, PrintBatchPlate
  31. from backend.app.models.print_queue import PrintQueueItem, PrintQueueVariant
  32. from backend.app.models.printer import Printer
  33. from backend.app.models.project import Project
  34. from backend.app.models.user import User
  35. from backend.app.schemas.print_queue import (
  36. PrintBatchCreate,
  37. PrintBatchDispatchRequest,
  38. PrintBatchPlateProgress,
  39. PrintBatchPlateTarget,
  40. PrintBatchResponse,
  41. PrintBatchUngroupResponse,
  42. PrintBatchUpdate,
  43. PrintQueueBulkUpdate,
  44. PrintQueueBulkUpdateResponse,
  45. PrintQueueItemCreate,
  46. PrintQueueItemResponse,
  47. PrintQueueItemUpdate,
  48. PrintQueueReorder,
  49. QueueVariantCreate,
  50. QueueVariantSummary,
  51. )
  52. from backend.app.services.filament_deficit import compute_deficit_for_queue_item
  53. from backend.app.services.filament_requirements import overrides_for_plate
  54. from backend.app.services.finance_budget import release_budget_reservation, validate_print_budget
  55. from backend.app.services.notification_service import notification_service
  56. from backend.app.services.print_batch import (
  57. BatchDispatchError,
  58. dispatch_remaining,
  59. load_progress,
  60. refresh_batch_status,
  61. refresh_batch_status_for_item,
  62. )
  63. from backend.app.services.print_cost_estimate import estimate_queue_source_cost
  64. from backend.app.services.queue_position import lock_queue_positions, max_queue_position
  65. from backend.app.utils.printer_models import (
  66. is_gcode_compatible,
  67. )
  68. from backend.app.utils.threemf_tools import (
  69. extract_plate_metadata_from_3mf,
  70. extract_print_time_from_3mf,
  71. )
  72. logger = logging.getLogger(__name__)
  73. router = APIRouter(prefix="/queue", tags=["queue"])
  74. # Answer to a caller who may not start a job that waits for review (#1620)
  75. _AWAITING_REVIEW = "This job waits for review: someone who can manage all queue jobs has to start it"
  76. def _variant_summaries(item: PrintQueueItem) -> list[QueueVariantSummary]:
  77. """Cross-model candidates for display (#671), or [] if they weren't loaded.
  78. Every route that builds a queue response eager-loads ``variants``. Reading
  79. the attribute unguarded would still be a landmine for the next one that
  80. doesn't: a lazy load on an async session raises rather than degrading, so a
  81. forgotten ``selectinload`` would turn a card render into a 500.
  82. """
  83. if "variants" in inspect(item).unloaded:
  84. return []
  85. return [
  86. QueueVariantSummary(
  87. library_file_id=v.library_file_id,
  88. filename=v.library_file.filename if v.library_file else "",
  89. target_model=v.target_model,
  90. position=v.position,
  91. )
  92. for v in item.variants
  93. ]
  94. def _extract_filament_types_from_3mf(file_path: Path, plate_id: int | None = None) -> list[str]:
  95. """Extract unique filament types from a 3MF file.
  96. Args:
  97. file_path: Path to the 3MF file
  98. plate_id: Optional plate index to filter for (for multi-plate files)
  99. Returns:
  100. List of unique filament types (e.g., ["PLA", "PETG"])
  101. """
  102. types: set[str] = set()
  103. try:
  104. with zipfile.ZipFile(file_path, "r") as zf:
  105. if "Metadata/slice_info.config" not in zf.namelist():
  106. return []
  107. content = zf.read("Metadata/slice_info.config").decode()
  108. root = ET.fromstring(content)
  109. if plate_id is not None:
  110. # Find the plate element with matching index
  111. for plate_elem in root.findall(".//plate"):
  112. plate_index = None
  113. for meta in plate_elem.findall("metadata"):
  114. if meta.get("key") == "index":
  115. try:
  116. plate_index = int(meta.get("value", "0"))
  117. except ValueError:
  118. pass # Skip plate with unparseable index
  119. break
  120. if plate_index == plate_id:
  121. for filament_elem in plate_elem.findall("filament"):
  122. filament_type = filament_elem.get("type", "")
  123. used_g = filament_elem.get("used_g", "0")
  124. try:
  125. used_grams = float(used_g)
  126. except (ValueError, TypeError):
  127. used_grams = 0
  128. if used_grams > 0 and filament_type:
  129. types.add(filament_type)
  130. break
  131. else:
  132. # No plate_id specified - extract all filaments with used_g > 0
  133. for filament_elem in root.findall(".//filament"):
  134. filament_type = filament_elem.get("type", "")
  135. used_g = filament_elem.get("used_g", "0")
  136. try:
  137. used_grams = float(used_g)
  138. except (ValueError, TypeError):
  139. used_grams = 0
  140. if used_grams > 0 and filament_type:
  141. types.add(filament_type)
  142. except Exception as e:
  143. logger.warning("Failed to extract filament types from %s: %s", file_path, e)
  144. return sorted(types)
  145. # Local alias kept so existing call sites stay compact; the implementation lives
  146. # in utils/threemf_tools.py so the notification path (main.py) can reuse it
  147. # without importing from a routes module (#1785).
  148. _extract_print_time_from_3mf = extract_print_time_from_3mf
  149. def _assert_can_queue_archive(
  150. archive: PrintArchive, current_user: User | ApiKeyActor | None, printer_scope: PrinterScope
  151. ) -> None:
  152. """Gate turning *archive* into a print. Raises rather than returning a verdict.
  153. Shared by every route that creates queue items from an archive, so a new
  154. one can't quietly become a weaker door to the same action than
  155. ``POST /queue/`` is.
  156. Three checks:
  157. * The archive must not be deleted, and its printer must be one the caller
  158. may use; otherwise it reads as missing, as the archive routes answer.
  159. * IDOR fix (maziggy/bambuddy-security #2): without this, a caller with
  160. QUEUE_CREATE could queue any user's archive even without ARCHIVES_READ on
  161. it — Landon's PoC enumerated this on admin's archives as operator1. Gate
  162. on ARCHIVES_READ_ALL OR ownership. 404 (not 403) so we don't leak "this
  163. id exists but you can't queue it" for enumeration.
  164. * Reprint perm gate (#1625): the legacy ``/archives/{id}/reprint`` endpoint
  165. required ARCHIVES_REPRINT_OWN/ALL, and every route that replaces it must
  166. keep that gate or an operator with QUEUE_CREATE could reprint via a
  167. direct API call even when explicitly denied reprint perm. Mirrors the
  168. frontend ``canModify('archives', 'reprint', ...)`` helper: REPRINT_ALL
  169. allows any archive, REPRINT_OWN allows own only, ownerless archives
  170. require REPRINT_ALL (fail-closed).
  171. """
  172. # A deleted archive, or one from a printer the caller can't use, reads as
  173. # missing, like the archive routes answer for it.
  174. if archive.deleted_at is not None or not printer_scope.allows(archive.printer_id):
  175. raise HTTPException(404, "Archive not found")
  176. # None is auth off (for an API key too).
  177. if current_user is None:
  178. return
  179. owns_archive = archive.created_by_id is not None and archive.created_by_id == current_user.id
  180. # An ownerless archive is no one's own, also not a caller without an id.
  181. if not current_user.has_permission(Permission.ARCHIVES_READ_ALL.value) and not owns_archive:
  182. raise HTTPException(404, "Archive not found")
  183. has_reprint = current_user.has_permission(Permission.ARCHIVES_REPRINT_ALL.value) or (
  184. owns_archive and current_user.has_permission(Permission.ARCHIVES_REPRINT_OWN.value)
  185. )
  186. if not has_reprint:
  187. raise HTTPException(
  188. status_code=403,
  189. detail="Permission archives:reprint_own or archives:reprint_all required",
  190. )
  191. def _assert_can_queue_library_file(library_file: LibraryFile, current_user: User | ApiKeyActor | None) -> None:
  192. """Gate turning *library_file* into a print — LIBRARY_READ_ALL or ownership."""
  193. # None is auth off. An API key arrives as its RequestActor, never None.
  194. if current_user is None:
  195. return
  196. # An ownerless file is no one's own, also not a caller without an id.
  197. owns_file = library_file.created_by_id is not None and library_file.created_by_id == current_user.id
  198. if not current_user.has_permission(Permission.LIBRARY_READ_ALL.value) and not owns_file:
  199. raise HTTPException(404, "Library file not found")
  200. async def _is_orders_last_source(db: AsyncSession, item: PrintQueueItem) -> bool:
  201. """True when deleting *item* would leave an order owing runs it can't queue.
  202. Dispatch produces the runs an order still owes by cloning an existing
  203. queue item for the same plate — that row is the only record of the printer
  204. target, AMS mapping and print options the user chose. Delete the last one
  205. while the plate still has a target and the order is stranded: it goes on
  206. reporting work outstanding with no way left to produce it (#2960).
  207. """
  208. if item.batch_id is None:
  209. return False
  210. # A cancelled order can never dispatch again, so nothing about it can be
  211. # stranded and its leftover rows must stay deletable — tidying up after a
  212. # cancel is the most likely reason anyone deletes them.
  213. status = (await db.execute(select(PrintBatch.status).where(PrintBatch.id == item.batch_id))).scalar_one_or_none()
  214. if status == "cancelled":
  215. return False
  216. plate_scope = (
  217. PrintBatchPlate.plate_id == item.plate_id if item.plate_id is not None else PrintBatchPlate.plate_id.is_(None)
  218. )
  219. # first(), not scalar_one_or_none(): a UNIQUE(batch_id, plate_id) does not
  220. # constrain NULL plate_ids on either dialect, and a duplicate whole-file
  221. # row must not turn a delete into a 500.
  222. target = (
  223. (
  224. await db.execute(
  225. select(PrintBatchPlate.quantity_target)
  226. .where(PrintBatchPlate.batch_id == item.batch_id)
  227. .where(plate_scope)
  228. .order_by(PrintBatchPlate.quantity_target.desc())
  229. .limit(1)
  230. )
  231. )
  232. .scalars()
  233. .first()
  234. )
  235. # No target row: a grouping, or a plate assigned into the order by hand.
  236. # A target of 0 is legal and means the plate is not wanted. Neither owes
  237. # anything, so neither can be stranded.
  238. if not target:
  239. return False
  240. item_scope = (
  241. PrintQueueItem.plate_id == item.plate_id if item.plate_id is not None else PrintQueueItem.plate_id.is_(None)
  242. )
  243. survivor = (
  244. await db.execute(
  245. select(PrintQueueItem.id)
  246. .where(PrintQueueItem.batch_id == item.batch_id)
  247. .where(item_scope)
  248. .where(PrintQueueItem.id != item.id)
  249. .limit(1)
  250. )
  251. ).scalar_one_or_none()
  252. return survivor is None
  253. async def _assert_can_dispatch_batch_sources(
  254. db: AsyncSession, batch_id: int, current_user: User | ApiKeyActor | None, printer_scope: PrinterScope
  255. ) -> None:
  256. """Apply the ``POST /queue/`` source-file gates to everything a dispatch would print.
  257. Dispatching clones existing queue items, so without this it would be a
  258. weaker door to the same outcome: a caller holding QUEUE_CREATE and
  259. QUEUE_UPDATE_ALL but explicitly denied ``archives:reprint_*`` could start
  260. prints through an order that ``POST /queue/`` would have refused them.
  261. Every distinct source among the batch's items is checked, including the
  262. library files behind cross-model variants — those get cloned too, and any
  263. one of them may be the file that actually runs.
  264. """
  265. archive_ids = set(
  266. (
  267. await db.execute(
  268. select(PrintQueueItem.archive_id)
  269. .where(PrintQueueItem.batch_id == batch_id)
  270. .where(PrintQueueItem.archive_id.is_not(None))
  271. .distinct()
  272. )
  273. )
  274. .scalars()
  275. .all()
  276. )
  277. library_file_ids = set(
  278. (
  279. await db.execute(
  280. select(PrintQueueItem.library_file_id)
  281. .where(PrintQueueItem.batch_id == batch_id)
  282. .where(PrintQueueItem.library_file_id.is_not(None))
  283. .distinct()
  284. )
  285. )
  286. .scalars()
  287. .all()
  288. )
  289. library_file_ids |= set(
  290. (
  291. await db.execute(
  292. select(PrintQueueVariant.library_file_id)
  293. .join(PrintQueueItem, PrintQueueVariant.queue_item_id == PrintQueueItem.id)
  294. .where(PrintQueueItem.batch_id == batch_id)
  295. .distinct()
  296. )
  297. )
  298. .scalars()
  299. .all()
  300. )
  301. for archive_id in archive_ids:
  302. archive = (await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))).scalar_one_or_none()
  303. # A deleted source can't be printed; dispatch will fail on it anyway.
  304. if archive is not None:
  305. _assert_can_queue_archive(archive, current_user, printer_scope)
  306. for library_file_id in library_file_ids:
  307. library_file = (
  308. await db.execute(LibraryFile.active().where(LibraryFile.id == library_file_id))
  309. ).scalar_one_or_none()
  310. if library_file is not None:
  311. _assert_can_queue_library_file(library_file, current_user)
  312. async def _resolve_source_path(db: AsyncSession, item: PrintQueueItem) -> Path | None:
  313. """Resolve an existing queue item's source 3MF on disk, or None."""
  314. if item.archive_id:
  315. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  316. archive = result.scalar_one_or_none()
  317. if archive:
  318. return settings.base_dir / archive.file_path
  319. elif item.library_file_id:
  320. result = await db.execute(LibraryFile.active().where(LibraryFile.id == item.library_file_id))
  321. library_file = result.scalar_one_or_none()
  322. if library_file:
  323. lib_path = Path(library_file.file_path)
  324. return lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  325. return None
  326. async def _trusted_item_estimated_cost(
  327. db: AsyncSession,
  328. item: PrintQueueItem,
  329. *,
  330. printer_id: int | None,
  331. plate_id: int | None,
  332. ams_mapping: list[int] | str | None,
  333. ) -> float | None:
  334. """Recompute an existing queue item's cost from its persisted source."""
  335. if item.archive_id:
  336. archive = await db.scalar(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  337. return await estimate_queue_source_cost(
  338. db,
  339. archive=archive,
  340. plate_id=plate_id,
  341. ams_mapping=ams_mapping,
  342. printer_id=printer_id,
  343. )
  344. if item.library_file_id:
  345. library_file = await db.scalar(LibraryFile.active().where(LibraryFile.id == item.library_file_id))
  346. return await estimate_queue_source_cost(
  347. db,
  348. library_file=library_file,
  349. plate_id=plate_id,
  350. ams_mapping=ams_mapping,
  351. printer_id=printer_id,
  352. )
  353. result = await db.execute(
  354. select(PrintQueueVariant, LibraryFile)
  355. .join(LibraryFile, LibraryFile.id == PrintQueueVariant.library_file_id)
  356. .where(PrintQueueVariant.queue_item_id == item.id)
  357. )
  358. estimates = [
  359. await estimate_queue_source_cost(
  360. db,
  361. library_file=library_file,
  362. plate_id=variant.plate_id,
  363. ams_mapping=variant.ams_mapping,
  364. printer_id=printer_id,
  365. )
  366. for variant, library_file in result.all()
  367. ]
  368. if not estimates or any(cost is None for cost in estimates):
  369. return None
  370. return max(cost for cost in estimates if cost is not None)
  371. def _enrich_response(item: PrintQueueItem) -> PrintQueueItemResponse:
  372. """Add nested archive/printer/library_file info to response."""
  373. # Parse ams_mapping from JSON string BEFORE model_validate
  374. ams_mapping_parsed = None
  375. if item.ams_mapping:
  376. try:
  377. ams_mapping_parsed = json.loads(item.ams_mapping)
  378. except json.JSONDecodeError:
  379. ams_mapping_parsed = None
  380. # Parse required_filament_types from JSON string
  381. required_filament_types_parsed = None
  382. if item.required_filament_types:
  383. try:
  384. required_filament_types_parsed = json.loads(item.required_filament_types)
  385. except json.JSONDecodeError:
  386. required_filament_types_parsed = None
  387. # Parse filament_overrides from JSON string
  388. filament_overrides_parsed = None
  389. if item.filament_overrides:
  390. try:
  391. filament_overrides_parsed = json.loads(item.filament_overrides)
  392. except json.JSONDecodeError:
  393. filament_overrides_parsed = None
  394. # Parse nozzle_mapping from JSON string (#1780 — H2C rack slicer-pick
  395. # preservation). Nullable opaque JSON blob stored verbatim from
  396. # BambuStudio's project_file; surface it parsed for the response model
  397. # and any future "edit print → nozzle" UI.
  398. nozzle_mapping_parsed = None
  399. if item.nozzle_mapping:
  400. try:
  401. nozzle_mapping_parsed = json.loads(item.nozzle_mapping)
  402. except json.JSONDecodeError:
  403. nozzle_mapping_parsed = None
  404. # The operator's rack-position pick (#1784), keyed by filament group. Sent
  405. # parsed so the print dialog can show which hotend each group will use.
  406. nozzle_rack_choice_parsed = None
  407. if item.nozzle_rack_choice:
  408. try:
  409. nozzle_rack_choice_parsed = json.loads(item.nozzle_rack_choice)
  410. except json.JSONDecodeError:
  411. nozzle_rack_choice_parsed = None
  412. nozzles_info_parsed = None
  413. if item.nozzles_info:
  414. try:
  415. nozzles_info_parsed = json.loads(item.nozzles_info)
  416. except json.JSONDecodeError:
  417. nozzles_info_parsed = None
  418. # Create response with parsed ams_mapping
  419. item_dict = {
  420. "id": item.id,
  421. "printer_id": item.printer_id,
  422. "target_model": item.target_model,
  423. "target_location": item.target_location,
  424. "required_filament_types": required_filament_types_parsed,
  425. "filament_overrides": filament_overrides_parsed,
  426. "waiting_reason": item.waiting_reason,
  427. "archive_id": item.archive_id,
  428. "library_file_id": item.library_file_id,
  429. "cost_center_id": item.cost_center_id,
  430. "estimated_cost": item.estimated_cost,
  431. "position": item.position,
  432. "scheduled_time": item.scheduled_time,
  433. "require_previous_success": item.require_previous_success,
  434. "auto_off_after": item.auto_off_after,
  435. "manual_start": item.manual_start,
  436. "filament_short": bool(item.filament_short),
  437. "skip_filament_check": bool(item.skip_filament_check),
  438. "ams_mapping": ams_mapping_parsed,
  439. "plate_id": item.plate_id,
  440. "bed_levelling": item.bed_levelling,
  441. "flow_cali": item.flow_cali,
  442. "vibration_cali": item.vibration_cali,
  443. "layer_inspect": item.layer_inspect,
  444. "timelapse": item.timelapse,
  445. "use_ams": item.use_ams,
  446. "nozzle_offset_cali": item.nozzle_offset_cali,
  447. "confirm_outcome": item.confirm_outcome,
  448. "preheat_override": item.preheat_override,
  449. "preheat_chamber_target_override": item.preheat_chamber_target_override,
  450. "status": item.status,
  451. "started_at": item.started_at,
  452. "completed_at": item.completed_at,
  453. "error_message": item.error_message,
  454. "created_at": item.created_at,
  455. # User tracking (Issue #206)
  456. "created_by_id": item.created_by_id,
  457. "created_by_username": item.created_by.username if item.created_by else None,
  458. # Batch grouping
  459. "batch_id": item.batch_id,
  460. "batch_name": item.batch.name if item.batch else None,
  461. # SJF scheduling
  462. "been_jumped": item.been_jumped,
  463. # Auto-print G-code injection
  464. "gcode_injection": item.gcode_injection,
  465. # H2C rack-swap nozzle pick (#1780)
  466. "nozzle_mapping": nozzle_mapping_parsed,
  467. "nozzle_rack_choice": nozzle_rack_choice_parsed,
  468. "nozzles_info": nozzles_info_parsed,
  469. "cleanup_library_after_dispatch": item.cleanup_library_after_dispatch,
  470. # Cross-model alternatives (#671). Guarded rather than read directly:
  471. # every route that reaches here eager-loads the relationship, but a
  472. # caller that forgets would trigger a lazy load, and a lazy load on an
  473. # async session raises rather than degrading. An empty list is the
  474. # correct answer for the ordinary item this would most likely be.
  475. "variants": _variant_summaries(item),
  476. }
  477. response = PrintQueueItemResponse(**item_dict)
  478. if item.archive:
  479. # Soft-deleted archive: files are gone from disk but the row stays
  480. # (its filament/cost contribution still flows into stats per #1343).
  481. # Suppress the archive-derived UI surface so the queue page doesn't
  482. # 404-storm the thumbnail / plates / plate-thumbnail endpoints — the
  483. # frontend's existing truthy gate on archive_thumbnail covers it
  484. # (#1348 follow-up). The archive_deleted flag lets the UI render a
  485. # "source deleted" badge on these rows.
  486. if item.archive.deleted_at is not None:
  487. response.archive_deleted = True
  488. else:
  489. response.archive_name = item.archive.print_name or item.archive.filename
  490. response.archive_thumbnail = item.archive.thumbnail_path
  491. response.print_time_seconds = item.archive.print_time_seconds
  492. response.filament_used_grams = item.archive.filament_used_grams
  493. response.filament_type = item.archive.filament_type
  494. response.filament_color = item.archive.filament_color
  495. response.layer_height = item.archive.layer_height
  496. response.nozzle_diameter = item.archive.nozzle_diameter
  497. response.sliced_for_model = item.archive.sliced_for_model
  498. response.bed_type = item.archive.bed_type
  499. # Marks history/reprint rows whose archive carries the slicer's own
  500. # live-resolved AMS-slot pick (extra_data.slicer_ams_mapping) — see
  501. # `_extract_slicer_ams_mapping_json` in virtual_printer/manager.py.
  502. #
  503. # Only when the saved mapping was resolved against *this* row's
  504. # printer: a global tray ID means nothing on another printer, so
  505. # that's the exact condition under which the mapping is reused. A
  506. # badge on a row where nothing gets reused would be a lie (#2700
  507. # review). Model-based rows (printer_id None) never match, which is
  508. # correct — the mapping is not reused there either.
  509. extra = item.archive.extra_data if isinstance(item.archive.extra_data, dict) else {}
  510. saved_mapping = extra.get("slicer_ams_mapping")
  511. response.archive_has_slicer_ams_mapping = (
  512. isinstance(saved_mapping, dict)
  513. and isinstance(saved_mapping.get("mapping"), list)
  514. and item.printer_id is not None
  515. and saved_mapping.get("printer_id") == item.printer_id
  516. )
  517. if item.plate_id:
  518. archive_path = settings.base_dir / item.archive.file_path
  519. if archive_path.exists():
  520. # One cached parse for all three per-plate overrides (#2573).
  521. plate_meta = extract_plate_metadata_from_3mf(archive_path, item.plate_id)
  522. if plate_meta.print_time_seconds is not None:
  523. response.print_time_seconds = plate_meta.print_time_seconds
  524. if plate_meta.filament_used_grams > 0:
  525. response.filament_used_grams = plate_meta.filament_used_grams
  526. if plate_meta.bed_type:
  527. response.bed_type = plate_meta.bed_type
  528. if item.library_file:
  529. response.library_file_name = (
  530. item.library_file.file_metadata.get("print_name") if item.library_file.file_metadata else None
  531. )
  532. if not response.library_file_name:
  533. response.library_file_name = item.library_file.filename
  534. response.library_file_thumbnail = item.library_file.thumbnail_path
  535. # Get metadata from library file if no archive
  536. if not item.archive and item.library_file.file_metadata:
  537. response.print_time_seconds = item.library_file.file_metadata.get("print_time_seconds")
  538. response.filament_used_grams = item.library_file.file_metadata.get("filament_used_grams")
  539. response.filament_type = item.library_file.file_metadata.get("filament_type")
  540. response.filament_color = item.library_file.file_metadata.get("filament_color")
  541. response.layer_height = item.library_file.file_metadata.get("layer_height")
  542. response.nozzle_diameter = item.library_file.file_metadata.get("nozzle_diameter")
  543. response.sliced_for_model = item.library_file.file_metadata.get("sliced_for_model")
  544. response.bed_type = item.library_file.file_metadata.get("bed_type")
  545. if item.plate_id:
  546. lib_path = Path(item.library_file.file_path)
  547. library_file_path = lib_path if lib_path.is_absolute() else settings.base_dir / item.library_file.file_path
  548. if library_file_path.exists():
  549. # One cached parse for all three per-plate overrides (#2573).
  550. plate_meta = extract_plate_metadata_from_3mf(library_file_path, item.plate_id)
  551. if plate_meta.print_time_seconds is not None:
  552. response.print_time_seconds = plate_meta.print_time_seconds
  553. if plate_meta.filament_used_grams > 0:
  554. response.filament_used_grams = plate_meta.filament_used_grams
  555. if plate_meta.bed_type:
  556. response.bed_type = plate_meta.bed_type
  557. if item.printer:
  558. response.printer_name = item.printer.name
  559. return response
  560. @router.get("/", response_model=list[PrintQueueItemResponse])
  561. async def list_queue(
  562. printer_id: int | None = Query(None, description="Filter by printer (-1 for unassigned)"),
  563. status: str | None = Query(None, description="Filter by status"),
  564. target_model: str | None = Query(
  565. None, description="Filter by target model (also includes model-based items when combined with printer_id)"
  566. ),
  567. db: AsyncSession = Depends(get_db),
  568. auth_result: tuple[User | None, bool] = Depends(
  569. require_ownership_permission(
  570. Permission.QUEUE_READ_ALL,
  571. Permission.QUEUE_READ_OWN,
  572. )
  573. ),
  574. printer_scope: PrinterScope = RequestPrinterScope,
  575. ):
  576. """List all queue items, optionally filtered by printer or status."""
  577. user, can_read_all = auth_result
  578. query = (
  579. select(PrintQueueItem)
  580. .options(
  581. selectinload(PrintQueueItem.archive),
  582. selectinload(PrintQueueItem.printer),
  583. selectinload(PrintQueueItem.library_file),
  584. selectinload(PrintQueueItem.created_by),
  585. selectinload(PrintQueueItem.batch),
  586. # Cross-model candidates (#671) and their files, for the card label.
  587. selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file),
  588. )
  589. # The order the scheduler dispatches in (#3200), so the first pending
  590. # item for a printer is the one it will start next -- which is what the
  591. # printer card's "Next in queue" shows. Sorting by printer first put
  592. # every "Any <model>" job (no printer_id) ahead of a job pinned to that
  593. # printer, whatever their positions.
  594. .order_by(PrintQueueItem.position, PrintQueueItem.id)
  595. )
  596. if user is not None and not can_read_all:
  597. query = query.where(PrintQueueItem.created_by_id == user.id)
  598. # Items bound to printers the caller can't see stay out (#1727)
  599. if (clause := printer_scope.where(PrintQueueItem.printer_id)) is not None:
  600. query = query.where(clause)
  601. if printer_id is not None:
  602. if printer_id == -1:
  603. # Special value: filter for unassigned items
  604. query = query.where(PrintQueueItem.printer_id.is_(None))
  605. else:
  606. # Resolve effective model: prefer explicit param, fall back to printer's DB model.
  607. # This ensures model-based "Any X" items are returned even when the frontend
  608. # doesn't send target_model (e.g. printer.model is NULL on the client side).
  609. effective_model = target_model
  610. if not effective_model:
  611. printer_row = (
  612. await db.execute(select(Printer.model).where(Printer.id == printer_id))
  613. ).scalar_one_or_none()
  614. effective_model = printer_row
  615. if effective_model:
  616. # Include both printer-specific items AND model-based (unassigned) items
  617. query = query.where(
  618. or_(
  619. PrintQueueItem.printer_id == printer_id,
  620. and_(
  621. PrintQueueItem.printer_id.is_(None),
  622. func.lower(PrintQueueItem.target_model) == effective_model.lower(),
  623. ),
  624. )
  625. )
  626. else:
  627. query = query.where(PrintQueueItem.printer_id == printer_id)
  628. elif target_model:
  629. query = query.where(func.lower(PrintQueueItem.target_model) == target_model.lower())
  630. if status:
  631. query = query.where(PrintQueueItem.status == status)
  632. result = await db.execute(query)
  633. items = result.scalars().all()
  634. return [_enrich_response(item) for item in items]
  635. async def _resolve_queue_variants(
  636. db: AsyncSession,
  637. specs: list[QueueVariantCreate],
  638. current_user: User | ApiKeyActor | None,
  639. ) -> list[tuple[QueueVariantCreate, LibraryFile, str]]:
  640. """Validate a cross-model candidate set and pair each file with its model (#671).
  641. Validated as a set, not file by file, because the failure modes are about the
  642. set: two candidates for the same printer give the resolver no basis to choose,
  643. and a set where nothing can ever run is a job that waits forever.
  644. At least one candidate must have an active printer — the rest may not, which
  645. is deliberate. Grouping the H2C slice before the H2C arrives is a reasonable
  646. thing to do, and refusing the whole queue action over it would be worse than
  647. letting that candidate simply never match.
  648. """
  649. file_ids = [s.library_file_id for s in specs]
  650. if len(set(file_ids)) != len(file_ids):
  651. raise HTTPException(400, "The same file cannot be listed twice as a variant")
  652. rows = (await db.execute(LibraryFile.active().where(LibraryFile.id.in_(file_ids)))).scalars().all()
  653. by_id = {f.id: f for f in rows}
  654. resolved: list[tuple[QueueVariantCreate, LibraryFile, str]] = []
  655. seen_models: dict[str, str] = {}
  656. any_active_printer = False
  657. for spec in specs:
  658. library_file = by_id.get(spec.library_file_id)
  659. # Same IDOR posture as the single-file path: a file the caller cannot read
  660. # is reported as missing rather than forbidden.
  661. if not library_file or (
  662. current_user
  663. and not current_user.has_permission(Permission.LIBRARY_READ_ALL.value)
  664. and (library_file.created_by_id is None or library_file.created_by_id != current_user.id)
  665. ):
  666. raise HTTPException(404, f"Library file not found: {spec.library_file_id}")
  667. from backend.app.utils.filename import InvalidFilenameError, validate_print_filename
  668. try:
  669. validate_print_filename(library_file.filename)
  670. except InvalidFilenameError as e:
  671. raise HTTPException(400, str(e)) from e
  672. model = resolve_variant_model(library_file, spec.target_model)
  673. if not model:
  674. raise HTTPException(
  675. 400,
  676. f"{library_file.filename} does not say which printer it was sliced for — "
  677. "set its target model explicitly",
  678. )
  679. # Cross-model safety gate (#2578), per candidate. A set is only as safe as
  680. # its worst member, and model-based dispatch has no human in the loop.
  681. sliced_for = (library_file.file_metadata or {}).get("sliced_for_model")
  682. if not is_gcode_compatible(sliced_for, model):
  683. raise HTTPException(
  684. 400,
  685. f"{library_file.filename} was sliced for {sliced_for} and cannot be dispatched to {model} printers",
  686. )
  687. if model in seen_models:
  688. raise HTTPException(
  689. 400,
  690. f"{library_file.filename} and {seen_models[model]} are both for {model} — "
  691. "variants must target different printers",
  692. )
  693. seen_models[model] = library_file.filename
  694. has_printer = (
  695. (
  696. await db.execute(
  697. select(Printer).where(Printer.model == model).where(Printer.is_active == True) # noqa: E712
  698. )
  699. )
  700. .scalars()
  701. .first()
  702. )
  703. any_active_printer = any_active_printer or bool(has_printer)
  704. resolved.append((spec, library_file, model))
  705. if not any_active_printer:
  706. raise HTTPException(400, f"No active printers for any of: {', '.join(seen_models)}")
  707. return resolved
  708. def _variant_values(
  709. spec: QueueVariantCreate,
  710. library_file: LibraryFile,
  711. model: str,
  712. position: int,
  713. ) -> dict:
  714. """Column values for one candidate, extracted from its own 3MF.
  715. Each candidate is a different slice, so its filament requirements and print
  716. time come from its own file rather than being inherited from the item.
  717. Returns values rather than a row so a quantity>1 batch can build one row per
  718. copy without re-opening the 3MF for each.
  719. """
  720. lib_path = Path(library_file.file_path)
  721. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  722. required_types = None
  723. filament_overrides_json = None
  724. print_time = (library_file.file_metadata or {}).get("print_time_seconds")
  725. if file_path.exists():
  726. types = _extract_filament_types_from_3mf(file_path, spec.plate_id)
  727. if types:
  728. required_types = json.dumps(types)
  729. if spec.plate_id:
  730. plate_time = _extract_print_time_from_3mf(file_path, spec.plate_id)
  731. if plate_time is not None:
  732. print_time = plate_time
  733. if spec.filament_overrides:
  734. plate_overrides = overrides_for_plate(spec.filament_overrides, file_path, spec.plate_id)
  735. if plate_overrides:
  736. filament_overrides_json = json.dumps(plate_overrides)
  737. override_types = sorted({o["type"] for o in plate_overrides if "type" in o})
  738. if override_types:
  739. existing = set(json.loads(required_types)) if required_types else set()
  740. required_types = json.dumps(sorted(existing | set(override_types)))
  741. return {
  742. "position": position,
  743. "library_file_id": library_file.id,
  744. "target_model": model,
  745. "plate_id": spec.plate_id,
  746. "ams_mapping": json.dumps(spec.ams_mapping) if spec.ams_mapping else None,
  747. "nozzle_mapping": json.dumps(spec.nozzle_mapping) if spec.nozzle_mapping else None,
  748. "nozzle_rack_choice": json.dumps(spec.nozzle_rack_choice) if spec.nozzle_rack_choice else None,
  749. "filament_overrides": filament_overrides_json,
  750. "required_filament_types": required_types,
  751. "print_time_seconds": print_time,
  752. }
  753. @router.post("/", response_model=PrintQueueItemResponse)
  754. async def add_to_queue(
  755. data: PrintQueueItemCreate,
  756. db: AsyncSession = Depends(get_db),
  757. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  758. printer_scope: PrinterScope = RequestPrinterScope,
  759. review_required: bool = QueueReviewRequired,
  760. actor: User | ApiKeyActor | None = RequestActor,
  761. ):
  762. """Add an item to the print queue."""
  763. # 0 is no file, as on the checks below; stored, it would point at nothing.
  764. data.archive_id = data.archive_id or None
  765. data.library_file_id = data.library_file_id or None
  766. # Normalize target_model (e.g., "Bambu Lab X1E" / "C13" -> "X1E").
  767. # normalize_model_name resolves internal codes first: the previous
  768. # `normalize_printer_model(x) or normalize_printer_model_id(x)` chain never
  769. # reached the code map, because the first call returns unknown input
  770. # unchanged — so a "C13" target stayed "C13", matched no printer row and
  771. # left the item waiting forever. Identical result for every other spelling.
  772. target_model_norm = normalize_model_name(data.target_model)
  773. # Cross-model alternatives (#671): several sliced files, whichever printer
  774. # frees up first. The whole candidate set is validated before anything is
  775. # written — a half-valid set would produce a job that can only reach some of
  776. # the printers the user asked for, with nothing to say which.
  777. variant_specs: list[tuple[QueueVariantCreate, LibraryFile, str]] = []
  778. if data.variants:
  779. if data.printer_id:
  780. raise HTTPException(
  781. 400, "Cannot specify both printer_id and variants — pick a printer or offer alternatives"
  782. )
  783. if data.archive_id or data.library_file_id:
  784. raise HTTPException(
  785. 400, "Cannot combine variants with archive_id or library_file_id — the variants are the files"
  786. )
  787. variant_specs = await _resolve_queue_variants(db, data.variants, actor)
  788. # Mirror the first candidate onto the item so the queue listing, the SJF
  789. # grouping and the "Any H2S" label have something before a printer is
  790. # picked. Resolution overwrites it with whichever candidate actually runs.
  791. target_model_norm = variant_specs[0][2]
  792. # Validate that either archive_id or library_file_id is provided.
  793. # A cross-model item deliberately holds neither: its files live on the
  794. # variant rows. Pointing library_file_id at one of them would be worse than
  795. # useless — that FK is ON DELETE CASCADE, so deleting a single alternative
  796. # would take the whole queue item with it.
  797. if not data.archive_id and not data.library_file_id and not data.variants:
  798. raise HTTPException(400, "Either archive_id or library_file_id must be provided")
  799. # Cannot specify both printer_id and target_model
  800. if data.printer_id and target_model_norm:
  801. raise HTTPException(400, "Cannot specify both printer_id and target_model")
  802. if target_model_norm:
  803. # The key itself, not its actor: the scheduler holds an any-printer job
  804. # to its creator's printers, and a key may be limited to fewer of them.
  805. ensure_model_target_allowed(current_user, printer_scope)
  806. # Validate printer exists (if assigned)
  807. if data.printer_id is not None:
  808. printer_scope.ensure(data.printer_id)
  809. result = await db.execute(select(Printer).where(Printer.id == data.printer_id))
  810. if not result.scalar_one_or_none():
  811. raise HTTPException(400, "Printer not found")
  812. # Validate target_model has active printers. Skipped for cross-model items:
  813. # target_model there is just the first candidate, and _resolve_queue_variants
  814. # has already required that *some* candidate has a printer.
  815. if target_model_norm and not data.variants:
  816. result = await db.execute(
  817. select(Printer).where(Printer.model == target_model_norm).where(Printer.is_active == True) # noqa: E712
  818. )
  819. if not result.scalars().first():
  820. raise HTTPException(400, f"No active printers for model: {target_model_norm}")
  821. # Validate archive exists (if provided) and get it for filament extraction
  822. archive = None
  823. if data.archive_id:
  824. result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  825. archive = result.scalar_one_or_none()
  826. # Missing answers like hidden (404 from the gate below).
  827. if not archive:
  828. raise HTTPException(404, "Archive not found")
  829. _assert_can_queue_archive(archive, actor, printer_scope)
  830. # Validate library file exists (if provided) and get it for filament extraction
  831. library_file = None
  832. if data.library_file_id:
  833. result = await db.execute(LibraryFile.active().where(LibraryFile.id == data.library_file_id))
  834. library_file = result.scalar_one_or_none()
  835. # Missing answers like hidden (404 from the gate below).
  836. if not library_file:
  837. raise HTTPException(404, "Library file not found")
  838. _assert_can_queue_library_file(library_file, actor)
  839. # Bambu SD card is FAT32/exFAT — illegal filename chars would 553 at
  840. # FTP upload time (#1540). Reject at queue time so the user gets the
  841. # actionable error before waiting in queue.
  842. from backend.app.utils.filename import InvalidFilenameError, validate_print_filename
  843. try:
  844. validate_print_filename(library_file.filename)
  845. except InvalidFilenameError as e:
  846. raise HTTPException(400, str(e)) from e
  847. # Cross-model safety gate (#2578): a G-code 3MF sliced for one model must
  848. # not be queued for dispatch to an incompatible model. The UI can no longer
  849. # produce such rows, but API-created rows must be rejected here too — the
  850. # scheduler assigns model-based items to hardware with no human in the loop.
  851. if target_model_norm:
  852. sliced_for = None
  853. if archive:
  854. sliced_for = archive.sliced_for_model
  855. elif library_file and library_file.file_metadata:
  856. sliced_for = library_file.file_metadata.get("sliced_for_model")
  857. if not is_gcode_compatible(sliced_for, target_model_norm):
  858. raise HTTPException(
  859. 400,
  860. f"File was sliced for {sliced_for} and cannot be dispatched to {target_model_norm} printers",
  861. )
  862. # Extract filament types for model-based assignment (used by scheduler for validation)
  863. required_filament_types = None
  864. file_path = None
  865. # Get file path from archive or library file
  866. if archive:
  867. file_path = settings.base_dir / archive.file_path
  868. elif library_file:
  869. lib_path = Path(library_file.file_path)
  870. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  871. if target_model_norm:
  872. if file_path and file_path.exists():
  873. filament_types = _extract_filament_types_from_3mf(file_path, data.plate_id)
  874. if filament_types:
  875. required_filament_types = json.dumps(filament_types)
  876. logger.info("Extracted filament types for model-based queue: %s", filament_types)
  877. # If filament overrides are provided, update required_filament_types to match override types.
  878. # A specific-printer job keeps its overrides too (#3133): an override chosen for
  879. # "Any P2S" survives the switch to one P2S in the print dialog, and when the
  880. # dialog could not resolve every tray the scheduler recomputes the mapping at
  881. # dispatch — against the 3MF's filament, unless the row still says otherwise.
  882. # The type list below stays model-only; it gates which printer of a model is
  883. # eligible, which a printer-targeted job has already settled.
  884. filament_overrides_json = None
  885. if data.filament_overrides and (target_model_norm or data.printer_id is not None):
  886. plate_overrides = overrides_for_plate(data.filament_overrides, file_path, data.plate_id)
  887. if plate_overrides:
  888. filament_overrides_json = json.dumps(plate_overrides)
  889. # Update required_filament_types from overrides so scheduler validates against overridden types
  890. override_types = sorted({o["type"] for o in plate_overrides if "type" in o})
  891. if override_types and target_model_norm:
  892. # Merge with existing types (overrides may only cover some slots)
  893. existing_types = set(json.loads(required_filament_types)) if required_filament_types else set()
  894. # Replace types for overridden slots, keep others
  895. all_types = existing_types | set(override_types)
  896. required_filament_types = json.dumps(sorted(all_types))
  897. # Validate quantity
  898. quantity = max(1, data.quantity)
  899. # Validate batch_id if provided. Client passes batch_id when adding items
  900. # into a pre-created batch (multi-plate auto-batch or "Group as batch" flow).
  901. # 404 keeps the existing-id leak surface low.
  902. batch = None
  903. batch_id = None
  904. if data.batch_id is not None:
  905. result = await db.execute(select(PrintBatch).where(PrintBatch.id == data.batch_id))
  906. existing_batch = result.scalar_one_or_none()
  907. if not existing_batch:
  908. raise HTTPException(404, "Batch not found")
  909. if existing_batch.status != "active":
  910. raise HTTPException(400, "Cannot add items to a non-active batch")
  911. if (
  912. actor is not None
  913. and existing_batch.created_by_id is not None
  914. and existing_batch.created_by_id != actor.id
  915. and not actor.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  916. ):
  917. raise HTTPException(404, "Batch not found")
  918. batch = existing_batch
  919. batch_id = existing_batch.id
  920. # Create batch if quantity > 1 and no batch_id provided
  921. if batch_id is None and quantity > 1:
  922. # Derive batch name from source file
  923. batch_name_base = "Batch"
  924. if archive:
  925. batch_name_base = archive.print_name or archive.filename or "Batch"
  926. elif library_file:
  927. if library_file.file_metadata:
  928. batch_name_base = library_file.file_metadata.get("print_name") or library_file.filename
  929. else:
  930. batch_name_base = library_file.filename
  931. elif variant_specs:
  932. # A cross-model job carries neither archive_id nor library_file_id --
  933. # the candidates are the files (#671) -- so both branches above miss
  934. # and every such batch was named "Batch". Unreachable until the print
  935. # dialog could ask for more than one copy of one (#3101). Name it
  936. # after the first candidate, which is what the dialog names the job
  937. # after and what the resolver prefers when both printers are free.
  938. first_file = variant_specs[0][1]
  939. batch_name_base = (first_file.file_metadata or {}).get("print_name") or first_file.filename or "Batch"
  940. batch_name_base = batch_name_base.replace(".gcode.3mf", "").replace(".3mf", "")
  941. batch = PrintBatch(
  942. name=f"{batch_name_base} ×{quantity}",
  943. archive_id=data.archive_id,
  944. library_file_id=data.library_file_id,
  945. quantity=quantity,
  946. status="active",
  947. created_by_id=actor.id if actor else None,
  948. )
  949. db.add(batch)
  950. await db.flush() # Get batch.id before creating items
  951. batch_id = batch.id
  952. # Positions are one sequence across every pending item (#3200), so a new
  953. # item lands relative to the whole list, not to its printer's share of it.
  954. queue_scope = (PrintQueueItem.status == "pending",)
  955. await lock_queue_positions(db)
  956. insert_position = max(1, data.insert_position or 1)
  957. if data.insert_at_top or data.insert_position is not None:
  958. max_pos = await max_queue_position(db)
  959. insert_position = min(insert_position, max_pos + 1)
  960. await db.execute(
  961. update(PrintQueueItem)
  962. .where(*queue_scope)
  963. .where(PrintQueueItem.position >= insert_position)
  964. .values(position=PrintQueueItem.position + quantity)
  965. )
  966. start_position = insert_position
  967. else:
  968. start_position = await max_queue_position(db) + 1
  969. # Resolve print_time_seconds for SJF scheduling (cache on item at creation)
  970. cached_print_time = None
  971. if archive:
  972. cached_print_time = archive.print_time_seconds
  973. if data.plate_id:
  974. archive_path = settings.base_dir / archive.file_path
  975. if archive_path.exists():
  976. plate_time = _extract_print_time_from_3mf(archive_path, data.plate_id)
  977. if plate_time is not None:
  978. cached_print_time = plate_time
  979. elif library_file:
  980. if library_file.file_metadata:
  981. cached_print_time = library_file.file_metadata.get("print_time_seconds")
  982. if data.plate_id:
  983. lib_path = Path(library_file.file_path)
  984. library_file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  985. if library_file_path.exists():
  986. plate_time = _extract_print_time_from_3mf(library_file_path, data.plate_id)
  987. if plate_time is not None:
  988. cached_print_time = plate_time
  989. # Validate project exists before insert so a bogus ID yields 404, not an FK-constraint 500
  990. if data.project_id is not None:
  991. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  992. if not project_result.scalar_one_or_none():
  993. raise HTTPException(status_code=404, detail="Project not found")
  994. # Security boundary: the browser's estimated_cost is only a display hint.
  995. # Budget enforcement and the persisted reservation value must be derived
  996. # from the server-owned archive/library metadata and spool assignments.
  997. if variant_specs:
  998. variant_costs = [
  999. await estimate_queue_source_cost(
  1000. db,
  1001. library_file=variant_file,
  1002. plate_id=spec.plate_id,
  1003. ams_mapping=spec.ams_mapping,
  1004. printer_id=data.printer_id,
  1005. )
  1006. for spec, variant_file, _model in variant_specs
  1007. ]
  1008. trusted_estimated_cost = (
  1009. max(cost for cost in variant_costs if cost is not None)
  1010. if variant_costs and all(cost is not None for cost in variant_costs)
  1011. else None
  1012. )
  1013. else:
  1014. trusted_estimated_cost = await estimate_queue_source_cost(
  1015. db,
  1016. archive=archive,
  1017. library_file=library_file,
  1018. plate_id=data.plate_id,
  1019. ams_mapping=data.ams_mapping,
  1020. printer_id=data.printer_id,
  1021. )
  1022. await validate_print_budget(
  1023. db,
  1024. cost_center_id=data.cost_center_id,
  1025. estimated_cost=trusted_estimated_cost,
  1026. current_user=actor,
  1027. quantity=quantity,
  1028. )
  1029. ams_mapping_json = json.dumps(data.ams_mapping) if data.ams_mapping else None
  1030. # Same Text-as-JSON convention for the rack-position pick (#1784).
  1031. nozzle_rack_choice_json = json.dumps(data.nozzle_rack_choice) if data.nozzle_rack_choice else None
  1032. # Reprint fallback: the caller didn't specify an explicit ams_mapping (no
  1033. # per-slot filament-mapping edit was made), but the archive carries the
  1034. # slicer's own live-resolved AMS-slot pick from the original print (see
  1035. # `extra_data.slicer_ams_mapping`, written by the VP-queue path via
  1036. # `_extract_slicer_ams_mapping_json`). Reuse it so the reprint dispatches
  1037. # to the exact same physical spool instead of the scheduler re-deriving a
  1038. # (possibly ambiguous) mapping from just the file's static type/color.
  1039. #
  1040. # Global tray IDs only mean something relative to the specific printer
  1041. # they were resolved against, so this only fires when the reprint targets
  1042. # that exact printer (`extra_data.slicer_ams_mapping.printer_id`) — never
  1043. # for a model-based dispatch (data.printer_id is None) or a reprint aimed
  1044. # at a different printer, where the same tray number can hold a
  1045. # completely different spool (#2700 review).
  1046. #
  1047. # It also stands down when the request carries force-color-match overrides:
  1048. # those are the caller asking the scheduler to match strictly against the
  1049. # printer's live trays, and they are only ever applied inside
  1050. # `_compute_ams_mapping_for_printer` — the function a stored mapping makes
  1051. # the scheduler skip. Same precedence as the VP-side toggle pair (#2700
  1052. # review).
  1053. #
  1054. # Note this is otherwise unconditional — it applies regardless of whether
  1055. # the physical spool in that slot has changed since the original print.
  1056. # #1308 covers re-verifying a stored mapping against live AMS state at
  1057. # dispatch time; that check is a separate PR and, once merged, will also
  1058. # catch a stale slot inherited through this fallback.
  1059. wants_live_color_match = any(
  1060. isinstance(o, dict) and o.get("force_color_match") for o in (data.filament_overrides or [])
  1061. )
  1062. if (
  1063. ams_mapping_json is None
  1064. and not wants_live_color_match
  1065. and archive
  1066. and archive.extra_data
  1067. and data.printer_id is not None
  1068. ):
  1069. saved = archive.extra_data.get("slicer_ams_mapping")
  1070. if (
  1071. isinstance(saved, dict)
  1072. and saved.get("printer_id") == data.printer_id
  1073. and isinstance(saved.get("mapping"), list)
  1074. and saved["mapping"]
  1075. ):
  1076. ams_mapping_json = json.dumps(saved["mapping"])
  1077. items = []
  1078. for i in range(quantity):
  1079. item = PrintQueueItem(
  1080. printer_id=data.printer_id,
  1081. target_model=target_model_norm,
  1082. target_location=data.target_location,
  1083. required_filament_types=required_filament_types,
  1084. filament_overrides=filament_overrides_json,
  1085. archive_id=data.archive_id,
  1086. library_file_id=data.library_file_id,
  1087. cost_center_id=data.cost_center_id,
  1088. estimated_cost=trusted_estimated_cost,
  1089. scheduled_time=data.scheduled_time,
  1090. require_previous_success=data.require_previous_success,
  1091. auto_off_after=data.auto_off_after,
  1092. # Without queue:start_unreviewed the job waits for someone to
  1093. # start it, whatever the request asked for (#1620)
  1094. manual_start=data.manual_start or review_required,
  1095. skip_filament_check=data.skip_filament_check,
  1096. ams_mapping=ams_mapping_json,
  1097. nozzle_rack_choice=nozzle_rack_choice_json,
  1098. plate_id=data.plate_id,
  1099. bed_levelling=data.bed_levelling,
  1100. flow_cali=data.flow_cali,
  1101. vibration_cali=data.vibration_cali,
  1102. layer_inspect=data.layer_inspect,
  1103. timelapse=data.timelapse,
  1104. use_ams=data.use_ams,
  1105. nozzle_offset_cali=data.nozzle_offset_cali,
  1106. confirm_outcome=data.confirm_outcome,
  1107. preheat_override=data.preheat_override,
  1108. preheat_chamber_target_override=data.preheat_chamber_target_override,
  1109. gcode_injection=data.gcode_injection,
  1110. cleanup_library_after_dispatch=data.cleanup_library_after_dispatch,
  1111. project_id=data.project_id,
  1112. position=start_position + i,
  1113. status="pending",
  1114. created_by_id=actor.id if actor else None,
  1115. batch_id=batch_id,
  1116. print_time_seconds=cached_print_time,
  1117. )
  1118. db.add(item)
  1119. items.append(item)
  1120. if variant_specs:
  1121. variant_values = [
  1122. _variant_values(spec, library_file, model, position)
  1123. for position, (spec, library_file, model) in enumerate(variant_specs)
  1124. ]
  1125. # SJF orders pending items before any printer is known, so the row carries
  1126. # the shortest candidate's estimate. Resolution replaces it with the one
  1127. # that actually runs.
  1128. estimates = [v["print_time_seconds"] for v in variant_values if v["print_time_seconds"]]
  1129. for item in items:
  1130. # Each copy in a quantity>1 batch gets its own candidate rows —
  1131. # attempt counts are per-item, and two copies must be free to land on
  1132. # different printers.
  1133. item.variants.extend(PrintQueueVariant(**values) for values in variant_values)
  1134. item.print_time_seconds = min(estimates) if estimates else None
  1135. await db.commit()
  1136. # Refresh the first item for the response
  1137. item = items[0]
  1138. await db.refresh(item)
  1139. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  1140. source_name = f"archive {data.archive_id}" if data.archive_id else f"library file {data.library_file_id}"
  1141. target_desc = data.printer_id or (f"model {target_model_norm}" if target_model_norm else "unassigned")
  1142. qty_desc = f" (×{quantity})" if quantity > 1 else ""
  1143. logger.info("Added %s to queue for %s%s", source_name, target_desc, qty_desc)
  1144. # MQTT relay - publish queue job added
  1145. try:
  1146. from backend.app.services.mqtt_relay import mqtt_relay
  1147. await mqtt_relay.on_queue_job_added(
  1148. job_id=item.id,
  1149. filename=item.archive.filename if item.archive else "",
  1150. printer_id=item.printer_id,
  1151. printer_name=item.printer.name if item.printer else None,
  1152. )
  1153. except Exception:
  1154. pass # Don't fail queue add if MQTT fails
  1155. # Send notification for job added
  1156. try:
  1157. job_name = (
  1158. item.archive.filename
  1159. if item.archive
  1160. else item.library_file.filename
  1161. if item.library_file
  1162. else f"Job #{item.id}"
  1163. )
  1164. job_name = job_name.replace(".gcode.3mf", "").replace(".3mf", "")
  1165. if quantity > 1:
  1166. job_name = f"{job_name} ×{quantity}"
  1167. target = (
  1168. item.printer.name if item.printer else (f"Any {item.target_model}" if target_model_norm else "Unassigned")
  1169. )
  1170. await notification_service.on_queue_job_added(
  1171. job_name=job_name,
  1172. target=target,
  1173. db=db,
  1174. printer_id=item.printer_id,
  1175. printer_name=item.printer.name if item.printer else None,
  1176. )
  1177. except Exception:
  1178. pass # Don't fail queue add if notification fails
  1179. return _enrich_response(item)
  1180. @router.patch("/bulk", response_model=PrintQueueBulkUpdateResponse)
  1181. async def bulk_update_queue_items(
  1182. data: PrintQueueBulkUpdate,
  1183. db: AsyncSession = Depends(get_db),
  1184. auth_result: tuple[User | None, bool] = Depends(
  1185. require_ownership_permission(
  1186. Permission.QUEUE_UPDATE_ALL,
  1187. Permission.QUEUE_UPDATE_OWN,
  1188. )
  1189. ),
  1190. printer_scope: PrinterScope = RequestPrinterScope,
  1191. actor: User | ApiKeyActor | None = RequestActor,
  1192. ):
  1193. """Bulk update multiple queue items with the same values.
  1194. Only pending items can be updated. Non-pending items are skipped.
  1195. Items not owned by the user are also skipped (unless user has *_all permission).
  1196. """
  1197. user, can_modify_all = auth_result
  1198. if not data.item_ids:
  1199. raise HTTPException(400, "No item IDs provided")
  1200. # Get fields to update (exclude item_ids and unset fields)
  1201. update_data = data.model_dump(exclude={"item_ids"}, exclude_unset=True)
  1202. if not update_data:
  1203. raise HTTPException(400, "No fields to update")
  1204. # Validate printer_id if being changed
  1205. if "printer_id" in update_data and update_data["printer_id"] is not None:
  1206. printer_scope.ensure(update_data["printer_id"])
  1207. result = await db.execute(select(Printer).where(Printer.id == update_data["printer_id"]))
  1208. if not result.scalar_one_or_none():
  1209. raise HTTPException(400, "Printer not found")
  1210. # Fetch all items, minus any bound to a printer the caller can't see (#1727)
  1211. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id.in_(data.item_ids)))
  1212. items = [item for item in result.scalars().all() if printer_scope.allows(item.printer_id)]
  1213. updated_count = 0
  1214. skipped_count = 0
  1215. validates_billing_fields = "cost_center_id" in update_data or "estimated_cost" in update_data
  1216. for item in items:
  1217. # Skip non-pending rows and rows a dispatch worker has claimed (#2615) —
  1218. # editing a claimed row mid-upload would split it from the in-flight
  1219. # dispatch, so it's excluded from the bulk change (cancel to move it).
  1220. if item.status != "pending" or item.dispatching_at is not None:
  1221. skipped_count += 1
  1222. continue
  1223. # Ownership check
  1224. if not can_modify_all and item.created_by_id != user.id:
  1225. skipped_count += 1
  1226. continue
  1227. # Clearing "wait for manual start" starts the job, which needs the
  1228. # same right as the start button (#1620)
  1229. if (
  1230. item.manual_start
  1231. and "manual_start" in update_data
  1232. and not update_data["manual_start"]
  1233. and not may_start_queue_item(user, can_modify_all, item.created_by_id)
  1234. ):
  1235. skipped_count += 1
  1236. continue
  1237. item_update_data = update_data.copy()
  1238. if validates_billing_fields:
  1239. trusted_estimated_cost = await _trusted_item_estimated_cost(
  1240. db,
  1241. item,
  1242. printer_id=item_update_data.get("printer_id", item.printer_id),
  1243. plate_id=item.plate_id,
  1244. ams_mapping=item.ams_mapping,
  1245. )
  1246. item_update_data["estimated_cost"] = trusted_estimated_cost
  1247. await validate_print_budget(
  1248. db,
  1249. cost_center_id=item_update_data.get("cost_center_id", item.cost_center_id),
  1250. estimated_cost=trusted_estimated_cost,
  1251. current_user=actor,
  1252. exclude_queue_item_id=item.id,
  1253. )
  1254. for field, value in item_update_data.items():
  1255. setattr(item, field, value)
  1256. updated_count += 1
  1257. await db.commit()
  1258. logger.info("Bulk updated %s queue items, skipped %s", updated_count, skipped_count)
  1259. return PrintQueueBulkUpdateResponse(
  1260. updated_count=updated_count,
  1261. skipped_count=skipped_count,
  1262. message=f"Updated {updated_count} items"
  1263. + (f", skipped {skipped_count} non-pending/not-owned" if skipped_count else ""),
  1264. )
  1265. # --- Batch endpoints ---
  1266. def _validate_plate_targets(
  1267. plates: list[PrintBatchPlateTarget] | None,
  1268. ) -> list[PrintBatchPlateTarget] | None:
  1269. """Reject duplicate plates and orders that ask for nothing at all.
  1270. A duplicate would violate the (batch_id, plate_id) unique constraint at
  1271. flush time — and on SQLite/PostgreSQL a NULL plate_id slips past that
  1272. constraint entirely, so the check has to happen here to catch two
  1273. "whole file" rows in one order.
  1274. """
  1275. if plates is None:
  1276. return None
  1277. if not plates:
  1278. raise HTTPException(400, "plates must contain at least one plate")
  1279. seen: set[int | None] = set()
  1280. for target in plates:
  1281. if target.plate_id in seen:
  1282. label = target.plate_id if target.plate_id is not None else "whole file"
  1283. raise HTTPException(400, f"Duplicate plate in order: {label}")
  1284. seen.add(target.plate_id)
  1285. if all(target.quantity_target == 0 for target in plates):
  1286. raise HTTPException(400, "Order must request at least one print")
  1287. return plates
  1288. async def _validate_batch_project(
  1289. db: AsyncSession, project_id: int | None, current_user: User | ApiKeyActor | None
  1290. ) -> None:
  1291. """404 on a bogus project id rather than letting the FK blow up as a 500."""
  1292. if project_id is None:
  1293. return
  1294. result = await db.execute(select(Project).where(Project.id == project_id))
  1295. if not result.scalar_one_or_none():
  1296. raise HTTPException(404, "Project not found")
  1297. async def _load_batch_for_write(
  1298. db: AsyncSession, batch_id: int, current_user: User | ApiKeyActor | None, permission: Permission
  1299. ) -> PrintBatch:
  1300. """Fetch a batch the caller is allowed to modify, or 404.
  1301. 404 rather than 403 on the ownership miss, matching the rest of this
  1302. module: a 403 would confirm the id exists to someone enumerating.
  1303. """
  1304. result = await db.execute(
  1305. select(PrintBatch).options(selectinload(PrintBatch.plates)).where(PrintBatch.id == batch_id)
  1306. )
  1307. batch = result.scalar_one_or_none()
  1308. if not batch:
  1309. raise HTTPException(404, "Batch not found")
  1310. if (
  1311. current_user is not None
  1312. and batch.created_by_id is not None
  1313. and batch.created_by_id != current_user.id
  1314. and not current_user.has_permission(permission.value)
  1315. ):
  1316. raise HTTPException(404, "Batch not found")
  1317. return batch
  1318. # Deliberately without the existing batch's id: the caller may not be allowed
  1319. # to read it. They can look it up by the pair, which applies the usual
  1320. # ownership rules.
  1321. _EXTERNAL_REF_TAKEN = "A batch for this external_source and external_ref already exists"
  1322. async def _ensure_batch_in_scope(db: AsyncSession, batch_id: int, printer_scope: PrinterScope) -> None:
  1323. """404 a batch holding jobs on printers the caller can't see (#1727).
  1324. Cancelling or ungrouping acts on every member, so doing it to only the
  1325. visible ones would leave a batch that reads cancelled with jobs still
  1326. pending, and refusing outright is the honest answer.
  1327. """
  1328. if printer_scope.is_unrestricted:
  1329. return
  1330. result = await db.execute(select(PrintQueueItem.printer_id).where(PrintQueueItem.batch_id == batch_id))
  1331. if not all(printer_scope.allows(pid) for (pid,) in result.all()):
  1332. raise HTTPException(404, "Batch not found")
  1333. @router.post("/batches", response_model=PrintBatchResponse)
  1334. async def create_batch(
  1335. data: PrintBatchCreate,
  1336. db: AsyncSession = Depends(get_db),
  1337. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  1338. printer_scope: PrinterScope = RequestPrinterScope,
  1339. actor: User | ApiKeyActor | None = RequestActor,
  1340. ):
  1341. """Create a batch.
  1342. Two modes:
  1343. * ``item_ids`` provided: assign the listed pending queue items to a new
  1344. batch ("Group as batch" UI action).
  1345. * ``item_ids`` omitted/empty: create an empty batch so the client can
  1346. pass the returned ``id`` on subsequent ``POST /queue/`` calls. Used by
  1347. the multi-plate auto-batch flow in PrintModal.
  1348. ``plates`` turns the batch into an order with per-plate targets (#342):
  1349. progress is then measured against what was asked for rather than against
  1350. what happened to be queued, so a failed run still counts as owed. Omitting
  1351. it keeps the pre-#342 behaviour exactly.
  1352. """
  1353. if not data.name or not data.name.strip():
  1354. raise HTTPException(400, "Batch name is required")
  1355. plate_targets = _validate_plate_targets(data.plates)
  1356. await _validate_batch_project(db, data.project_id, actor)
  1357. # The file the batch prints: 0 is none, any other id passes the same source
  1358. # checks as POST /queue/, and a missing one answers like a hidden one.
  1359. data.archive_id = data.archive_id or None
  1360. data.library_file_id = data.library_file_id or None
  1361. if data.archive_id is not None:
  1362. batch_archive = await db.get(PrintArchive, data.archive_id)
  1363. if batch_archive is None:
  1364. raise HTTPException(404, "Archive not found")
  1365. _assert_can_queue_archive(batch_archive, actor, printer_scope)
  1366. if data.library_file_id is not None:
  1367. batch_file = (
  1368. await db.execute(LibraryFile.active().where(LibraryFile.id == data.library_file_id))
  1369. ).scalar_one_or_none()
  1370. if batch_file is None:
  1371. raise HTTPException(404, "Library file not found")
  1372. _assert_can_queue_library_file(batch_file, actor)
  1373. if data.external_source is not None:
  1374. existing = await db.execute(
  1375. select(PrintBatch.id).where(
  1376. PrintBatch.external_source == data.external_source,
  1377. PrintBatch.external_ref == data.external_ref,
  1378. )
  1379. )
  1380. if existing.scalar_one_or_none() is not None:
  1381. raise HTTPException(409, _EXTERNAL_REF_TAKEN)
  1382. batch = PrintBatch(
  1383. name=data.name.strip()[:255],
  1384. archive_id=data.archive_id,
  1385. library_file_id=data.library_file_id,
  1386. quantity=len(data.item_ids) if data.item_ids else 1,
  1387. status="active",
  1388. created_by_id=actor.id if actor else None,
  1389. project_id=data.project_id,
  1390. due_date=data.due_date,
  1391. notes=data.notes,
  1392. external_source=data.external_source,
  1393. external_ref=data.external_ref,
  1394. )
  1395. db.add(batch)
  1396. try:
  1397. await db.flush() # Need batch.id before assigning to items
  1398. except IntegrityError:
  1399. # Lost a race with a concurrent create for the same external record:
  1400. # the unique index caught what the lookup above could not.
  1401. await db.rollback()
  1402. raise HTTPException(409, _EXTERNAL_REF_TAKEN) from None
  1403. if plate_targets is not None:
  1404. for target in plate_targets:
  1405. db.add(
  1406. PrintBatchPlate(
  1407. batch_id=batch.id,
  1408. plate_id=target.plate_id,
  1409. plate_name=target.plate_name,
  1410. quantity_target=target.quantity_target,
  1411. sort_order=target.sort_order,
  1412. )
  1413. )
  1414. # The legacy `quantity` column is display-only; keep it meaningful for
  1415. # anything still reading it by making it the order's total.
  1416. batch.quantity = max(1, sum(t.quantity_target for t in plate_targets))
  1417. assigned = 0
  1418. if data.item_ids:
  1419. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id.in_(data.item_ids)))
  1420. items = result.scalars().all()
  1421. for item in items:
  1422. if item.status != "pending":
  1423. continue
  1424. if item.batch_id is not None:
  1425. continue
  1426. if not printer_scope.allows(item.printer_id):
  1427. continue
  1428. if (
  1429. actor is not None
  1430. and item.created_by_id != actor.id
  1431. and not actor.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  1432. ):
  1433. continue
  1434. item.batch_id = batch.id
  1435. assigned += 1
  1436. batch.quantity = max(assigned, 1)
  1437. await db.commit()
  1438. await db.refresh(batch)
  1439. logger.info("Created batch %s '%s' with %s assigned items", batch.id, batch.name, assigned)
  1440. return await _build_batch_response(db, batch)
  1441. @router.patch("/batches/{batch_id}", response_model=PrintBatchResponse)
  1442. async def update_batch(
  1443. batch_id: int,
  1444. data: PrintBatchUpdate,
  1445. db: AsyncSession = Depends(get_db),
  1446. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_OWN),
  1447. actor: User | ApiKeyActor | None = RequestActor,
  1448. ):
  1449. """Edit an order's header or its per-plate targets while it runs (#342).
  1450. Production requirements change mid-run, so targets are editable. Lowering a
  1451. target below what has already been dispatched is allowed and simply leaves
  1452. ``remaining`` at zero — cancelling the surplus queue items is a separate,
  1453. explicit action, because silently deleting queued work on a number change
  1454. would be a nasty surprise.
  1455. """
  1456. batch = await _load_batch_for_write(db, batch_id, actor, Permission.QUEUE_UPDATE_ALL)
  1457. plate_targets = _validate_plate_targets(data.plates)
  1458. if data.project_id is not None:
  1459. await _validate_batch_project(db, data.project_id, actor)
  1460. if data.name is not None:
  1461. if not data.name.strip():
  1462. raise HTTPException(400, "Batch name is required")
  1463. batch.name = data.name.strip()[:255]
  1464. if data.project_id is not None:
  1465. batch.project_id = data.project_id
  1466. if data.due_date is not None:
  1467. batch.due_date = data.due_date
  1468. if data.notes is not None:
  1469. batch.notes = data.notes
  1470. if data.status is not None:
  1471. batch.status = data.status
  1472. if plate_targets is not None:
  1473. existing = {row.plate_id: row for row in batch.plates}
  1474. for target in plate_targets:
  1475. row = existing.pop(target.plate_id, None)
  1476. if row is None:
  1477. db.add(
  1478. PrintBatchPlate(
  1479. batch_id=batch.id,
  1480. plate_id=target.plate_id,
  1481. plate_name=target.plate_name,
  1482. quantity_target=target.quantity_target,
  1483. sort_order=target.sort_order,
  1484. )
  1485. )
  1486. else:
  1487. row.quantity_target = target.quantity_target
  1488. row.sort_order = target.sort_order
  1489. if target.plate_name is not None:
  1490. row.plate_name = target.plate_name
  1491. # Plates absent from the payload are dropped — the list is the order.
  1492. for orphan in existing.values():
  1493. await db.delete(orphan)
  1494. batch.quantity = max(1, sum(t.quantity_target for t in plate_targets))
  1495. await db.flush()
  1496. await db.refresh(batch)
  1497. # Raising a target on a finished order reopens it; lowering one on a
  1498. # running order can complete it.
  1499. await refresh_batch_status(db, batch)
  1500. await db.commit()
  1501. await db.refresh(batch)
  1502. logger.info("Updated batch %s", batch.id)
  1503. return await _build_batch_response(db, batch)
  1504. @router.post("/batches/{batch_id}/dispatch", response_model=PrintBatchResponse)
  1505. async def dispatch_batch(
  1506. batch_id: int,
  1507. data: PrintBatchDispatchRequest,
  1508. db: AsyncSession = Depends(get_db),
  1509. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  1510. printer_scope: PrinterScope = RequestPrinterScope,
  1511. review_required: bool = QueueReviewRequired,
  1512. actor: User | ApiKeyActor | None = RequestActor,
  1513. ):
  1514. """Queue the runs this order still owes (#342).
  1515. Each new item is cloned from the most recent item for the same plate in
  1516. this batch, so it inherits the printer/model target, AMS mapping, filament
  1517. overrides and print options the user already chose — and the validation
  1518. those went through at creation time.
  1519. """
  1520. batch = await _load_batch_for_write(db, batch_id, actor, Permission.QUEUE_UPDATE_ALL)
  1521. if batch.status == "cancelled":
  1522. raise HTTPException(400, "Cannot dispatch a cancelled batch")
  1523. # Dispatch starts prints, so it must not be a weaker door than POST /queue/.
  1524. await _assert_can_dispatch_batch_sources(db, batch.id, actor, printer_scope)
  1525. try:
  1526. created = await dispatch_remaining(
  1527. db,
  1528. batch,
  1529. plate_id=data.plate_id,
  1530. only_plate=data.only_plate,
  1531. limit=data.limit,
  1532. created_by_id=actor.id if actor else None,
  1533. )
  1534. except BatchDispatchError as exc:
  1535. raise HTTPException(400, str(exc)) from exc
  1536. # The clones inherit their templates' targets, which whoever queued them
  1537. # was allowed; the dispatcher has to be allowed them too (#1727).
  1538. try:
  1539. for item in created:
  1540. printer_scope.ensure(item.printer_id)
  1541. if item.printer_id is None:
  1542. ensure_model_target_allowed(current_user, printer_scope)
  1543. except HTTPException:
  1544. await db.rollback()
  1545. raise
  1546. # A clone copies its template's "wait for manual start", which is off once
  1547. # staff started the template; the dispatcher's own jobs still wait (#1620)
  1548. if review_required:
  1549. for item in created:
  1550. item.manual_start = True
  1551. await db.commit()
  1552. await db.refresh(batch)
  1553. logger.info("Batch %s dispatched %d item(s)", batch.id, len(created))
  1554. return await _build_batch_response(db, batch)
  1555. @router.post("/batches/{batch_id}/ungroup", response_model=PrintBatchUngroupResponse)
  1556. async def ungroup_batch(
  1557. batch_id: int,
  1558. db: AsyncSession = Depends(get_db),
  1559. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_OWN),
  1560. printer_scope: PrinterScope = RequestPrinterScope,
  1561. actor: User | ApiKeyActor | None = RequestActor,
  1562. ):
  1563. """Disband a batch: clear batch_id from all members and delete the batch row.
  1564. Items stay in the queue. Only ungroups items the caller owns (unless they
  1565. hold QUEUE_UPDATE_ALL). A batch with all members ungrouped is deleted.
  1566. """
  1567. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  1568. batch = result.scalar_one_or_none()
  1569. if not batch:
  1570. raise HTTPException(404, "Batch not found")
  1571. can_modify_all = actor is None or actor.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  1572. if not can_modify_all and batch.created_by_id != (actor.id if actor else None):
  1573. raise HTTPException(404, "Batch not found")
  1574. await _ensure_batch_in_scope(db, batch_id, printer_scope)
  1575. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.batch_id == batch_id))
  1576. items = result.scalars().all()
  1577. ungrouped = 0
  1578. remaining = 0
  1579. for item in items:
  1580. if not can_modify_all and item.created_by_id != (actor.id if actor else None):
  1581. remaining += 1
  1582. continue
  1583. item.batch_id = None
  1584. ungrouped += 1
  1585. # Delete the batch row only when all members were ungrouped — otherwise it
  1586. # still owns the items the caller couldn't touch.
  1587. if remaining == 0:
  1588. await db.delete(batch)
  1589. await db.commit()
  1590. logger.info("Ungrouped batch %s (%s items)", batch_id, ungrouped)
  1591. return PrintBatchUngroupResponse(
  1592. ungrouped_count=ungrouped,
  1593. message=f"Ungrouped {ungrouped} item(s)",
  1594. )
  1595. @router.get("/batches", response_model=list[PrintBatchResponse])
  1596. async def list_batches(
  1597. status: str | None = Query(None, description="Filter by status (active, completed, cancelled)"),
  1598. external_source: str | None = Query(None, description="Filter by the integration that created the batch"),
  1599. external_ref: str | None = Query(None, description="Filter by the external record the batch fulfils"),
  1600. db: AsyncSession = Depends(get_db),
  1601. auth_result: tuple[User | None, bool] = Depends(
  1602. require_ownership_permission(
  1603. Permission.QUEUE_READ_ALL,
  1604. Permission.QUEUE_READ_OWN,
  1605. )
  1606. ),
  1607. ):
  1608. """List print batches with progress stats.
  1609. Batches with neither queue items nor per-plate targets are omitted. Those
  1610. are empty shells — a grouping whose items were deleted with their source
  1611. archive, or a create that never got as far as adding any — and they carry
  1612. nothing to show, track or dispatch. A brand-new order is still listed
  1613. before its first dispatch, because its targets say what it owes.
  1614. """
  1615. current_user, can_read_all = auth_result
  1616. query = (
  1617. select(PrintBatch)
  1618. .where(
  1619. select(PrintQueueItem.id).where(PrintQueueItem.batch_id == PrintBatch.id).exists()
  1620. | select(PrintBatchPlate.id).where(PrintBatchPlate.batch_id == PrintBatch.id).exists()
  1621. )
  1622. .order_by(PrintBatch.created_at.desc())
  1623. )
  1624. if status:
  1625. query = query.where(PrintBatch.status == status)
  1626. if external_source is not None:
  1627. query = query.where(PrintBatch.external_source == external_source)
  1628. if external_ref is not None:
  1629. query = query.where(PrintBatch.external_ref == external_ref)
  1630. if current_user is not None and not can_read_all:
  1631. query = query.where(PrintBatch.created_by_id == current_user.id)
  1632. result = await db.execute(query)
  1633. batches = result.scalars().all()
  1634. # Resolve creator names in one query rather than one per batch.
  1635. creator_ids = {b.created_by_id for b in batches if b.created_by_id is not None}
  1636. usernames: dict[int, str] = {}
  1637. if creator_ids:
  1638. rows = await db.execute(select(User.id, User.username).where(User.id.in_(creator_ids)))
  1639. usernames = {row[0]: row[1] for row in rows.all()}
  1640. return [await _build_batch_response(db, batch, usernames=usernames) for batch in batches]
  1641. @router.get("/batches/{batch_id}", response_model=PrintBatchResponse)
  1642. async def get_batch(
  1643. batch_id: int,
  1644. db: AsyncSession = Depends(get_db),
  1645. auth_result: tuple[User | None, bool] = Depends(
  1646. require_ownership_permission(
  1647. Permission.QUEUE_READ_ALL,
  1648. Permission.QUEUE_READ_OWN,
  1649. )
  1650. ),
  1651. ):
  1652. """Get a print batch with progress stats."""
  1653. current_user, can_read_all = auth_result
  1654. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  1655. batch = result.scalar_one_or_none()
  1656. if not batch:
  1657. raise HTTPException(404, "Batch not found")
  1658. if (
  1659. current_user is not None
  1660. and not can_read_all
  1661. and (batch.created_by_id is None or batch.created_by_id != current_user.id)
  1662. ):
  1663. raise HTTPException(404, "Batch not found")
  1664. return await _build_batch_response(db, batch)
  1665. @router.delete("/batches/{batch_id}")
  1666. async def cancel_batch(
  1667. batch_id: int,
  1668. db: AsyncSession = Depends(get_db),
  1669. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_DELETE_ALL),
  1670. printer_scope: PrinterScope = RequestPrinterScope,
  1671. ):
  1672. """Cancel all pending items in a batch and mark batch as cancelled."""
  1673. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  1674. batch = result.scalar_one_or_none()
  1675. if not batch:
  1676. raise HTTPException(404, "Batch not found")
  1677. await _ensure_batch_in_scope(db, batch_id, printer_scope)
  1678. # Cancel all pending queue items in this batch
  1679. result = await db.execute(
  1680. select(PrintQueueItem).where(and_(PrintQueueItem.batch_id == batch_id, PrintQueueItem.status == "pending"))
  1681. )
  1682. pending_items = result.scalars().all()
  1683. cancelled_count = 0
  1684. cancelled_ids: list[int] = []
  1685. for item in pending_items:
  1686. item.status = "cancelled"
  1687. await release_budget_reservation(
  1688. db,
  1689. source_type="print_queue",
  1690. source_id=item.id,
  1691. status="released",
  1692. )
  1693. cancelled_ids.append(item.id)
  1694. cancelled_count += 1
  1695. batch.status = "cancelled"
  1696. await db.commit()
  1697. # Same as the single-item path: a dispatch already preheating for one of
  1698. # these cannot see the status change on its own (#2727).
  1699. from backend.app.services.print_scheduler import scheduler as _scheduler
  1700. for _cancelled_id in cancelled_ids:
  1701. _scheduler.notify_dispatch_cancelled(_cancelled_id)
  1702. return {"message": f"Batch cancelled, {cancelled_count} pending items cancelled"}
  1703. async def _build_batch_response(
  1704. db: AsyncSession, batch: PrintBatch, *, usernames: dict[int, str] | None = None
  1705. ) -> PrintBatchResponse:
  1706. """Build a batch response with per-plate progress derived from queue items.
  1707. ``usernames`` lets the list endpoint resolve every creator in one query
  1708. instead of one per batch.
  1709. """
  1710. progress = await load_progress(db, batch)
  1711. created_by_username = None
  1712. if batch.created_by_id:
  1713. if usernames is not None:
  1714. created_by_username = usernames.get(batch.created_by_id)
  1715. else:
  1716. result = await db.execute(select(User).where(User.id == batch.created_by_id))
  1717. user = result.scalar_one_or_none()
  1718. if user:
  1719. created_by_username = user.username
  1720. return PrintBatchResponse(
  1721. id=batch.id,
  1722. name=batch.name,
  1723. archive_id=batch.archive_id,
  1724. library_file_id=batch.library_file_id,
  1725. quantity=batch.quantity,
  1726. status=batch.status,
  1727. created_at=batch.created_at,
  1728. completed_at=batch.completed_at,
  1729. created_by_id=batch.created_by_id,
  1730. created_by_username=created_by_username,
  1731. project_id=batch.project_id,
  1732. due_date=batch.due_date,
  1733. notes=batch.notes,
  1734. external_source=batch.external_source,
  1735. external_ref=batch.external_ref,
  1736. pending_count=progress.pending,
  1737. printing_count=progress.printing,
  1738. completed_count=progress.completed,
  1739. failed_count=progress.failed,
  1740. cancelled_count=progress.cancelled,
  1741. skipped_count=progress.skipped,
  1742. has_targets=progress.has_targets,
  1743. target_count=progress.target,
  1744. remaining_count=progress.remaining,
  1745. dispatchable_count=progress.dispatchable_remaining,
  1746. actual_cost=progress.actual_cost,
  1747. estimated_remaining_cost=progress.estimated_remaining_cost,
  1748. filament_used_grams=progress.filament_used_grams,
  1749. print_time_seconds=progress.print_time_seconds,
  1750. plates=[
  1751. PrintBatchPlateProgress(
  1752. plate_id=plate.plate_id,
  1753. plate_name=plate.plate_name,
  1754. quantity_target=plate.quantity_target,
  1755. dispatched=plate.dispatched,
  1756. remaining=plate.remaining,
  1757. pending_count=plate.pending,
  1758. printing_count=plate.printing,
  1759. completed_count=plate.completed,
  1760. failed_count=plate.failed,
  1761. cancelled_count=plate.cancelled,
  1762. skipped_count=plate.skipped,
  1763. actual_cost=plate.actual_cost,
  1764. estimated_remaining_cost=plate.estimated_remaining_cost,
  1765. filament_used_grams=plate.filament_used_grams,
  1766. print_time_seconds=plate.print_time_seconds,
  1767. can_dispatch=plate.can_dispatch,
  1768. )
  1769. for plate in progress.plates
  1770. ],
  1771. )
  1772. @router.get("/{item_id}", response_model=PrintQueueItemResponse)
  1773. async def get_queue_item(
  1774. item_id: int,
  1775. db: AsyncSession = Depends(get_db),
  1776. auth_result: tuple[User | None, bool] = Depends(
  1777. require_ownership_permission(
  1778. Permission.QUEUE_READ_ALL,
  1779. Permission.QUEUE_READ_OWN,
  1780. )
  1781. ),
  1782. printer_scope: PrinterScope = RequestPrinterScope,
  1783. ):
  1784. """Get a specific queue item."""
  1785. current_user, can_read_all = auth_result
  1786. result = await db.execute(
  1787. select(PrintQueueItem)
  1788. .options(
  1789. selectinload(PrintQueueItem.archive),
  1790. selectinload(PrintQueueItem.printer),
  1791. selectinload(PrintQueueItem.library_file),
  1792. selectinload(PrintQueueItem.created_by),
  1793. selectinload(PrintQueueItem.batch),
  1794. # Cross-model candidates (#671) and their files, for the card label.
  1795. selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file),
  1796. )
  1797. .where(PrintQueueItem.id == item_id)
  1798. )
  1799. item = result.scalar_one_or_none()
  1800. if not item or not printer_scope.allows(item.printer_id):
  1801. raise HTTPException(404, "Queue item not found")
  1802. if (
  1803. current_user is not None
  1804. and not can_read_all
  1805. and (item.created_by_id is None or item.created_by_id != current_user.id)
  1806. ):
  1807. raise HTTPException(404, "Queue item not found")
  1808. return _enrich_response(item)
  1809. @router.patch("/{item_id}", response_model=PrintQueueItemResponse)
  1810. async def update_queue_item(
  1811. item_id: int,
  1812. data: PrintQueueItemUpdate,
  1813. db: AsyncSession = Depends(get_db),
  1814. auth_result: tuple[User | None, bool] = Depends(
  1815. require_ownership_permission(
  1816. Permission.QUEUE_UPDATE_ALL,
  1817. Permission.QUEUE_UPDATE_OWN,
  1818. )
  1819. ),
  1820. printer_scope: PrinterScope = RequestPrinterScope,
  1821. actor: User | ApiKeyActor | None = RequestActor,
  1822. ):
  1823. """Update a queue item."""
  1824. user, can_modify_all = auth_result
  1825. result = await db.execute(
  1826. select(PrintQueueItem)
  1827. # Needed by the cross-model guard below, and by the response builder —
  1828. # without it _variant_summaries falls back to [] and a PATCH would strip
  1829. # the alternatives out of the payload it echoes back.
  1830. .options(selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file))
  1831. .where(PrintQueueItem.id == item_id)
  1832. )
  1833. item = result.scalar_one_or_none()
  1834. if not item or not printer_scope.allows(item.printer_id):
  1835. raise HTTPException(404, "Queue item not found")
  1836. # Ownership check
  1837. if not can_modify_all:
  1838. if item.created_by_id != user.id:
  1839. raise HTTPException(403, "You can only update your own queue items")
  1840. if item.status != "pending":
  1841. raise HTTPException(400, "Can only update pending items")
  1842. # Dispatch claim (#2615): the row is pending but a scheduler worker has
  1843. # already claimed it and is uploading to its printer. Editing now (e.g.
  1844. # reassigning printer_id) would split the queue row from the in-flight
  1845. # archive/expected-print/physical command. Reject until dispatch finishes;
  1846. # to move it, cancel first (the coordinated escape) and re-queue.
  1847. if item.dispatching_at is not None:
  1848. raise HTTPException(409, "Item is being dispatched — cancel it first to make changes")
  1849. update_data = data.model_dump(exclude_unset=True)
  1850. # Clearing "wait for manual start" starts the job, which needs the same
  1851. # right as the start button (#1620)
  1852. if (
  1853. item.manual_start
  1854. and "manual_start" in update_data
  1855. and not update_data["manual_start"]
  1856. and not may_start_queue_item(user, can_modify_all, item.created_by_id)
  1857. ):
  1858. raise HTTPException(403, _AWAITING_REVIEW)
  1859. # Normalize target_model if being updated (see add_to_queue for why the
  1860. # code map has to run first).
  1861. if "target_model" in update_data and update_data["target_model"]:
  1862. update_data["target_model"] = normalize_model_name(update_data["target_model"])
  1863. ensure_model_target_allowed(user, printer_scope)
  1864. # A cross-model item (#671) owns its own printer decision: each candidate
  1865. # carries its model, and the resolver folds the winner onto the row at
  1866. # dispatch. Assigning a printer here would leave a row with variants *and* a
  1867. # printer_id, and the fixed-printer branch of the scheduler wins that race —
  1868. # so it would dispatch a row whose library_file_id is still null and die in
  1869. # the upload. Narrowing target_model is refused for the same reason: it
  1870. # would silently discard every alternative the user queued.
  1871. #
  1872. # Compared against the current value rather than merely present, because the
  1873. # edit dialog re-sends target_model unchanged on every save.
  1874. if item.variants:
  1875. for field in ("printer_id", "target_model"):
  1876. if field in update_data and update_data[field] != getattr(item, field):
  1877. raise HTTPException(
  1878. 400,
  1879. "This job has printer alternatives — remove them before assigning a printer or model",
  1880. )
  1881. # Cannot specify both printer_id and target_model
  1882. new_printer_id = update_data.get("printer_id", item.printer_id)
  1883. new_target_model = update_data.get("target_model", item.target_model)
  1884. if new_printer_id and new_target_model:
  1885. raise HTTPException(400, "Cannot specify both printer_id and target_model")
  1886. # Validate new printer_id if being changed (and not None)
  1887. if "printer_id" in update_data and update_data["printer_id"] is not None:
  1888. printer_scope.ensure(update_data["printer_id"])
  1889. result = await db.execute(select(Printer).where(Printer.id == update_data["printer_id"]))
  1890. if not result.scalar_one_or_none():
  1891. raise HTTPException(400, "Printer not found")
  1892. # Validate target_model has active printers
  1893. if "target_model" in update_data and update_data["target_model"]:
  1894. result = await db.execute(
  1895. select(Printer).where(Printer.model == update_data["target_model"]).where(Printer.is_active == True) # noqa: E712
  1896. )
  1897. if not result.scalars().first():
  1898. raise HTTPException(400, f"No active printers for model: {update_data['target_model']}")
  1899. # Cross-model safety gate (#2578) — same check as the create route, so
  1900. # a mismatched target can't be introduced by editing either.
  1901. sliced_for = None
  1902. if item.archive_id:
  1903. result = await db.execute(select(PrintArchive.sliced_for_model).where(PrintArchive.id == item.archive_id))
  1904. sliced_for = result.scalar_one_or_none()
  1905. elif item.library_file_id:
  1906. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  1907. lib = result.scalar_one_or_none()
  1908. if lib and lib.file_metadata:
  1909. sliced_for = lib.file_metadata.get("sliced_for_model")
  1910. if not is_gcode_compatible(sliced_for, update_data["target_model"]):
  1911. raise HTTPException(
  1912. 400,
  1913. f"File was sliced for {sliced_for} and cannot be dispatched to {update_data['target_model']} printers",
  1914. )
  1915. # A job that now waits for any printer of a model keeps nothing that was
  1916. # resolved against one printer: the scheduler picks the printer and maps
  1917. # its trays then (#3239). The edit dialog omits both fields on such a move.
  1918. if new_target_model and not new_printer_id:
  1919. update_data["ams_mapping"] = None
  1920. if item.printer_id and "skip_filament_check" not in update_data:
  1921. update_data["skip_filament_check"] = False
  1922. # Serialize ams_mapping to JSON for TEXT column storage
  1923. if "ams_mapping" in update_data:
  1924. update_data["ams_mapping"] = json.dumps(update_data["ams_mapping"]) if update_data["ams_mapping"] else None
  1925. # Serialize filament_overrides to JSON for TEXT column storage, keeping only
  1926. # the slots this item's plate actually prints (#2551 — same shared-override
  1927. # list the create path narrows).
  1928. if "filament_overrides" in update_data:
  1929. overrides = update_data["filament_overrides"]
  1930. if overrides:
  1931. overrides = overrides_for_plate(
  1932. overrides,
  1933. await _resolve_source_path(db, item),
  1934. update_data.get("plate_id", item.plate_id),
  1935. )
  1936. update_data["filament_overrides"] = json.dumps(overrides) if overrides else None
  1937. # Serialize H2C rack-swap nozzle pick (#1780) to JSON for TEXT column
  1938. # storage; same Text-as-opaque-blob convention as ams_mapping above.
  1939. if "nozzle_mapping" in update_data:
  1940. update_data["nozzle_mapping"] = (
  1941. json.dumps(update_data["nozzle_mapping"]) if update_data["nozzle_mapping"] else None
  1942. )
  1943. # Same Text-as-JSON convention for the rack-position pick (#1784). An empty
  1944. # object clears it, which is how the UI says "assign these for me again".
  1945. if "nozzle_rack_choice" in update_data:
  1946. update_data["nozzle_rack_choice"] = (
  1947. json.dumps(update_data["nozzle_rack_choice"]) if update_data["nozzle_rack_choice"] else None
  1948. )
  1949. trusted_estimated_cost = await _trusted_item_estimated_cost(
  1950. db,
  1951. item,
  1952. printer_id=update_data.get("printer_id", item.printer_id),
  1953. plate_id=update_data.get("plate_id", item.plate_id),
  1954. ams_mapping=update_data.get("ams_mapping", item.ams_mapping),
  1955. )
  1956. update_data["estimated_cost"] = trusted_estimated_cost
  1957. await validate_print_budget(
  1958. db,
  1959. cost_center_id=update_data.get("cost_center_id", item.cost_center_id),
  1960. estimated_cost=trusted_estimated_cost,
  1961. current_user=actor,
  1962. exclude_queue_item_id=item.id,
  1963. )
  1964. # Re-check the dispatch claim right before mutating (#2615). Several awaited
  1965. # validations ran since the guard above, and a scheduler worker may have
  1966. # claimed the row in that gap. A fresh read (item isn't dirty yet, so no
  1967. # autoflush races the check) narrows the window to effectively nothing.
  1968. claimed = (
  1969. await db.execute(select(PrintQueueItem.dispatching_at).where(PrintQueueItem.id == item_id))
  1970. ).scalar_one_or_none()
  1971. if claimed is not None:
  1972. raise HTTPException(409, "Item is being dispatched — cancel it first to make changes")
  1973. for field, value in update_data.items():
  1974. setattr(item, field, value)
  1975. await db.commit()
  1976. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  1977. logger.info("Updated queue item %s", item_id)
  1978. return _enrich_response(item)
  1979. @router.delete("/{item_id}")
  1980. async def delete_queue_item(
  1981. item_id: int,
  1982. db: AsyncSession = Depends(get_db),
  1983. auth_result: tuple[User | None, bool] = Depends(
  1984. require_ownership_permission(
  1985. Permission.QUEUE_DELETE_ALL,
  1986. Permission.QUEUE_DELETE_OWN,
  1987. )
  1988. ),
  1989. printer_scope: PrinterScope = RequestPrinterScope,
  1990. ):
  1991. """Remove an item from the queue.
  1992. An order's last surviving run for a plate is cancelled instead of deleted
  1993. (#2960). The row is what a later dispatch clones, so removing it would
  1994. leave the order reporting work outstanding that nothing could ever
  1995. produce. A completed run is exempt: it is the record of something that was
  1996. actually made, and rewriting it as cancelled would falsify the order's
  1997. progress.
  1998. """
  1999. user, can_modify_all = auth_result
  2000. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  2001. item = result.scalar_one_or_none()
  2002. if not item or not printer_scope.allows(item.printer_id):
  2003. raise HTTPException(404, "Queue item not found")
  2004. # Ownership check
  2005. if not can_modify_all:
  2006. if item.created_by_id != user.id:
  2007. raise HTTPException(403, "You can only delete your own queue items")
  2008. if item.status == "printing":
  2009. raise HTTPException(400, "Cannot delete item that is currently printing")
  2010. keep_as_cancelled = item.status != "completed" and await _is_orders_last_source(db, item)
  2011. await release_budget_reservation(
  2012. db,
  2013. source_type="print_queue",
  2014. source_id=item.id,
  2015. status="released",
  2016. )
  2017. if keep_as_cancelled:
  2018. item.status = "cancelled"
  2019. await db.flush()
  2020. # The order may have been sitting on "completed" if this run's target
  2021. # was met by it; cancelling reopens it.
  2022. await refresh_batch_status_for_item(db, item.id)
  2023. else:
  2024. await db.delete(item)
  2025. await db.commit()
  2026. # Stop an in-flight preheat for this item: the dispatch coroutine is
  2027. # parked in a sleep and cannot see the status we just wrote (#2727).
  2028. from backend.app.services.print_scheduler import scheduler as _scheduler
  2029. _scheduler.notify_dispatch_cancelled(item_id)
  2030. if keep_as_cancelled:
  2031. logger.info("Kept queue item %s as cancelled — last source for its batch order plate", item_id)
  2032. return {
  2033. "message": "Item cancelled rather than deleted: it is the only run the order can re-queue this plate from",
  2034. "deleted": False,
  2035. }
  2036. logger.info("Deleted queue item %s", item_id)
  2037. return {"message": "Queue item deleted", "deleted": True}
  2038. @router.post("/reorder")
  2039. async def reorder_queue(
  2040. data: PrintQueueReorder,
  2041. db: AsyncSession = Depends(get_db),
  2042. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_ALL),
  2043. printer_scope: PrinterScope = RequestPrinterScope,
  2044. ):
  2045. """Bulk update positions for queue items."""
  2046. for reorder_item in data.items:
  2047. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == reorder_item.id))
  2048. item = result.scalar_one_or_none()
  2049. # Jobs on printers the caller can't see keep their place (#1727)
  2050. if item and item.status == "pending" and printer_scope.allows(item.printer_id):
  2051. item.position = reorder_item.position
  2052. await db.commit()
  2053. logger.info("Reordered %s queue items", len(data.items))
  2054. return {"message": f"Reordered {len(data.items)} items"}
  2055. @router.post("/printer/{printer_id}/resume")
  2056. async def resume_queue_after_failure(
  2057. printer_id: int,
  2058. db: AsyncSession = Depends(get_db),
  2059. _: User | None = RequirePrinterPermissionIfAuthEnabled(Permission.QUEUE_UPDATE_ALL),
  2060. ):
  2061. """Clear the previous-success gate for a printer and restore skipped items.
  2062. Single atomic op (#1818):
  2063. * Sets ``gate_acknowledged=True`` on every ``failed`` / ``aborted`` queue
  2064. item for this printer that's still in the scheduler's lookback window,
  2065. so the next ``_check_previous_success`` call ignores them.
  2066. * Restores ``skipped`` items whose ``error_message`` matches the
  2067. scheduler's exact "Previous print failed or was aborted" gate string
  2068. back to ``pending`` (clears ``error_message`` + ``completed_at``).
  2069. Returns counts so the UI can render a precise toast. No-op endpoint
  2070. (zero counts) when called against a printer with no gate to clear.
  2071. """
  2072. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2073. printer = result.scalar_one_or_none()
  2074. if not printer:
  2075. raise HTTPException(404, "Printer not found")
  2076. ack_result = await db.execute(
  2077. select(PrintQueueItem)
  2078. .where(PrintQueueItem.printer_id == printer_id)
  2079. .where(PrintQueueItem.status.in_(["failed", "aborted"]))
  2080. .where(PrintQueueItem.gate_acknowledged == False) # noqa: E712
  2081. )
  2082. to_ack = ack_result.scalars().all()
  2083. for failed_item in to_ack:
  2084. failed_item.gate_acknowledged = True
  2085. restore_result = await db.execute(
  2086. select(PrintQueueItem)
  2087. .where(PrintQueueItem.printer_id == printer_id)
  2088. .where(PrintQueueItem.status == "skipped")
  2089. .where(PrintQueueItem.error_message == "Previous print failed or was aborted")
  2090. )
  2091. to_restore = restore_result.scalars().all()
  2092. for skipped_item in to_restore:
  2093. skipped_item.status = "pending"
  2094. skipped_item.error_message = None
  2095. skipped_item.completed_at = None
  2096. await db.commit()
  2097. logger.info(
  2098. "Resume after failure on printer %s: acknowledged %d failure(s), restored %d skipped item(s)",
  2099. printer_id,
  2100. len(to_ack),
  2101. len(to_restore),
  2102. )
  2103. return {"acknowledged": len(to_ack), "restored": len(to_restore)}
  2104. @router.post("/{item_id}/cancel")
  2105. async def cancel_queue_item(
  2106. item_id: int,
  2107. db: AsyncSession = Depends(get_db),
  2108. auth_result: tuple[User | None, bool] = Depends(
  2109. require_ownership_permission(
  2110. Permission.QUEUE_UPDATE_ALL,
  2111. Permission.QUEUE_UPDATE_OWN,
  2112. )
  2113. ),
  2114. printer_scope: PrinterScope = RequestPrinterScope,
  2115. ):
  2116. """Cancel a pending queue item."""
  2117. user, can_modify_all = auth_result
  2118. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  2119. item = result.scalar_one_or_none()
  2120. if not item or not printer_scope.allows(item.printer_id):
  2121. raise HTTPException(404, "Queue item not found")
  2122. # Ownership check
  2123. if not can_modify_all:
  2124. if item.created_by_id != user.id:
  2125. raise HTTPException(403, "You can only cancel your own queue items")
  2126. if item.status not in ("pending",):
  2127. raise HTTPException(400, f"Cannot cancel item with status '{item.status}'")
  2128. item.status = "cancelled"
  2129. item.completed_at = datetime.now(timezone.utc)
  2130. await release_budget_reservation(
  2131. db,
  2132. source_type="print_queue",
  2133. source_id=item.id,
  2134. status="released",
  2135. )
  2136. await db.commit()
  2137. # Stop an in-flight preheat for this item: the dispatch coroutine is
  2138. # parked in a sleep and cannot see the status we just wrote (#2727).
  2139. from backend.app.services.print_scheduler import scheduler as _scheduler
  2140. _scheduler.notify_dispatch_cancelled(item_id)
  2141. logger.info("Cancelled queue item %s", item_id)
  2142. return {"message": "Queue item cancelled"}
  2143. @router.post("/{item_id}/stop")
  2144. async def stop_queue_item(
  2145. item_id: int,
  2146. db: AsyncSession = Depends(get_db),
  2147. auth_result: tuple[User | None, bool] = Depends(
  2148. require_ownership_permission(
  2149. Permission.QUEUE_UPDATE_ALL,
  2150. Permission.QUEUE_UPDATE_OWN,
  2151. )
  2152. ),
  2153. printer_scope: PrinterScope = RequestPrinterScope,
  2154. ):
  2155. """Stop an actively printing queue item.
  2156. Ownership-scoped (#1625-followup): callers with QUEUE_UPDATE_OWN can stop
  2157. their own items; callers with QUEUE_UPDATE_ALL can stop any item. Mirrors
  2158. the /cancel shape. Pre-fix this required QUEUE_UPDATE_ALL — Operators
  2159. holding only _OWN saw the Stop button in the queue UI but got 403 on click.
  2160. """
  2161. user, can_modify_all = auth_result
  2162. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  2163. item = result.scalar_one_or_none()
  2164. if not item or not printer_scope.allows(item.printer_id):
  2165. raise HTTPException(404, "Queue item not found")
  2166. # Ownership check — mirrors /cancel. Ownerless items (created_by_id IS NULL)
  2167. # require _ALL: stop is destructive and an _OWN holder can't claim "they
  2168. # own it" the way /start does (#1670).
  2169. if not can_modify_all and user is not None:
  2170. if item.created_by_id is None or item.created_by_id != user.id:
  2171. raise HTTPException(403, "You can only stop your own queue items")
  2172. if item.status != "printing":
  2173. raise HTTPException(400, f"Can only stop items that are printing, current status: '{item.status}'")
  2174. # Capture values we need for background task
  2175. printer_id = item.printer_id
  2176. auto_off_after = item.auto_off_after
  2177. # Send the stop and mark the printer as user-stopped, even when the stop
  2178. # couldn't be sent: the job is closed below either way, so a "failed" the
  2179. # printer reports later is the user's cancel. Marked before the first await
  2180. # so an on_print_complete arriving during the db.commit() yield already
  2181. # sees the flag (no spurious "print failed" notification).
  2182. from backend.app.services.print_control import stop_print_by_user
  2183. stop_sent = False
  2184. try:
  2185. # A printing job always has a printer; without one there is nothing to stop.
  2186. if printer_id is not None:
  2187. stop_sent = stop_print_by_user(printer_id, mark_when_unsent=True)
  2188. if not stop_sent:
  2189. logger.warning("stop_print returned False for printer %s - printer may not be connected", printer_id)
  2190. except Exception as e:
  2191. logger.error("Error sending stop command for queue item %s: %s", item_id, e)
  2192. # Update queue item status regardless - if printer is off, print is already stopped
  2193. item.status = "cancelled"
  2194. item.completed_at = datetime.now(timezone.utc)
  2195. item.error_message = "Stopped by user" if stop_sent else "Stopped by user (printer was offline)"
  2196. # Reconcile the linked archive when the printer is offline (#2603). When the
  2197. # stop command reaches the printer it later reports the stop over MQTT and
  2198. # on_print_complete flips the archive to cancelled/failed. When the printer is
  2199. # offline no such event ever arrives, so the archive would stay "printing"
  2200. # forever (queue row cancelled, archive still printing — the reporter's
  2201. # archive 436). Close it out here, mirroring what the MQTT path would have
  2202. # done. Only touch a still-"printing" archive so we never overwrite a real
  2203. # completion that raced in.
  2204. if not stop_sent and item.archive_id:
  2205. archive = await db.get(PrintArchive, item.archive_id)
  2206. if archive and archive.status == "printing":
  2207. archive.status = "cancelled"
  2208. archive.completed_at = datetime.now(timezone.utc)
  2209. archive.failure_reason = "Stopped by user (printer was offline)"
  2210. await db.commit()
  2211. logger.info("Stopped printing queue item %s (stop command sent: %s)", item_id, stop_sent)
  2212. # Schedule power-off if the queue item opted in. Delegates to the smart-plug
  2213. # manager so the off honours each plug's configured strategy (time delay or
  2214. # temperature threshold), is cancelled if the printer starts printing again,
  2215. # and never cuts power on a loaded print (#1890). Previously an inline block
  2216. # hardcoded a 50°C / 600s cooldown wait and powered off on the timeout
  2217. # regardless of print state.
  2218. if auto_off_after:
  2219. from backend.app.services.smart_plug_manager import smart_plug_manager
  2220. try:
  2221. await smart_plug_manager.schedule_off_after_queue_job(printer_id, db)
  2222. except Exception as e:
  2223. logger.warning("Auto-off: Failed to schedule power-off for printer %s: %s", printer_id, e)
  2224. return {"message": "Print stopped" if stop_sent else "Queue item cancelled (printer was offline)"}
  2225. @router.post("/{item_id}/start")
  2226. async def start_queue_item(
  2227. item_id: int,
  2228. skip_filament_check: bool = Query(default=False),
  2229. db: AsyncSession = Depends(get_db),
  2230. auth_result: tuple[User | None, bool] = Depends(
  2231. require_ownership_permission(
  2232. Permission.QUEUE_UPDATE_ALL,
  2233. Permission.QUEUE_UPDATE_OWN,
  2234. )
  2235. ),
  2236. printer_scope: PrinterScope = RequestPrinterScope,
  2237. actor: User | ApiKeyActor | None = RequestActor,
  2238. ):
  2239. """Manually start a staged (manual_start) queue item.
  2240. Ownership-scoped (#1625-followup): callers with QUEUE_UPDATE_OWN can
  2241. start their own items + claim ownership of NULL-owner items (VP-uploaded
  2242. items arrive unattributed per #1670). Callers with QUEUE_UPDATE_ALL can
  2243. start any item. Pre-fix this required QUEUE_UPDATE_OWN with no ownership
  2244. check, so _OWN holders could start anyone's queue items via direct API.
  2245. Clears the manual_start flag so the scheduler picks it up. When
  2246. ``skip_filament_check`` is false (the default) the live filament
  2247. deficit (#1496) is checked first — if the assigned spool can't satisfy
  2248. a slot's required grams, the route returns ``409`` with the deficit
  2249. payload so the caller can show a confirm dialog and retry with
  2250. ``skip_filament_check=true``. The same goes for a filament the printer has
  2251. no tray for at all (#2799): ``409`` with ``code=unmatched_filament`` and
  2252. the missing filaments.
  2253. """
  2254. user, can_modify_all = auth_result
  2255. result = await db.execute(
  2256. select(PrintQueueItem)
  2257. .options(
  2258. selectinload(PrintQueueItem.archive),
  2259. selectinload(PrintQueueItem.printer),
  2260. selectinload(PrintQueueItem.library_file),
  2261. selectinload(PrintQueueItem.batch),
  2262. selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file),
  2263. )
  2264. .where(PrintQueueItem.id == item_id)
  2265. )
  2266. item = result.scalar_one_or_none()
  2267. if not item or not printer_scope.allows(item.printer_id):
  2268. raise HTTPException(404, "Queue item not found")
  2269. # Ownership check — softer than /cancel because /start is the entry point
  2270. # for #1670's VP-import flow: an unowned item is claimable by the first
  2271. # _OWN holder who clicks ▶, and the route below credits them as owner.
  2272. # An item with a DIFFERENT owner → 403.
  2273. if not can_modify_all and user is not None:
  2274. if item.created_by_id is not None and item.created_by_id != user.id:
  2275. raise HTTPException(403, "You can only start your own queue items")
  2276. if not may_start_queue_item(user, can_modify_all, item.created_by_id):
  2277. raise HTTPException(403, _AWAITING_REVIEW)
  2278. if item.status != "pending":
  2279. raise HTTPException(400, f"Can only start pending items, current status: '{item.status}'")
  2280. item.estimated_cost = await _trusted_item_estimated_cost(
  2281. db,
  2282. item,
  2283. printer_id=item.printer_id,
  2284. plate_id=item.plate_id,
  2285. ams_mapping=item.ams_mapping,
  2286. )
  2287. await validate_print_budget(
  2288. db,
  2289. cost_center_id=item.cost_center_id,
  2290. estimated_cost=item.estimated_cost,
  2291. current_user=actor,
  2292. exclude_queue_item_id=item.id,
  2293. )
  2294. # Live deficit check — re-evaluated against current spool state, so a
  2295. # spool swap between scheduler flagging and the user clicking ▶ clears
  2296. # the block automatically.
  2297. if not skip_filament_check:
  2298. deficit = await compute_deficit_for_queue_item(db, item)
  2299. if deficit:
  2300. raise HTTPException(
  2301. status_code=409,
  2302. detail={
  2303. "code": "insufficient_filament",
  2304. "deficit": [d.to_dict() for d in deficit],
  2305. },
  2306. )
  2307. # A filament the printer has no tray for at all (#2799). Without this,
  2308. # Start on an item the scheduler held for exactly that would release it
  2309. # only for the next pass to hold it again, and nothing would ever offer
  2310. # "Print Anyway".
  2311. from backend.app.services.print_scheduler import scheduler as _scheduler
  2312. # A convenience, not a gate: the scheduler holds the item again if the
  2313. # filament is still missing, so a failure here must not break Start.
  2314. try:
  2315. missing = await _scheduler.missing_filament_for_start(db, item)
  2316. except Exception:
  2317. logger.exception("Queue item %s: filament check before start failed", item_id)
  2318. missing = None
  2319. if missing:
  2320. raise HTTPException(
  2321. status_code=409,
  2322. detail={
  2323. "code": "unmatched_filament",
  2324. "missing": missing,
  2325. },
  2326. )
  2327. # Print Anyway / no deficit: clear the flags and let the scheduler dispatch.
  2328. item.manual_start = False
  2329. item.filament_short = False
  2330. # Persist the user's "Print Anyway" decision so the scheduler does not
  2331. # immediately re-flag this item on the next tick (#1698-followup). The
  2332. # pre-fix behaviour bounced between "user said anyway" and
  2333. # "scheduler re-blocked on same deficit" forever.
  2334. if skip_filament_check:
  2335. item.skip_filament_check = True
  2336. # Credit the clicker as the item's owner when no prior owner is set —
  2337. # VP-uploaded queue items arrive over FTP unattributed, so without this
  2338. # the print log's User column stays blank even when auth is on
  2339. # (#1670). An item that already has a creator (UI-added queue items)
  2340. # keeps that attribution; the dispatcher is not promoted over the
  2341. # original uploader.
  2342. if user is not None and item.created_by_id is None:
  2343. item.created_by_id = user.id
  2344. await db.commit()
  2345. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  2346. logger.info(
  2347. "Manually started queue item %s (cleared manual_start; skip_filament_check=%s)",
  2348. item_id,
  2349. skip_filament_check,
  2350. )
  2351. return _enrich_response(item)