projects.py 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  1. import io
  2. import json
  3. import logging
  4. import os
  5. import uuid
  6. import zipfile
  7. from collections.abc import Sequence
  8. from dataclasses import dataclass, fields
  9. from datetime import datetime
  10. from pathlib import Path
  11. from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
  12. from fastapi.responses import FileResponse, StreamingResponse
  13. from sqlalchemy import and_, case, func, or_, select, update
  14. from sqlalchemy.ext.asyncio import AsyncSession
  15. from sqlalchemy.orm import selectinload
  16. from backend.app.api.routes.library import get_library_dir
  17. from backend.app.core.auth import RequestPrinterScope, RequirePermissionIfAuthEnabled, require_media_token_permission
  18. from backend.app.core.config import settings
  19. from backend.app.core.database import get_db
  20. from backend.app.core.permissions import Permission
  21. from backend.app.core.printer_scope import PrinterScope
  22. from backend.app.models.archive import PrintArchive
  23. from backend.app.models.library import LibraryFile, LibraryFolder
  24. from backend.app.models.print_log import PrintLogEntry
  25. from backend.app.models.print_queue import PrintQueueItem, PrintQueueVariant
  26. from backend.app.models.project import PROJECT_PRIORITIES, PROJECT_STATUSES, Project
  27. from backend.app.models.project_bom import ProjectBOMItem
  28. from backend.app.models.user import User
  29. from backend.app.schemas.project import (
  30. ArchivePreview,
  31. BatchAddArchives,
  32. BatchAddQueueItems,
  33. BOMItemCreate,
  34. BOMItemResponse,
  35. BOMItemUpdate,
  36. ProjectChildPreview,
  37. ProjectCreate,
  38. ProjectFileProgress,
  39. ProjectImport,
  40. ProjectListResponse,
  41. ProjectResponse,
  42. ProjectStats,
  43. ProjectUpdate,
  44. TimelineEvent,
  45. )
  46. from backend.app.utils.http import build_content_disposition
  47. from backend.app.utils.safe_path import safe_join_under
  48. logger = logging.getLogger(__name__)
  49. router = APIRouter(prefix="/projects", tags=["projects"])
  50. def _imported_priority(value: object) -> str:
  51. """An imported project's priority: one the editor accepts, else "normal"."""
  52. return value if isinstance(value, str) and value in PROJECT_PRIORITIES else "normal"
  53. def _imported_status(value: object) -> str:
  54. """An imported project's status: one the editor accepts, else "active"."""
  55. return value if isinstance(value, str) and value in PROJECT_STATUSES else "active"
  56. _FAILURE_STATUSES = ("failed", "aborted", "cancelled", "stopped")
  57. # A completed run whose user verdict is 'reject' (#1898) finished on the
  58. # machine but produced scrap — everywhere a project counts good parts, that
  59. # run must not contribute. NULL verdict (never asked / not answered) counts
  60. # as good, matching behaviour before the feature existed.
  61. _NOT_REJECTED = or_(PrintLogEntry.user_verdict.is_(None), PrintLogEntry.user_verdict != "reject")
  62. # Soft-deleted archives (#1343) keep their row — and therefore their
  63. # ``project_id`` — after their files have been removed from disk, so that global
  64. # Quick Stats can still count their filament / time / cost. Nothing in this
  65. # module filtered on that, which left deleted prints listed on the project with
  66. # thumbnails pointing at files that no longer exist, and no way to unassign them
  67. # (the only unassign UI lives on the Archives page, which correctly hides them)
  68. # — #2731.
  69. #
  70. # Every project-scoped query filters them out, counts included: a project that
  71. # lists 11 prints must not claim 12. That is a deliberate divergence from the
  72. # global Quick Stats behaviour, where the whole point of the soft delete is that
  73. # the contribution survives. A project is a piece of work with a definite
  74. # membership, not a lifetime total, so a print the user deleted has left it.
  75. _LIVE_ARCHIVE = PrintArchive.deleted_at.is_(None)
  76. @dataclass
  77. class _ProjectTotals:
  78. """Raw per-project aggregates, before targets turn them into percentages.
  79. Kept addable so a master project's numbers are the plain sum of its own
  80. and every descendant's (#1264) — no second set of SQL that could drift
  81. from the single-project path.
  82. """
  83. total_runs: int = 0
  84. total_items: int = 0
  85. completed_items: int = 0
  86. failed_runs: int = 0
  87. total_time_seconds: float = 0.0
  88. total_filament_grams: float = 0.0
  89. filament_cost: float = 0.0
  90. energy_kwh: float = 0.0
  91. energy_cost: float = 0.0
  92. wear_cost: float = 0.0
  93. queued_prints: int = 0
  94. in_progress_prints: int = 0
  95. bom_total_items: int = 0
  96. bom_completed_items: int = 0
  97. bom_cost: float = 0.0
  98. def __add__(self, other: "_ProjectTotals") -> "_ProjectTotals":
  99. return _ProjectTotals(
  100. **{f.name: getattr(self, f.name) + getattr(other, f.name) for f in fields(_ProjectTotals)}
  101. )
  102. async def _load_totals(db: AsyncSession, project_ids: Sequence[int]) -> dict[int, _ProjectTotals]:
  103. """Aggregate prints, queue and BOM for several projects at once.
  104. Grouped rather than one round trip per project because a master project
  105. has to aggregate its whole subtree, and the sub-project list shows each
  106. branch's own roll-up alongside it (#1264).
  107. Aggregates from ``print_log_entries`` joined to ``print_archives`` so
  108. every actual run contributes — pre-fix this counted ``print_archives``
  109. (one row per file), which under-reported every reprint by collapsing
  110. runs back into the source file (#1593). The Archive Print Log view
  111. already drives off the same source (``archives.py::list_archives_slim``),
  112. so project stats now stay aligned with the per-archive numbers.
  113. Orphan log entries (``archive_id IS NULL`` after archive deletion via
  114. ``ON DELETE SET NULL``) are excluded by the inner join — they can't
  115. be attributed to a project.
  116. Projects with nothing recorded are absent from every grouped result, so
  117. the caller gets a zeroed ``_ProjectTotals`` for them rather than a KeyError.
  118. """
  119. totals: dict[int, _ProjectTotals] = {pid: _ProjectTotals() for pid in project_ids}
  120. if not totals:
  121. return totals
  122. # Per-run aggregates. Each run's duration, filament, cost, and energy come
  123. # from the log row, not the source archive — so multi-plate 3MFs and
  124. # reprints both count correctly. The total/completed/failed splits are all
  125. # per-run too: quantity is summed per run, while failures are counted as
  126. # runs rather than parts.
  127. log_rows = await db.execute(
  128. select(
  129. PrintArchive.project_id.label("project_id"),
  130. func.count(PrintLogEntry.id).label("total_runs"),
  131. func.coalesce(func.sum(PrintLogEntry.duration_seconds), 0).label("total_time"),
  132. func.coalesce(func.sum(PrintLogEntry.filament_used_grams), 0).label("total_filament"),
  133. func.coalesce(func.sum(PrintLogEntry.cost), 0).label("total_filament_cost"),
  134. func.coalesce(func.sum(PrintLogEntry.energy_kwh), 0).label("total_energy"),
  135. func.coalesce(func.sum(PrintLogEntry.energy_cost), 0).label("total_energy_cost"),
  136. func.coalesce(func.sum(PrintLogEntry.wear_cost), 0).label("total_wear_cost"),
  137. func.coalesce(func.sum(PrintArchive.quantity), 0).label("total_items"),
  138. # A completed run the user marked as reject (#1898) produced no
  139. # usable parts — keep it out of the good-parts count.
  140. func.coalesce(
  141. func.sum(
  142. case((and_(PrintLogEntry.status == "completed", _NOT_REJECTED), PrintArchive.quantity), else_=0)
  143. ),
  144. 0,
  145. ).label("completed_items"),
  146. func.coalesce(
  147. func.sum(case((PrintLogEntry.status.in_(_FAILURE_STATUSES), 1), else_=0)),
  148. 0,
  149. ).label("failed_runs"),
  150. )
  151. .join(PrintArchive, PrintArchive.id == PrintLogEntry.archive_id)
  152. .where(PrintArchive.project_id.in_(list(totals)), _LIVE_ARCHIVE)
  153. .group_by(PrintArchive.project_id)
  154. )
  155. for row in log_rows:
  156. entry = totals[row.project_id]
  157. entry.total_runs = int(row.total_runs or 0)
  158. entry.total_time_seconds = float(row.total_time or 0)
  159. entry.total_filament_grams = float(row.total_filament or 0)
  160. entry.filament_cost = float(row.total_filament_cost or 0)
  161. entry.energy_kwh = float(row.total_energy or 0)
  162. entry.energy_cost = float(row.total_energy_cost or 0)
  163. entry.wear_cost = float(row.total_wear_cost or 0)
  164. entry.total_items = int(row.total_items or 0)
  165. entry.completed_items = int(row.completed_items or 0)
  166. entry.failed_runs = int(row.failed_runs or 0)
  167. queue_rows = await db.execute(
  168. select(
  169. PrintQueueItem.project_id.label("project_id"),
  170. func.coalesce(func.sum(case((PrintQueueItem.status == "pending", 1), else_=0)), 0).label("queued"),
  171. func.coalesce(func.sum(case((PrintQueueItem.status == "printing", 1), else_=0)), 0).label("in_progress"),
  172. )
  173. .where(PrintQueueItem.project_id.in_(list(totals)))
  174. .group_by(PrintQueueItem.project_id)
  175. )
  176. for row in queue_rows:
  177. entry = totals[row.project_id]
  178. entry.queued_prints = int(row.queued or 0)
  179. entry.in_progress_prints = int(row.in_progress or 0)
  180. bom_rows = await db.execute(
  181. select(
  182. ProjectBOMItem.project_id.label("project_id"),
  183. func.count(ProjectBOMItem.id).label("total"),
  184. func.sum(case((ProjectBOMItem.quantity_acquired >= ProjectBOMItem.quantity_needed, 1), else_=0)).label(
  185. "completed"
  186. ),
  187. func.coalesce(func.sum(ProjectBOMItem.unit_price * ProjectBOMItem.quantity_needed), 0).label("bom_cost"),
  188. )
  189. .where(ProjectBOMItem.project_id.in_(list(totals)))
  190. .group_by(ProjectBOMItem.project_id)
  191. )
  192. for row in bom_rows:
  193. entry = totals[row.project_id]
  194. entry.bom_total_items = int(row.total or 0)
  195. entry.bom_completed_items = int(row.completed or 0)
  196. entry.bom_cost = float(row.bom_cost or 0)
  197. return totals
  198. def _stats_from_totals(
  199. totals: _ProjectTotals, target_count: int | None = None, target_parts_count: int | None = None
  200. ) -> ProjectStats:
  201. """Turn raw aggregates into the response shape, applying the targets."""
  202. # Calculate progress for plates (target_count vs total_archives)
  203. progress_percent = None
  204. remaining_prints = None
  205. if target_count and target_count > 0:
  206. progress_percent = round((totals.total_runs / target_count) * 100, 1)
  207. remaining_prints = max(0, target_count - totals.total_runs)
  208. # Calculate progress for parts (target_parts_count vs completed_items)
  209. parts_progress_percent = None
  210. remaining_parts = None
  211. if target_parts_count and target_parts_count > 0:
  212. parts_progress_percent = round((totals.completed_items / target_parts_count) * 100, 1)
  213. remaining_parts = max(0, target_parts_count - totals.completed_items)
  214. return ProjectStats(
  215. total_archives=totals.total_runs,
  216. total_items=totals.total_items,
  217. completed_prints=totals.completed_items, # Sum of quantities for completed prints
  218. failed_prints=totals.failed_runs,
  219. queued_prints=totals.queued_prints,
  220. in_progress_prints=totals.in_progress_prints,
  221. total_print_time_hours=round(totals.total_time_seconds / 3600, 2),
  222. total_filament_grams=round(totals.total_filament_grams, 2),
  223. progress_percent=progress_percent,
  224. parts_progress_percent=parts_progress_percent,
  225. estimated_cost=round(totals.filament_cost, 2),
  226. total_energy_kwh=round(totals.energy_kwh, 3),
  227. total_energy_cost=round(totals.energy_cost, 3),
  228. total_wear_cost=round(totals.wear_cost, 3),
  229. remaining_prints=remaining_prints,
  230. remaining_parts=remaining_parts,
  231. bom_total_items=totals.bom_total_items,
  232. bom_completed_items=totals.bom_completed_items,
  233. bom_cost=round(totals.bom_cost, 2),
  234. )
  235. async def compute_project_stats(
  236. db: AsyncSession, project_id: int, target_count: int | None = None, target_parts_count: int | None = None
  237. ) -> ProjectStats:
  238. """Compute statistics for a single project, excluding any sub-projects.
  239. Sub-project roll-ups go through ``compute_subtree_stats`` instead. This
  240. stays own-prints-only on purpose: it is what every existing caller means
  241. by "this project's numbers", and widening it would silently restate the
  242. figures of anyone who had already nested projects over the API.
  243. """
  244. totals = (await _load_totals(db, [project_id]))[project_id]
  245. return _stats_from_totals(totals, target_count, target_parts_count)
  246. def _descendants_of(children: dict[int, list[int]], root_id: int) -> list[int]:
  247. """Every project nested under ``root_id``, at any depth, root excluded.
  248. Walked in Python off one already-fetched parent map rather than a recursive
  249. CTE, so SQLite and PostgreSQL stay on identical code paths.
  250. ``seen`` is not belt-and-braces. ``update_project`` only ever rejected a
  251. project as its own *direct* parent, so any database written before that
  252. guard was widened can hold A -> B -> A, and an unguarded walk over one
  253. would never terminate.
  254. """
  255. found: list[int] = []
  256. seen = {root_id}
  257. stack = [root_id]
  258. while stack:
  259. for child in children.get(stack.pop(), ()):
  260. if child in seen:
  261. continue
  262. seen.add(child)
  263. found.append(child)
  264. stack.append(child)
  265. return found
  266. async def _project_descendants(db: AsyncSession, root_id: int) -> list[int]:
  267. """``_descendants_of`` for callers that only need the ids, not the totals."""
  268. rows = (await db.execute(select(Project.id, Project.parent_id).where(Project.parent_id.is_not(None)))).all()
  269. children: dict[int, list[int]] = {}
  270. for pid, parent_id in rows:
  271. children.setdefault(parent_id, []).append(pid)
  272. return _descendants_of(children, root_id)
  273. @dataclass
  274. class _SubtreeReport:
  275. """What the detail endpoint needs to describe a project and its tree."""
  276. descendant_count: int
  277. # None when the project has no sub-projects: the roll-up would be identical
  278. # to the project's own stats, and the UI uses its absence to stay quiet
  279. # rather than showing a second, equal set of numbers.
  280. rollup: ProjectStats | None
  281. child_previews: list[ProjectChildPreview]
  282. async def compute_subtree_stats(db: AsyncSession, root_id: int) -> _SubtreeReport:
  283. """Roll a project's own numbers up with every sub-project beneath it (#1264).
  284. Four queries regardless of tree size or depth: one for the parent map, then
  285. the three grouped aggregates in ``_load_totals`` covering the whole subtree
  286. at once. Each direct child's preview carries *its* branch's roll-up, so the
  287. listed rows add up to the master's total minus the master's own prints.
  288. """
  289. rows = (
  290. await db.execute(
  291. select(
  292. Project.id,
  293. Project.parent_id,
  294. Project.name,
  295. Project.color,
  296. Project.status,
  297. Project.target_count,
  298. Project.target_parts_count,
  299. )
  300. )
  301. ).all()
  302. by_id = {row.id: row for row in rows}
  303. children: dict[int, list[int]] = {}
  304. for row in rows:
  305. if row.parent_id is not None:
  306. children.setdefault(row.parent_id, []).append(row.id)
  307. descendants = _descendants_of(children, root_id)
  308. if not descendants:
  309. return _SubtreeReport(descendant_count=0, rollup=None, child_previews=[])
  310. totals = await _load_totals(db, [root_id, *descendants])
  311. def branch(node_id: int) -> tuple[_ProjectTotals, list[int]]:
  312. """Totals for ``node_id`` plus everything under it, and that id list."""
  313. ids = [node_id, *_descendants_of(children, node_id)]
  314. summed = _ProjectTotals()
  315. for pid in ids:
  316. summed = summed + totals[pid]
  317. return summed, ids
  318. def summed_target(ids: Sequence[int], attr: str) -> int | None:
  319. """Targets add up across the tree; all-unset stays unset, not zero."""
  320. total = sum(getattr(by_id[pid], attr) or 0 for pid in ids)
  321. return total or None
  322. subtree_ids = [root_id, *descendants]
  323. root_totals, _ = branch(root_id)
  324. rollup = _stats_from_totals(
  325. root_totals,
  326. summed_target(subtree_ids, "target_count"),
  327. summed_target(subtree_ids, "target_parts_count"),
  328. )
  329. previews: list[ProjectChildPreview] = []
  330. for child_id in sorted(children.get(root_id, ()), key=lambda cid: by_id[cid].name):
  331. child = by_id[child_id]
  332. child_totals, child_ids = branch(child_id)
  333. # Progress here is runs-against-plate-target, matching what the child's
  334. # own page reports. It used to be completed *quantities* against the
  335. # same target, so a row's percentage disagreed with the page it linked
  336. # to.
  337. child_stats = _stats_from_totals(child_totals, summed_target(child_ids, "target_count"))
  338. previews.append(
  339. ProjectChildPreview(
  340. id=child.id,
  341. name=child.name,
  342. color=child.color,
  343. status=child.status,
  344. progress_percent=child_stats.progress_percent,
  345. descendant_count=len(child_ids) - 1,
  346. total_archives=child_stats.total_archives,
  347. completed_prints=child_stats.completed_prints,
  348. total_print_time_hours=child_stats.total_print_time_hours,
  349. total_filament_grams=child_stats.total_filament_grams,
  350. total_cost=round(
  351. child_stats.estimated_cost
  352. + child_stats.total_energy_cost
  353. + child_stats.total_wear_cost
  354. + child_stats.bom_cost,
  355. 2,
  356. ),
  357. )
  358. )
  359. return _SubtreeReport(descendant_count=len(descendants), rollup=rollup, child_previews=previews)
  360. @router.get("", response_model=list[ProjectListResponse])
  361. @router.get("/", response_model=list[ProjectListResponse])
  362. async def list_projects(
  363. status: str | None = None,
  364. db: AsyncSession = Depends(get_db),
  365. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  366. ):
  367. """List all projects with basic stats."""
  368. query = select(Project)
  369. if status:
  370. query = query.where(Project.status == status)
  371. query = query.order_by(Project.updated_at.desc())
  372. result = await db.execute(query)
  373. projects = result.scalars().all()
  374. # Direct sub-project counts for every project in one pass (#1264). Counted
  375. # across all projects rather than the filtered page: a sub-project hidden
  376. # by the status filter is still a sub-project, and a parent that claimed
  377. # none would invite deleting it as if nothing hung off it.
  378. child_counts = dict(
  379. (
  380. await db.execute(
  381. select(Project.parent_id, func.count(Project.id))
  382. .where(Project.parent_id.is_not(None))
  383. .group_by(Project.parent_id)
  384. )
  385. ).all()
  386. )
  387. # Compute quick stats for each project. Same per-run aggregation as
  388. # ``compute_project_stats`` — counts and quantities come from
  389. # ``print_log_entries`` joined to ``print_archives`` so reprints and
  390. # multi-plate prints contribute every run, not just the source file
  391. # (#1593). Quick stats and the full stats endpoint must agree.
  392. response = []
  393. for project in projects:
  394. log_quick_result = await db.execute(
  395. select(
  396. func.count(PrintLogEntry.id).label("archive_count"),
  397. func.coalesce(func.sum(PrintArchive.quantity), 0).label("total_items"),
  398. func.coalesce(
  399. func.sum(
  400. case((and_(PrintLogEntry.status == "completed", _NOT_REJECTED), PrintArchive.quantity), else_=0)
  401. ),
  402. 0,
  403. ).label("completed_count"),
  404. func.coalesce(
  405. func.sum(case((PrintLogEntry.status.in_(_FAILURE_STATUSES), 1), else_=0)),
  406. 0,
  407. ).label("failed_count"),
  408. )
  409. .join(PrintArchive, PrintArchive.id == PrintLogEntry.archive_id)
  410. .where(PrintArchive.project_id == project.id, _LIVE_ARCHIVE)
  411. )
  412. log_quick = log_quick_result.first()
  413. archive_count = int(log_quick.archive_count or 0)
  414. total_items = int(log_quick.total_items or 0)
  415. completed_count = int(log_quick.completed_count or 0)
  416. failed_count = int(log_quick.failed_count or 0)
  417. # Get queue count
  418. queue_count_result = await db.execute(
  419. select(func.count(PrintQueueItem.id)).where(
  420. PrintQueueItem.project_id == project.id,
  421. PrintQueueItem.status.in_(["pending", "printing"]),
  422. )
  423. )
  424. queue_count = queue_count_result.scalar() or 0
  425. # Plates progress: archive_count / target_count
  426. progress_percent = None
  427. if project.target_count and project.target_count > 0:
  428. progress_percent = round((archive_count / project.target_count) * 100, 1)
  429. # Get archive previews (up to 6 most recent)
  430. archives_result = await db.execute(
  431. select(PrintArchive)
  432. .where(PrintArchive.project_id == project.id, _LIVE_ARCHIVE)
  433. .order_by(PrintArchive.created_at.desc())
  434. .limit(6)
  435. )
  436. archives = archives_result.scalars().all()
  437. archive_previews = [
  438. ArchivePreview(
  439. id=a.id,
  440. print_name=a.print_name,
  441. thumbnail_path=a.thumbnail_path,
  442. status=a.status,
  443. filament_type=a.filament_type,
  444. filament_color=a.filament_color,
  445. )
  446. for a in archives
  447. ]
  448. response.append(
  449. ProjectListResponse(
  450. id=project.id,
  451. name=project.name,
  452. description=project.description,
  453. color=project.color,
  454. status=project.status,
  455. target_count=project.target_count,
  456. target_parts_count=project.target_parts_count,
  457. target_sets=project.target_sets,
  458. budget=project.budget,
  459. tags=project.tags,
  460. due_date=project.due_date,
  461. priority=project.priority,
  462. created_at=project.created_at,
  463. archive_count=archive_count,
  464. total_items=total_items,
  465. completed_count=completed_count,
  466. failed_count=failed_count,
  467. queue_count=queue_count,
  468. progress_percent=progress_percent,
  469. parent_id=project.parent_id,
  470. child_count=child_counts.get(project.id, 0),
  471. archives=archive_previews,
  472. url=project.url,
  473. cover_image_filename=project.cover_image_filename,
  474. )
  475. )
  476. return response
  477. @router.post("/", response_model=ProjectResponse)
  478. async def create_project(
  479. data: ProjectCreate,
  480. db: AsyncSession = Depends(get_db),
  481. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_CREATE),
  482. ):
  483. """Create a new project."""
  484. if data.priority not in PROJECT_PRIORITIES:
  485. raise HTTPException(status_code=400, detail="Invalid priority")
  486. # Verify parent exists if specified
  487. parent_name = None
  488. if data.parent_id:
  489. parent_result = await db.execute(select(Project).where(Project.id == data.parent_id))
  490. parent = parent_result.scalar_one_or_none()
  491. if not parent:
  492. raise HTTPException(status_code=400, detail="Parent project not found")
  493. parent_name = parent.name
  494. project = Project(
  495. name=data.name,
  496. description=data.description,
  497. color=data.color,
  498. target_count=data.target_count,
  499. target_parts_count=data.target_parts_count,
  500. target_sets=data.target_sets,
  501. notes=data.notes,
  502. tags=data.tags,
  503. due_date=data.due_date,
  504. priority=data.priority,
  505. budget=data.budget,
  506. parent_id=data.parent_id,
  507. url=data.url,
  508. )
  509. db.add(project)
  510. await db.flush()
  511. await db.refresh(project)
  512. stats = await compute_project_stats(db, project.id, project.target_count, project.target_parts_count)
  513. return ProjectResponse(
  514. id=project.id,
  515. name=project.name,
  516. description=project.description,
  517. color=project.color,
  518. status=project.status,
  519. target_count=project.target_count,
  520. target_parts_count=project.target_parts_count,
  521. target_sets=project.target_sets,
  522. notes=project.notes,
  523. attachments=project.attachments,
  524. url=project.url,
  525. cover_image_filename=project.cover_image_filename,
  526. tags=project.tags,
  527. due_date=project.due_date,
  528. priority=project.priority,
  529. budget=project.budget,
  530. is_template=project.is_template,
  531. template_source_id=project.template_source_id,
  532. parent_id=project.parent_id,
  533. parent_name=parent_name,
  534. children=[],
  535. created_at=project.created_at,
  536. updated_at=project.updated_at,
  537. stats=stats,
  538. )
  539. # ============ Phase 8: Template Endpoints (Static routes BEFORE dynamic {project_id}) ============
  540. @router.get("/templates", response_model=list[ProjectListResponse])
  541. async def list_templates(
  542. db: AsyncSession = Depends(get_db),
  543. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  544. ):
  545. """List all project templates."""
  546. result = await db.execute(select(Project).where(Project.is_template.is_(True)).order_by(Project.name))
  547. templates = result.scalars().all()
  548. response = []
  549. for project in templates:
  550. # Get archive count
  551. archive_count_result = await db.execute(
  552. select(func.count(PrintArchive.id)).where(PrintArchive.project_id == project.id, _LIVE_ARCHIVE)
  553. )
  554. archive_count = archive_count_result.scalar() or 0
  555. response.append(
  556. ProjectListResponse(
  557. id=project.id,
  558. name=project.name,
  559. description=project.description,
  560. color=project.color,
  561. status=project.status,
  562. target_count=project.target_count,
  563. target_parts_count=project.target_parts_count,
  564. target_sets=project.target_sets,
  565. budget=project.budget,
  566. tags=project.tags,
  567. due_date=project.due_date,
  568. priority=project.priority,
  569. created_at=project.created_at,
  570. archive_count=archive_count,
  571. queue_count=0,
  572. progress_percent=None,
  573. archives=[],
  574. url=project.url,
  575. cover_image_filename=project.cover_image_filename,
  576. )
  577. )
  578. return response
  579. @router.post("/from-template/{template_id}", response_model=ProjectResponse)
  580. async def create_project_from_template(
  581. template_id: int,
  582. name: str = None,
  583. db: AsyncSession = Depends(get_db),
  584. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_CREATE),
  585. ):
  586. """Create a new project from a template."""
  587. result = await db.execute(select(Project).where(Project.id == template_id))
  588. template = result.scalar_one_or_none()
  589. if not template:
  590. raise HTTPException(status_code=404, detail="Template not found")
  591. if not template.is_template:
  592. raise HTTPException(status_code=400, detail="Project is not a template")
  593. # Create new project
  594. project = Project(
  595. name=name or template.name.replace(" (Template)", ""),
  596. description=template.description,
  597. color=template.color,
  598. target_count=template.target_count,
  599. target_parts_count=template.target_parts_count,
  600. target_sets=template.target_sets,
  601. notes=template.notes,
  602. tags=template.tags,
  603. priority=template.priority,
  604. budget=template.budget,
  605. is_template=False,
  606. template_source_id=template.id,
  607. url=template.url,
  608. )
  609. db.add(project)
  610. await db.flush()
  611. # Copy BOM items
  612. bom_result = await db.execute(select(ProjectBOMItem).where(ProjectBOMItem.project_id == template_id))
  613. bom_items = bom_result.scalars().all()
  614. for item in bom_items:
  615. new_item = ProjectBOMItem(
  616. project_id=project.id,
  617. name=item.name,
  618. quantity_needed=item.quantity_needed,
  619. quantity_acquired=0,
  620. unit_price=item.unit_price,
  621. sourcing_url=item.sourcing_url,
  622. stl_filename=item.stl_filename,
  623. remarks=item.remarks,
  624. sort_order=item.sort_order,
  625. )
  626. db.add(new_item)
  627. await db.flush()
  628. await db.refresh(project)
  629. stats = await compute_project_stats(db, project.id, project.target_count, project.target_parts_count)
  630. return ProjectResponse(
  631. id=project.id,
  632. name=project.name,
  633. description=project.description,
  634. color=project.color,
  635. status=project.status,
  636. target_count=project.target_count,
  637. target_parts_count=project.target_parts_count,
  638. target_sets=project.target_sets,
  639. notes=project.notes,
  640. attachments=project.attachments,
  641. url=project.url,
  642. cover_image_filename=project.cover_image_filename,
  643. tags=project.tags,
  644. due_date=project.due_date,
  645. priority=project.priority,
  646. budget=project.budget,
  647. is_template=project.is_template,
  648. template_source_id=project.template_source_id,
  649. parent_id=project.parent_id,
  650. parent_name=None,
  651. children=[],
  652. created_at=project.created_at,
  653. updated_at=project.updated_at,
  654. stats=stats,
  655. )
  656. # ============ Dynamic {project_id} Routes ============
  657. @router.get("/{project_id}", response_model=ProjectResponse)
  658. async def get_project(
  659. project_id: int,
  660. db: AsyncSession = Depends(get_db),
  661. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  662. ):
  663. """Get a project by ID with detailed stats."""
  664. result = await db.execute(select(Project).where(Project.id == project_id))
  665. project = result.scalar_one_or_none()
  666. if not project:
  667. raise HTTPException(status_code=404, detail="Project not found")
  668. # Get parent name
  669. parent_name = None
  670. if project.parent_id:
  671. parent_result = await db.execute(select(Project.name).where(Project.id == project.parent_id))
  672. parent_name = parent_result.scalar()
  673. subtree = await compute_subtree_stats(db, project.id)
  674. stats = await compute_project_stats(db, project.id, project.target_count, project.target_parts_count)
  675. return ProjectResponse(
  676. id=project.id,
  677. name=project.name,
  678. description=project.description,
  679. color=project.color,
  680. status=project.status,
  681. target_count=project.target_count,
  682. target_parts_count=project.target_parts_count,
  683. target_sets=project.target_sets,
  684. notes=project.notes,
  685. attachments=project.attachments,
  686. url=project.url,
  687. cover_image_filename=project.cover_image_filename,
  688. tags=project.tags,
  689. due_date=project.due_date,
  690. priority=project.priority,
  691. budget=project.budget,
  692. is_template=project.is_template,
  693. template_source_id=project.template_source_id,
  694. parent_id=project.parent_id,
  695. parent_name=parent_name,
  696. children=subtree.child_previews,
  697. descendant_count=subtree.descendant_count,
  698. created_at=project.created_at,
  699. updated_at=project.updated_at,
  700. stats=stats,
  701. rollup_stats=subtree.rollup,
  702. )
  703. @router.patch("/{project_id}", response_model=ProjectResponse)
  704. async def update_project(
  705. project_id: int,
  706. data: ProjectUpdate,
  707. db: AsyncSession = Depends(get_db),
  708. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  709. ):
  710. """Update a project."""
  711. result = await db.execute(select(Project).where(Project.id == project_id))
  712. project = result.scalar_one_or_none()
  713. if not project:
  714. raise HTTPException(status_code=404, detail="Project not found")
  715. # Update fields if provided
  716. if data.name is not None:
  717. project.name = data.name
  718. if data.description is not None:
  719. project.description = data.description
  720. if data.color is not None:
  721. project.color = data.color
  722. if data.status is not None:
  723. if data.status not in PROJECT_STATUSES:
  724. raise HTTPException(status_code=400, detail="Invalid status")
  725. project.status = data.status
  726. if data.target_count is not None:
  727. project.target_count = data.target_count
  728. if data.target_parts_count is not None:
  729. project.target_parts_count = data.target_parts_count
  730. # Sent-but-null clears the copies-per-file target (#1897); omitted leaves it
  731. # alone (same #2536 semantics as tags/due_date below).
  732. if "target_sets" in data.model_fields_set:
  733. project.target_sets = data.target_sets
  734. if data.notes is not None:
  735. project.notes = data.notes
  736. # Sent-but-null clears the field; omitted leaves it alone. Guarding on
  737. # ``is not None`` would make an emptied tags field or a removed due date
  738. # silently revert to the stored value (#2536).
  739. if "tags" in data.model_fields_set:
  740. project.tags = data.tags
  741. if "due_date" in data.model_fields_set:
  742. project.due_date = data.due_date
  743. if data.priority is not None:
  744. if data.priority not in PROJECT_PRIORITIES:
  745. raise HTTPException(status_code=400, detail="Invalid priority")
  746. project.priority = data.priority
  747. if "budget" in data.model_fields_set:
  748. project.budget = data.budget
  749. if "url" in data.model_fields_set:
  750. # Pydantic validator already guarantees http(s) prefix or None.
  751. project.url = data.url
  752. if data.parent_id is not None:
  753. # Verify parent exists and prevent circular reference
  754. if data.parent_id == project_id:
  755. raise HTTPException(status_code=400, detail="Project cannot be its own parent")
  756. if data.parent_id != 0: # 0 means remove parent
  757. parent_result = await db.execute(select(Project).where(Project.id == data.parent_id))
  758. if not parent_result.scalar_one_or_none():
  759. raise HTTPException(status_code=400, detail="Parent project not found")
  760. # Refusing only the project itself left A -> B -> A reachable in two
  761. # calls, and a cycle has no root to roll figures up to — the walk in
  762. # ``_descendants_of`` would revisit forever without its seen-set
  763. # (#1264).
  764. if data.parent_id in await _project_descendants(db, project_id):
  765. raise HTTPException(status_code=400, detail="Project cannot be moved under one of its own sub-projects")
  766. project.parent_id = data.parent_id
  767. else:
  768. project.parent_id = None
  769. await db.flush()
  770. await db.refresh(project)
  771. # Get parent name
  772. parent_name = None
  773. if project.parent_id:
  774. parent_result = await db.execute(select(Project.name).where(Project.id == project.parent_id))
  775. parent_name = parent_result.scalar()
  776. subtree = await compute_subtree_stats(db, project.id)
  777. stats = await compute_project_stats(db, project.id, project.target_count, project.target_parts_count)
  778. return ProjectResponse(
  779. id=project.id,
  780. name=project.name,
  781. description=project.description,
  782. color=project.color,
  783. status=project.status,
  784. target_count=project.target_count,
  785. target_parts_count=project.target_parts_count,
  786. target_sets=project.target_sets,
  787. notes=project.notes,
  788. attachments=project.attachments,
  789. url=project.url,
  790. cover_image_filename=project.cover_image_filename,
  791. tags=project.tags,
  792. due_date=project.due_date,
  793. priority=project.priority,
  794. budget=project.budget,
  795. is_template=project.is_template,
  796. template_source_id=project.template_source_id,
  797. parent_id=project.parent_id,
  798. parent_name=parent_name,
  799. children=subtree.child_previews,
  800. descendant_count=subtree.descendant_count,
  801. created_at=project.created_at,
  802. updated_at=project.updated_at,
  803. stats=stats,
  804. rollup_stats=subtree.rollup,
  805. )
  806. @router.delete("/{project_id}")
  807. async def delete_project(
  808. project_id: int,
  809. db: AsyncSession = Depends(get_db),
  810. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_DELETE),
  811. ):
  812. """Delete a project. Archives and queue items will have project_id set to NULL."""
  813. result = await db.execute(select(Project).where(Project.id == project_id))
  814. project = result.scalar_one_or_none()
  815. if not project:
  816. raise HTTPException(status_code=404, detail="Project not found")
  817. # Sub-projects move up to the deleted project's own parent rather than
  818. # being cut loose at the top level, so deleting a middle layer collapses
  819. # the tree by one instead of scattering a branch (#1264). Left to the ORM
  820. # this would null their parent_id instead, which loses the grandparent.
  821. await db.execute(update(Project).where(Project.parent_id == project_id).values(parent_id=project.parent_id))
  822. await db.delete(project)
  823. return {"message": "Project deleted"}
  824. @router.get("/{project_id}/archives")
  825. async def list_project_archives(
  826. project_id: int,
  827. limit: int = 100,
  828. offset: int = 0,
  829. db: AsyncSession = Depends(get_db),
  830. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  831. printer_scope: PrinterScope = RequestPrinterScope,
  832. ):
  833. """List archives in a project."""
  834. # Verify project exists
  835. result = await db.execute(select(Project).where(Project.id == project_id))
  836. if not result.scalar_one_or_none():
  837. raise HTTPException(status_code=404, detail="Project not found")
  838. # Get archives with both ``project`` and ``created_by`` eagerly loaded.
  839. # ``archive_to_response`` accesses ``archive.created_by.username`` to
  840. # surface the creator on the archive card; without selectinload that's
  841. # a lazy attribute access on a closed async session, which throws
  842. # ``MissingGreenlet`` and produces a 500. ``ArchiveService.list_archives``
  843. # already loads both — this route just got out of step.
  844. query = (
  845. select(PrintArchive)
  846. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  847. .where(PrintArchive.project_id == project_id, _LIVE_ARCHIVE)
  848. .order_by(PrintArchive.created_at.desc())
  849. .limit(limit)
  850. .offset(offset)
  851. )
  852. # Only archives from printers the caller may see (#1727)
  853. if (clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  854. query = query.where(clause)
  855. result = await db.execute(query)
  856. archives = result.scalars().all()
  857. # Import the response converter from archives module
  858. from backend.app.api.routes.archives import _load_run_aggregates, archive_to_response
  859. # Load run aggregates so multi-run archives' time/accuracy badge is
  860. # suppressed consistently with the main archives list endpoint (#1608).
  861. run_aggregates = await _load_run_aggregates(db, [a.id for a in archives])
  862. return [archive_to_response(a, run_aggregate=run_aggregates.get(a.id)) for a in archives]
  863. @router.get("/{project_id}/queue")
  864. async def list_project_queue(
  865. project_id: int,
  866. db: AsyncSession = Depends(get_db),
  867. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  868. printer_scope: PrinterScope = RequestPrinterScope,
  869. ):
  870. """List queue items in a project."""
  871. # Verify project exists
  872. result = await db.execute(select(Project).where(Project.id == project_id))
  873. if not result.scalar_one_or_none():
  874. raise HTTPException(status_code=404, detail="Project not found")
  875. # Get queue items
  876. query = select(PrintQueueItem).where(PrintQueueItem.project_id == project_id).order_by(PrintQueueItem.position)
  877. if (clause := printer_scope.where(PrintQueueItem.printer_id)) is not None:
  878. query = query.where(clause)
  879. result = await db.execute(query)
  880. items = result.scalars().all()
  881. return items
  882. @router.get("/{project_id}/file-progress", response_model=list[ProjectFileProgress])
  883. async def get_project_file_progress(
  884. project_id: int,
  885. db: AsyncSession = Depends(get_db),
  886. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  887. ):
  888. """Completed-run counts per library file inside a project (#1897).
  889. Counts completed ``PrintLogEntry`` rows (same source as the aggregate
  890. project stats) of archives attributed to this project, and maps each run to
  891. one of the project's library files — the files living in folders linked to
  892. the project, the same set the project detail page renders.
  893. A run is attributed to exactly one file, by the strongest available match:
  894. 1. ``archive.library_file_id`` (stamped at queue dispatch since #1897),
  895. 2. content hash (covers historical rows),
  896. 3. filename (covers hash drift, e.g. re-sliced uploads of the same name).
  897. Files with no completed runs are omitted — the frontend treats absence as 0.
  898. """
  899. result = await db.execute(select(Project.id).where(Project.id == project_id))
  900. if result.scalar_one_or_none() is None:
  901. raise HTTPException(status_code=404, detail="Project not found")
  902. files_result = await db.execute(
  903. select(LibraryFile.id, LibraryFile.file_hash, LibraryFile.filename)
  904. .join(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  905. .where(LibraryFolder.project_id == project_id, LibraryFile.deleted_at.is_(None))
  906. )
  907. file_rows = files_result.all()
  908. if not file_rows:
  909. return []
  910. # First match wins within each tier, so iteration order (file id) is stable
  911. # when duplicates share a hash or filename.
  912. by_id = {fid for fid, _, _ in file_rows}
  913. by_hash: dict[str, int] = {}
  914. by_name: dict[str, int] = {}
  915. for fid, fhash, fname in file_rows:
  916. if fhash and fhash not in by_hash:
  917. by_hash[fhash] = fid
  918. if fname not in by_name:
  919. by_name[fname] = fid
  920. runs_result = await db.execute(
  921. select(
  922. PrintArchive.library_file_id,
  923. PrintArchive.content_hash,
  924. PrintArchive.filename,
  925. func.count(PrintLogEntry.id),
  926. )
  927. .join(PrintArchive, PrintArchive.id == PrintLogEntry.archive_id)
  928. .where(
  929. PrintArchive.project_id == project_id,
  930. PrintLogEntry.status == "completed",
  931. _NOT_REJECTED,
  932. _LIVE_ARCHIVE,
  933. )
  934. .group_by(PrintArchive.library_file_id, PrintArchive.content_hash, PrintArchive.filename)
  935. )
  936. counts: dict[int, int] = {}
  937. for lib_file_id, content_hash, filename, run_count in runs_result.all():
  938. if lib_file_id in by_id:
  939. fid = lib_file_id
  940. elif content_hash and content_hash in by_hash:
  941. fid = by_hash[content_hash]
  942. elif filename in by_name:
  943. fid = by_name[filename]
  944. else:
  945. continue
  946. counts[fid] = counts.get(fid, 0) + run_count
  947. return [ProjectFileProgress(file_id=fid, completed_count=n) for fid, n in sorted(counts.items())]
  948. @router.post("/{project_id}/add-archives")
  949. async def add_archives_to_project(
  950. project_id: int,
  951. data: BatchAddArchives,
  952. db: AsyncSession = Depends(get_db),
  953. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  954. ):
  955. """Batch add archives to a project."""
  956. # Verify project exists
  957. result = await db.execute(select(Project).where(Project.id == project_id))
  958. if not result.scalar_one_or_none():
  959. raise HTTPException(status_code=404, detail="Project not found")
  960. # Update archives
  961. updated = 0
  962. for archive_id in data.archive_ids:
  963. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  964. archive = result.scalar_one_or_none()
  965. if archive:
  966. archive.project_id = project_id
  967. updated += 1
  968. return {"message": f"Added {updated} archives to project"}
  969. @router.post("/{project_id}/add-queue")
  970. async def add_queue_items_to_project(
  971. project_id: int,
  972. data: BatchAddQueueItems,
  973. db: AsyncSession = Depends(get_db),
  974. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  975. ):
  976. """Batch add queue items to a project."""
  977. # Verify project exists
  978. result = await db.execute(select(Project).where(Project.id == project_id))
  979. if not result.scalar_one_or_none():
  980. raise HTTPException(status_code=404, detail="Project not found")
  981. # Update queue items
  982. updated = 0
  983. for item_id in data.queue_item_ids:
  984. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  985. item = result.scalar_one_or_none()
  986. if item:
  987. item.project_id = project_id
  988. updated += 1
  989. return {"message": f"Added {updated} queue items to project"}
  990. @router.post("/{project_id}/remove-archives")
  991. async def remove_archives_from_project(
  992. project_id: int,
  993. data: BatchAddArchives,
  994. db: AsyncSession = Depends(get_db),
  995. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  996. ):
  997. """Remove archives from a project (sets project_id to NULL)."""
  998. updated = 0
  999. for archive_id in data.archive_ids:
  1000. result = await db.execute(
  1001. select(PrintArchive).where(
  1002. PrintArchive.id == archive_id,
  1003. PrintArchive.project_id == project_id,
  1004. )
  1005. )
  1006. archive = result.scalar_one_or_none()
  1007. if archive:
  1008. archive.project_id = None
  1009. updated += 1
  1010. return {"message": f"Removed {updated} archives from project"}
  1011. def get_project_attachments_dir(project_id: int) -> Path:
  1012. """Get the attachments directory for a project."""
  1013. base_dir = Path(settings.archive_dir)
  1014. return base_dir / "projects" / str(project_id) / "attachments"
  1015. # Cover-image upload accepts only common web-renderable image types (#1155).
  1016. # Subset of ALLOWED_ATTACHMENT_EXTENSIONS minus .svg/.ico because those don't
  1017. # render well as a card thumbnail.
  1018. COVER_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp"}
  1019. COVER_IMAGE_CONTENT_TYPES = {
  1020. ".jpg": "image/jpeg",
  1021. ".jpeg": "image/jpeg",
  1022. ".png": "image/png",
  1023. ".gif": "image/gif",
  1024. ".webp": "image/webp",
  1025. }
  1026. # Allowed file extensions for attachments
  1027. ALLOWED_ATTACHMENT_EXTENSIONS = {
  1028. # Images
  1029. ".jpg",
  1030. ".jpeg",
  1031. ".png",
  1032. ".gif",
  1033. ".webp",
  1034. ".svg",
  1035. ".bmp",
  1036. ".ico",
  1037. # Documents
  1038. ".pdf",
  1039. ".doc",
  1040. ".docx",
  1041. ".xls",
  1042. ".xlsx",
  1043. ".ppt",
  1044. ".pptx",
  1045. ".odt",
  1046. ".ods",
  1047. ".odp",
  1048. ".txt",
  1049. ".rtf",
  1050. ".csv",
  1051. ".md",
  1052. # 3D/CAD files
  1053. ".stl",
  1054. ".obj",
  1055. ".3mf",
  1056. ".step",
  1057. ".stp",
  1058. ".iges",
  1059. ".igs",
  1060. ".f3d",
  1061. ".scad",
  1062. # Archives
  1063. ".zip",
  1064. ".rar",
  1065. ".7z",
  1066. ".tar",
  1067. ".gz",
  1068. # Code/scripts (for Klipper macros, scripts, etc.)
  1069. ".py",
  1070. ".sh",
  1071. ".cfg",
  1072. ".conf",
  1073. ".gcode",
  1074. ".ini",
  1075. # Other common formats
  1076. ".json",
  1077. ".xml",
  1078. ".yaml",
  1079. ".yml",
  1080. }
  1081. @router.post("/{project_id}/attachments")
  1082. async def upload_attachment(
  1083. project_id: int,
  1084. file: UploadFile = File(...),
  1085. db: AsyncSession = Depends(get_db),
  1086. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  1087. ):
  1088. """Upload an attachment to a project."""
  1089. logger.info("=== UPLOAD START: %s for project %s ===", file.filename, project_id)
  1090. # Verify project exists
  1091. result = await db.execute(select(Project).where(Project.id == project_id))
  1092. project = result.scalar_one_or_none()
  1093. if not project:
  1094. raise HTTPException(status_code=404, detail="Project not found")
  1095. # Validate file extension
  1096. original_name = file.filename or "unknown"
  1097. ext = os.path.splitext(original_name)[1].lower()
  1098. if ext not in ALLOWED_ATTACHMENT_EXTENSIONS:
  1099. raise HTTPException(
  1100. status_code=400,
  1101. detail=f"File type '{ext}' not supported. Allowed: images, PDFs, documents, STL, 3MF, archives.",
  1102. )
  1103. # Create attachments directory
  1104. attachments_dir = get_project_attachments_dir(project_id)
  1105. attachments_dir.mkdir(parents=True, exist_ok=True)
  1106. # Generate unique filename
  1107. unique_filename = f"{uuid.uuid4().hex}{ext}"
  1108. file_path = attachments_dir / unique_filename # SEC-PATH-OK: unique_filename = uuid.uuid4().hex + ext
  1109. # Save file
  1110. try:
  1111. with open(file_path, "wb") as f:
  1112. content = await file.read()
  1113. f.write(content)
  1114. logger.info("=== FILE SAVED: %s, size: %s ===", file_path, len(content))
  1115. except Exception as e:
  1116. logger.error("Failed to save attachment: %s", e)
  1117. raise HTTPException(status_code=500, detail="Failed to save attachment")
  1118. # Update project attachments JSON
  1119. attachments = list(project.attachments or [])
  1120. new_attachment = {
  1121. "filename": unique_filename,
  1122. "original_name": original_name,
  1123. "size": len(content),
  1124. "uploaded_at": datetime.now().isoformat(),
  1125. }
  1126. attachments.append(new_attachment)
  1127. # Simple ORM update
  1128. project.attachments = attachments
  1129. db.add(project) # Explicitly add to session
  1130. logger.info("=== BEFORE COMMIT: %s attachments ===", len(attachments))
  1131. await db.flush()
  1132. await db.commit()
  1133. logger.info("=== AFTER COMMIT ===")
  1134. # Verify by re-querying
  1135. result = await db.execute(select(Project).where(Project.id == project_id))
  1136. fresh_project = result.scalar_one()
  1137. logger.info("=== VERIFIED: %s attachments ===", len(fresh_project.attachments or []))
  1138. return {
  1139. "status": "success",
  1140. "filename": unique_filename,
  1141. "original_name": original_name,
  1142. "attachments": fresh_project.attachments,
  1143. }
  1144. @router.get("/{project_id}/attachments/{filename}")
  1145. async def download_attachment(
  1146. project_id: int,
  1147. filename: str,
  1148. db: AsyncSession = Depends(get_db),
  1149. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  1150. ):
  1151. """Download an attachment from a project."""
  1152. # Validate filename to prevent path traversal
  1153. if "/" in filename or "\\" in filename or ".." in filename or not filename:
  1154. raise HTTPException(status_code=400, detail="Invalid filename")
  1155. # Verify project exists
  1156. result = await db.execute(select(Project).where(Project.id == project_id))
  1157. project = result.scalar_one_or_none()
  1158. if not project:
  1159. raise HTTPException(status_code=404, detail="Project not found")
  1160. # Verify attachment exists in project
  1161. attachments = project.attachments or []
  1162. attachment = next((a for a in attachments if a.get("filename") == filename), None)
  1163. if not attachment:
  1164. raise HTTPException(status_code=404, detail="Attachment not found")
  1165. # Check file exists
  1166. file_path = (
  1167. get_project_attachments_dir(project_id) / filename
  1168. ) # SEC-PATH-OK: filename validated above (no /, \\, .., empty) + attachment membership check
  1169. if not file_path.exists():
  1170. raise HTTPException(status_code=404, detail="Attachment file not found")
  1171. return FileResponse(
  1172. file_path,
  1173. filename=attachment.get("original_name", filename),
  1174. media_type="application/octet-stream",
  1175. )
  1176. @router.delete("/{project_id}/attachments/{filename}")
  1177. async def delete_attachment(
  1178. project_id: int,
  1179. filename: str,
  1180. db: AsyncSession = Depends(get_db),
  1181. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  1182. ):
  1183. """Delete an attachment from a project."""
  1184. # Validate filename to prevent path traversal
  1185. if "/" in filename or "\\" in filename or ".." in filename or not filename:
  1186. raise HTTPException(status_code=400, detail="Invalid filename")
  1187. # Verify project exists
  1188. result = await db.execute(select(Project).where(Project.id == project_id))
  1189. project = result.scalar_one_or_none()
  1190. if not project:
  1191. raise HTTPException(status_code=404, detail="Project not found")
  1192. # Find and remove attachment from list
  1193. attachments = project.attachments or []
  1194. attachment = next((a for a in attachments if a.get("filename") == filename), None)
  1195. if not attachment:
  1196. raise HTTPException(status_code=404, detail="Attachment not found")
  1197. # Remove from list
  1198. attachments = [a for a in attachments if a.get("filename") != filename]
  1199. project.attachments = attachments if attachments else None
  1200. # Delete file
  1201. file_path = (
  1202. get_project_attachments_dir(project_id) / filename
  1203. ) # SEC-PATH-OK: filename validated above (no /, \\, .., empty) + attachment membership check
  1204. if file_path.exists():
  1205. try:
  1206. os.remove(file_path)
  1207. except Exception as e:
  1208. logger.warning("Failed to delete attachment file: %s", e)
  1209. await db.flush()
  1210. await db.refresh(project)
  1211. return {
  1212. "status": "success",
  1213. "message": "Attachment deleted",
  1214. "attachments": project.attachments,
  1215. }
  1216. # ============ #1155: Cover image ============
  1217. @router.post("/{project_id}/cover-image")
  1218. async def upload_project_cover_image(
  1219. project_id: int,
  1220. file: UploadFile = File(...),
  1221. db: AsyncSession = Depends(get_db),
  1222. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  1223. ):
  1224. """Upload (or replace) the project's cover image (#1155).
  1225. Stored alongside other attachments but tracked via Project.cover_image_filename
  1226. so swap/delete operations don't touch the attachments list. Replaces any
  1227. existing cover image — the prior file is deleted on disk before the new one
  1228. lands so a stuck filesystem reference can't accumulate orphaned images.
  1229. """
  1230. result = await db.execute(select(Project).where(Project.id == project_id))
  1231. project = result.scalar_one_or_none()
  1232. if not project:
  1233. raise HTTPException(status_code=404, detail="Project not found")
  1234. original_name = file.filename or "cover"
  1235. ext = os.path.splitext(original_name)[1].lower()
  1236. if ext not in COVER_IMAGE_EXTENSIONS:
  1237. raise HTTPException(
  1238. status_code=400,
  1239. detail=f"Cover image must be one of {sorted(COVER_IMAGE_EXTENSIONS)}",
  1240. )
  1241. attachments_dir = get_project_attachments_dir(project_id)
  1242. attachments_dir.mkdir(parents=True, exist_ok=True)
  1243. # Remove the previous cover-image file from disk first so we don't accumulate
  1244. # orphans when users repeatedly replace it. Best-effort: a missing/locked file
  1245. # shouldn't block a successful replacement.
  1246. if project.cover_image_filename:
  1247. old_path = attachments_dir / project.cover_image_filename
  1248. if old_path.exists():
  1249. try:
  1250. os.remove(old_path)
  1251. except OSError as e:
  1252. logger.warning("Failed to delete old cover image %s: %s", old_path, e)
  1253. unique_filename = f"cover_{uuid.uuid4().hex}{ext}"
  1254. file_path = attachments_dir / unique_filename # SEC-PATH-OK: unique_filename = f"cover_{uuid.uuid4().hex}{ext}"
  1255. try:
  1256. with open(file_path, "wb") as f:
  1257. content = await file.read()
  1258. f.write(content)
  1259. except OSError as e:
  1260. logger.error("Failed to save cover image: %s", e)
  1261. raise HTTPException(status_code=500, detail="Failed to save cover image")
  1262. project.cover_image_filename = unique_filename
  1263. db.add(project)
  1264. await db.flush()
  1265. await db.commit()
  1266. return {
  1267. "status": "success",
  1268. "filename": unique_filename,
  1269. "size": len(content),
  1270. }
  1271. @router.get("/{project_id}/cover-image")
  1272. async def get_project_cover_image(
  1273. project_id: int,
  1274. db: AsyncSession = Depends(get_db),
  1275. _: User | None = Depends(require_media_token_permission(Permission.PROJECTS_READ)),
  1276. ):
  1277. """Stream the project's cover image (#1155).
  1278. Browsers can't attach `Authorization: Bearer ...` to `<img src>` requests,
  1279. so this route accepts a `?token=` media credential, the same one
  1280. /archives/{id}/thumbnail takes. The frontend wraps URLs with `withMediaToken`.
  1281. Gated on ``projects:read`` like every other project route. It used to take
  1282. the camera-stream token, which required ``camera:view`` instead -- an
  1283. unrelated permission that a user could hold without any project access, and
  1284. that a project reader could easily lack (#3025). Projects carry no
  1285. ``created_by_id``, so there is no per-row owner to check beyond that."""
  1286. result = await db.execute(select(Project).where(Project.id == project_id))
  1287. project = result.scalar_one_or_none()
  1288. if not project:
  1289. raise HTTPException(status_code=404, detail="Project not found")
  1290. if not project.cover_image_filename:
  1291. raise HTTPException(status_code=404, detail="No cover image set")
  1292. file_path = get_project_attachments_dir(project_id) / project.cover_image_filename
  1293. if not file_path.exists():
  1294. # DB references a file that vanished from disk — clear the dangling
  1295. # reference so future GETs get a clean 404 instead of repeatedly
  1296. # touching the filesystem.
  1297. logger.warning("Cover image file missing for project %s: %s", project_id, file_path)
  1298. project.cover_image_filename = None
  1299. await db.commit()
  1300. raise HTTPException(status_code=404, detail="Cover image file not found")
  1301. ext = os.path.splitext(project.cover_image_filename)[1].lower()
  1302. media_type = COVER_IMAGE_CONTENT_TYPES.get(ext, "application/octet-stream")
  1303. return FileResponse(file_path, media_type=media_type)
  1304. @router.delete("/{project_id}/cover-image")
  1305. async def delete_project_cover_image(
  1306. project_id: int,
  1307. db: AsyncSession = Depends(get_db),
  1308. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  1309. ):
  1310. """Remove the project's cover image (#1155)."""
  1311. result = await db.execute(select(Project).where(Project.id == project_id))
  1312. project = result.scalar_one_or_none()
  1313. if not project:
  1314. raise HTTPException(status_code=404, detail="Project not found")
  1315. if project.cover_image_filename:
  1316. file_path = get_project_attachments_dir(project_id) / project.cover_image_filename
  1317. if file_path.exists():
  1318. try:
  1319. os.remove(file_path)
  1320. except OSError as e:
  1321. logger.warning("Failed to delete cover image file %s: %s", file_path, e)
  1322. project.cover_image_filename = None
  1323. db.add(project)
  1324. await db.flush()
  1325. await db.commit()
  1326. return {"status": "success"}
  1327. # ============ Phase 7: BOM Endpoints ============
  1328. @router.get("/{project_id}/bom", response_model=list[BOMItemResponse])
  1329. async def list_bom_items(
  1330. project_id: int,
  1331. db: AsyncSession = Depends(get_db),
  1332. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  1333. ):
  1334. """List all BOM items for a project."""
  1335. # Verify project exists
  1336. result = await db.execute(select(Project).where(Project.id == project_id))
  1337. if not result.scalar_one_or_none():
  1338. raise HTTPException(status_code=404, detail="Project not found")
  1339. # Get BOM items
  1340. result = await db.execute(
  1341. select(ProjectBOMItem)
  1342. .where(ProjectBOMItem.project_id == project_id)
  1343. .order_by(ProjectBOMItem.sort_order, ProjectBOMItem.id)
  1344. )
  1345. items = result.scalars().all()
  1346. response = []
  1347. for item in items:
  1348. # Get archive name if linked
  1349. archive_name = None
  1350. if item.archive_id:
  1351. archive_result = await db.execute(select(PrintArchive.print_name).where(PrintArchive.id == item.archive_id))
  1352. archive_name = archive_result.scalar()
  1353. response.append(
  1354. BOMItemResponse(
  1355. id=item.id,
  1356. project_id=item.project_id,
  1357. name=item.name,
  1358. quantity_needed=item.quantity_needed,
  1359. quantity_acquired=item.quantity_acquired,
  1360. unit_price=item.unit_price,
  1361. sourcing_url=item.sourcing_url,
  1362. archive_id=item.archive_id,
  1363. archive_name=archive_name,
  1364. stl_filename=item.stl_filename,
  1365. remarks=item.remarks,
  1366. sort_order=item.sort_order,
  1367. is_complete=item.quantity_acquired >= item.quantity_needed,
  1368. created_at=item.created_at,
  1369. updated_at=item.updated_at,
  1370. )
  1371. )
  1372. return response
  1373. @router.post("/{project_id}/bom", response_model=BOMItemResponse)
  1374. async def create_bom_item(
  1375. project_id: int,
  1376. data: BOMItemCreate,
  1377. db: AsyncSession = Depends(get_db),
  1378. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  1379. ):
  1380. """Add a BOM item to a project."""
  1381. # Verify project exists
  1382. result = await db.execute(select(Project).where(Project.id == project_id))
  1383. if not result.scalar_one_or_none():
  1384. raise HTTPException(status_code=404, detail="Project not found")
  1385. # Get max sort order
  1386. max_order_result = await db.execute(
  1387. select(func.max(ProjectBOMItem.sort_order)).where(ProjectBOMItem.project_id == project_id)
  1388. )
  1389. max_order = max_order_result.scalar() or 0
  1390. item = ProjectBOMItem(
  1391. project_id=project_id,
  1392. name=data.name,
  1393. quantity_needed=data.quantity_needed,
  1394. unit_price=data.unit_price,
  1395. sourcing_url=data.sourcing_url,
  1396. archive_id=data.archive_id,
  1397. stl_filename=data.stl_filename,
  1398. remarks=data.remarks,
  1399. sort_order=max_order + 1,
  1400. )
  1401. db.add(item)
  1402. await db.flush()
  1403. await db.refresh(item)
  1404. # Get archive name if linked
  1405. archive_name = None
  1406. if item.archive_id:
  1407. archive_result = await db.execute(select(PrintArchive.print_name).where(PrintArchive.id == item.archive_id))
  1408. archive_name = archive_result.scalar()
  1409. return BOMItemResponse(
  1410. id=item.id,
  1411. project_id=item.project_id,
  1412. name=item.name,
  1413. quantity_needed=item.quantity_needed,
  1414. quantity_acquired=item.quantity_acquired,
  1415. unit_price=item.unit_price,
  1416. sourcing_url=item.sourcing_url,
  1417. archive_id=item.archive_id,
  1418. archive_name=archive_name,
  1419. stl_filename=item.stl_filename,
  1420. remarks=item.remarks,
  1421. sort_order=item.sort_order,
  1422. is_complete=item.quantity_acquired >= item.quantity_needed,
  1423. created_at=item.created_at,
  1424. updated_at=item.updated_at,
  1425. )
  1426. @router.patch("/{project_id}/bom/{item_id}", response_model=BOMItemResponse)
  1427. async def update_bom_item(
  1428. project_id: int,
  1429. item_id: int,
  1430. data: BOMItemUpdate,
  1431. db: AsyncSession = Depends(get_db),
  1432. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  1433. ):
  1434. """Update a BOM item."""
  1435. result = await db.execute(
  1436. select(ProjectBOMItem).where(
  1437. ProjectBOMItem.id == item_id,
  1438. ProjectBOMItem.project_id == project_id,
  1439. )
  1440. )
  1441. item = result.scalar_one_or_none()
  1442. if not item:
  1443. raise HTTPException(status_code=404, detail="BOM item not found")
  1444. if data.name is not None:
  1445. item.name = data.name
  1446. if data.quantity_needed is not None:
  1447. item.quantity_needed = data.quantity_needed
  1448. if data.quantity_acquired is not None:
  1449. item.quantity_acquired = data.quantity_acquired
  1450. if data.unit_price is not None:
  1451. item.unit_price = data.unit_price if data.unit_price != 0 else None
  1452. if data.sourcing_url is not None:
  1453. item.sourcing_url = data.sourcing_url if data.sourcing_url else None
  1454. if data.archive_id is not None:
  1455. item.archive_id = data.archive_id if data.archive_id != 0 else None
  1456. if data.stl_filename is not None:
  1457. item.stl_filename = data.stl_filename if data.stl_filename else None
  1458. if data.remarks is not None:
  1459. item.remarks = data.remarks if data.remarks else None
  1460. await db.flush()
  1461. await db.refresh(item)
  1462. # Get archive name if linked
  1463. archive_name = None
  1464. if item.archive_id:
  1465. archive_result = await db.execute(select(PrintArchive.print_name).where(PrintArchive.id == item.archive_id))
  1466. archive_name = archive_result.scalar()
  1467. return BOMItemResponse(
  1468. id=item.id,
  1469. project_id=item.project_id,
  1470. name=item.name,
  1471. quantity_needed=item.quantity_needed,
  1472. quantity_acquired=item.quantity_acquired,
  1473. unit_price=item.unit_price,
  1474. sourcing_url=item.sourcing_url,
  1475. archive_id=item.archive_id,
  1476. archive_name=archive_name,
  1477. stl_filename=item.stl_filename,
  1478. remarks=item.remarks,
  1479. sort_order=item.sort_order,
  1480. is_complete=item.quantity_acquired >= item.quantity_needed,
  1481. created_at=item.created_at,
  1482. updated_at=item.updated_at,
  1483. )
  1484. @router.delete("/{project_id}/bom/{item_id}")
  1485. async def delete_bom_item(
  1486. project_id: int,
  1487. item_id: int,
  1488. db: AsyncSession = Depends(get_db),
  1489. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_UPDATE),
  1490. ):
  1491. """Delete a BOM item."""
  1492. result = await db.execute(
  1493. select(ProjectBOMItem).where(
  1494. ProjectBOMItem.id == item_id,
  1495. ProjectBOMItem.project_id == project_id,
  1496. )
  1497. )
  1498. item = result.scalar_one_or_none()
  1499. if not item:
  1500. raise HTTPException(status_code=404, detail="BOM item not found")
  1501. await db.delete(item)
  1502. return {"status": "success", "message": "BOM item deleted"}
  1503. @router.post("/{project_id}/create-template", response_model=ProjectResponse)
  1504. async def create_template_from_project(
  1505. project_id: int,
  1506. db: AsyncSession = Depends(get_db),
  1507. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_CREATE),
  1508. ):
  1509. """Create a template from an existing project."""
  1510. result = await db.execute(select(Project).where(Project.id == project_id))
  1511. source = result.scalar_one_or_none()
  1512. if not source:
  1513. raise HTTPException(status_code=404, detail="Project not found")
  1514. # Create template
  1515. template = Project(
  1516. name=f"{source.name} (Template)",
  1517. description=source.description,
  1518. color=source.color,
  1519. target_count=source.target_count,
  1520. target_parts_count=source.target_parts_count,
  1521. target_sets=source.target_sets,
  1522. notes=source.notes,
  1523. tags=source.tags,
  1524. priority=source.priority,
  1525. budget=source.budget,
  1526. is_template=True,
  1527. template_source_id=source.id,
  1528. url=source.url,
  1529. )
  1530. db.add(template)
  1531. await db.flush()
  1532. # Copy BOM items
  1533. bom_result = await db.execute(select(ProjectBOMItem).where(ProjectBOMItem.project_id == project_id))
  1534. bom_items = bom_result.scalars().all()
  1535. for item in bom_items:
  1536. new_item = ProjectBOMItem(
  1537. project_id=template.id,
  1538. name=item.name,
  1539. quantity_needed=item.quantity_needed,
  1540. quantity_acquired=0,
  1541. unit_price=item.unit_price,
  1542. sourcing_url=item.sourcing_url,
  1543. stl_filename=item.stl_filename,
  1544. remarks=item.remarks,
  1545. sort_order=item.sort_order,
  1546. )
  1547. db.add(new_item)
  1548. await db.flush()
  1549. await db.refresh(template)
  1550. stats = await compute_project_stats(db, template.id, template.target_count, template.target_parts_count)
  1551. return ProjectResponse(
  1552. id=template.id,
  1553. name=template.name,
  1554. description=template.description,
  1555. color=template.color,
  1556. status=template.status,
  1557. target_count=template.target_count,
  1558. target_parts_count=template.target_parts_count,
  1559. target_sets=template.target_sets,
  1560. notes=template.notes,
  1561. attachments=template.attachments,
  1562. url=template.url,
  1563. cover_image_filename=template.cover_image_filename,
  1564. tags=template.tags,
  1565. due_date=template.due_date,
  1566. priority=template.priority,
  1567. budget=template.budget,
  1568. is_template=template.is_template,
  1569. template_source_id=template.template_source_id,
  1570. parent_id=template.parent_id,
  1571. parent_name=None,
  1572. children=[],
  1573. created_at=template.created_at,
  1574. updated_at=template.updated_at,
  1575. stats=stats,
  1576. )
  1577. # ============ Phase 9: Timeline Endpoint ============
  1578. def _queue_item_name(item: PrintQueueItem) -> str | None:
  1579. """The name a queued job goes by, from its archive or library file.
  1580. Same sources as the queue page: the archive's print name or file name, but
  1581. not from a deleted archive, then the library file's print name or file name,
  1582. then for a job for any of several models (#671) its first candidate's file.
  1583. """
  1584. if item.archive is not None and item.archive.deleted_at is None:
  1585. name = item.archive.print_name or item.archive.filename
  1586. if name:
  1587. return name
  1588. if item.library_file is not None:
  1589. metadata = item.library_file.file_metadata or {}
  1590. return metadata.get("print_name") or item.library_file.filename
  1591. if item.variants and item.variants[0].library_file is not None:
  1592. return item.variants[0].library_file.filename
  1593. return None
  1594. @router.get("/{project_id}/timeline", response_model=list[TimelineEvent])
  1595. async def get_project_timeline(
  1596. project_id: int,
  1597. limit: int = 50,
  1598. db: AsyncSession = Depends(get_db),
  1599. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  1600. printer_scope: PrinterScope = RequestPrinterScope,
  1601. ):
  1602. """Get timeline of events for a project."""
  1603. # Verify project exists
  1604. result = await db.execute(select(Project).where(Project.id == project_id))
  1605. project = result.scalar_one_or_none()
  1606. if not project:
  1607. raise HTTPException(status_code=404, detail="Project not found")
  1608. events = []
  1609. # Project creation event
  1610. events.append(
  1611. TimelineEvent(
  1612. event_type="project_created",
  1613. timestamp=project.created_at,
  1614. title="Project created",
  1615. description=f"Project '{project.name}' was created",
  1616. )
  1617. )
  1618. # Get archives and add events
  1619. archive_query = (
  1620. select(PrintArchive)
  1621. .where(PrintArchive.project_id == project_id, _LIVE_ARCHIVE)
  1622. .order_by(PrintArchive.created_at.desc())
  1623. .limit(limit)
  1624. )
  1625. # Only events from printers the caller may see, as the archive and queue
  1626. # lists of the project (#1727)
  1627. if (clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  1628. archive_query = archive_query.where(clause)
  1629. archives = (await db.execute(archive_query)).scalars().all()
  1630. for archive in archives:
  1631. if archive.status == "completed":
  1632. events.append(
  1633. TimelineEvent(
  1634. event_type="print_completed",
  1635. timestamp=archive.completed_at or archive.created_at,
  1636. title="Print completed",
  1637. description=archive.print_name,
  1638. metadata={
  1639. "archive_id": archive.id,
  1640. "print_time_hours": round((archive.print_time_seconds or 0) / 3600, 2),
  1641. "filament_grams": round(archive.filament_used_grams or 0, 1),
  1642. },
  1643. )
  1644. )
  1645. elif archive.status == "failed":
  1646. events.append(
  1647. TimelineEvent(
  1648. event_type="print_failed",
  1649. timestamp=archive.completed_at or archive.created_at,
  1650. title="Print failed",
  1651. description=archive.print_name,
  1652. metadata={"archive_id": archive.id},
  1653. )
  1654. )
  1655. # Get queue items
  1656. queue_query = (
  1657. select(PrintQueueItem)
  1658. .where(PrintQueueItem.project_id == project_id)
  1659. .options(
  1660. selectinload(PrintQueueItem.archive),
  1661. selectinload(PrintQueueItem.library_file),
  1662. selectinload(PrintQueueItem.variants).selectinload(PrintQueueVariant.library_file),
  1663. )
  1664. .order_by(PrintQueueItem.created_at.desc())
  1665. .limit(limit)
  1666. )
  1667. if (clause := printer_scope.where(PrintQueueItem.printer_id)) is not None:
  1668. queue_query = queue_query.where(clause)
  1669. queue_items = (await db.execute(queue_query)).scalars().all()
  1670. for item in queue_items:
  1671. if item.status == "printing":
  1672. events.append(
  1673. TimelineEvent(
  1674. event_type="print_started",
  1675. timestamp=item.started_at or item.created_at,
  1676. title="Print started",
  1677. description=_queue_item_name(item),
  1678. metadata={"queue_item_id": item.id},
  1679. )
  1680. )
  1681. elif item.status == "pending":
  1682. events.append(
  1683. TimelineEvent(
  1684. event_type="queued",
  1685. timestamp=item.created_at,
  1686. title="Added to queue",
  1687. description=_queue_item_name(item),
  1688. metadata={"queue_item_id": item.id},
  1689. )
  1690. )
  1691. # Sort by timestamp descending
  1692. events.sort(key=lambda e: e.timestamp, reverse=True)
  1693. return events[:limit]
  1694. # ============ Phase 10: Import/Export Endpoints ============
  1695. @router.get("/{project_id}/export")
  1696. async def export_project(
  1697. project_id: int,
  1698. format: str = "zip", # "zip" (with files) or "json" (metadata only)
  1699. db: AsyncSession = Depends(get_db),
  1700. _: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_READ),
  1701. ):
  1702. """Export a project. Use format=zip (default) for full export with files, or format=json for metadata only."""
  1703. result = await db.execute(select(Project).where(Project.id == project_id))
  1704. project = result.scalar_one_or_none()
  1705. if not project:
  1706. raise HTTPException(status_code=404, detail="Project not found")
  1707. # Get BOM items
  1708. bom_result = await db.execute(
  1709. select(ProjectBOMItem).where(ProjectBOMItem.project_id == project_id).order_by(ProjectBOMItem.sort_order)
  1710. )
  1711. bom_items = bom_result.scalars().all()
  1712. bom_export = [
  1713. {
  1714. "name": item.name,
  1715. "quantity_needed": item.quantity_needed,
  1716. "quantity_acquired": item.quantity_acquired,
  1717. "unit_price": item.unit_price,
  1718. "sourcing_url": item.sourcing_url,
  1719. "stl_filename": item.stl_filename,
  1720. "remarks": item.remarks,
  1721. }
  1722. for item in bom_items
  1723. ]
  1724. # Get linked folders and their files
  1725. folders_result = await db.execute(
  1726. select(LibraryFolder).where(LibraryFolder.project_id == project_id).order_by(LibraryFolder.name)
  1727. )
  1728. linked_folders = folders_result.scalars().all()
  1729. folders_export = []
  1730. files_to_include = [] # (archive_path, zip_path)
  1731. for folder in linked_folders:
  1732. # Get files in this folder
  1733. files_result = await db.execute(
  1734. LibraryFile.active().where(LibraryFile.folder_id == folder.id).order_by(LibraryFile.filename)
  1735. )
  1736. files = files_result.scalars().all()
  1737. folder_files = []
  1738. for f in files:
  1739. folder_files.append(
  1740. {
  1741. "filename": f.filename,
  1742. "file_type": f.file_type,
  1743. "notes": f.notes,
  1744. }
  1745. )
  1746. # Add file to include in ZIP
  1747. library_dir = get_library_dir()
  1748. file_path = library_dir / f.file_path
  1749. if file_path.exists():
  1750. zip_path = f"files/{folder.name}/{f.filename}"
  1751. files_to_include.append((file_path, zip_path))
  1752. # Also include thumbnail if exists
  1753. if f.thumbnail_path:
  1754. thumb_path = library_dir / f.thumbnail_path
  1755. if thumb_path.exists():
  1756. thumb_zip_path = f"files/{folder.name}/.thumbnails/{f.filename}.png"
  1757. files_to_include.append((thumb_path, thumb_zip_path))
  1758. folders_export.append(
  1759. {
  1760. "name": folder.name,
  1761. "files": folder_files,
  1762. }
  1763. )
  1764. # Build project JSON
  1765. project_data = {
  1766. "name": project.name,
  1767. "description": project.description,
  1768. "color": project.color,
  1769. "status": project.status,
  1770. "target_count": project.target_count,
  1771. "target_parts_count": project.target_parts_count,
  1772. "target_sets": project.target_sets,
  1773. "notes": project.notes,
  1774. "tags": project.tags,
  1775. "due_date": project.due_date.isoformat() if project.due_date else None,
  1776. "priority": project.priority,
  1777. "budget": project.budget,
  1778. "bom_items": bom_export,
  1779. "linked_folders": folders_export,
  1780. }
  1781. # Return JSON if requested (for bulk export)
  1782. if format == "json":
  1783. return project_data
  1784. # Create ZIP in memory
  1785. zip_buffer = io.BytesIO()
  1786. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  1787. # Add project.json
  1788. zf.writestr("project.json", json.dumps(project_data, indent=2))
  1789. # Add files
  1790. for file_path, zip_path in files_to_include:
  1791. zf.write(file_path, zip_path)
  1792. zip_buffer.seek(0)
  1793. # Generate filename
  1794. safe_name = "".join(c if c.isalnum() or c in "-_ " else "_" for c in project.name)
  1795. filename = f"{safe_name}_{datetime.now().strftime('%Y-%m-%d')}.zip"
  1796. return StreamingResponse(
  1797. zip_buffer,
  1798. media_type="application/zip",
  1799. headers={"Content-Disposition": build_content_disposition(filename)},
  1800. )
  1801. @router.post("/import", response_model=ProjectResponse)
  1802. async def import_project(
  1803. data: ProjectImport,
  1804. db: AsyncSession = Depends(get_db),
  1805. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_CREATE),
  1806. ):
  1807. """Import a project with optional BOM items and linked folders."""
  1808. # Create the project
  1809. project = Project(
  1810. name=data.name,
  1811. description=data.description,
  1812. color=data.color,
  1813. status=_imported_status(data.status),
  1814. target_count=data.target_count,
  1815. target_parts_count=data.target_parts_count,
  1816. target_sets=data.target_sets,
  1817. notes=data.notes,
  1818. tags=data.tags,
  1819. due_date=data.due_date,
  1820. priority=_imported_priority(data.priority),
  1821. budget=data.budget,
  1822. )
  1823. db.add(project)
  1824. await db.flush()
  1825. # Create BOM items
  1826. for idx, bom_data in enumerate(data.bom_items):
  1827. bom_item = ProjectBOMItem(
  1828. project_id=project.id,
  1829. name=bom_data.name,
  1830. quantity_needed=bom_data.quantity_needed,
  1831. quantity_acquired=bom_data.quantity_acquired,
  1832. unit_price=bom_data.unit_price,
  1833. sourcing_url=bom_data.sourcing_url,
  1834. stl_filename=bom_data.stl_filename,
  1835. remarks=bom_data.remarks,
  1836. sort_order=idx,
  1837. )
  1838. db.add(bom_item)
  1839. # Create linked folders in library
  1840. for folder_data in data.linked_folders:
  1841. # Check if folder with this name already exists at root level
  1842. existing_result = await db.execute(
  1843. select(LibraryFolder).where(
  1844. LibraryFolder.name == folder_data.name,
  1845. LibraryFolder.parent_id.is_(None),
  1846. )
  1847. )
  1848. existing_folder = existing_result.scalar_one_or_none()
  1849. if existing_folder:
  1850. # Link existing folder to project
  1851. existing_folder.project_id = project.id
  1852. else:
  1853. # Create new folder linked to project
  1854. # A project's folder is shared, like every linked folder (#3201).
  1855. new_folder = LibraryFolder(
  1856. name=folder_data.name,
  1857. project_id=project.id,
  1858. is_external=False,
  1859. external_readonly=False,
  1860. external_show_hidden=False,
  1861. created_by_id=current_user.id if current_user else None,
  1862. shared=True,
  1863. )
  1864. db.add(new_folder)
  1865. await db.flush()
  1866. await db.refresh(project)
  1867. stats = await compute_project_stats(db, project.id, project.target_count, project.target_parts_count)
  1868. return ProjectResponse(
  1869. id=project.id,
  1870. name=project.name,
  1871. description=project.description,
  1872. color=project.color,
  1873. status=project.status,
  1874. target_count=project.target_count,
  1875. target_parts_count=project.target_parts_count,
  1876. target_sets=project.target_sets,
  1877. notes=project.notes,
  1878. attachments=project.attachments,
  1879. url=project.url,
  1880. cover_image_filename=project.cover_image_filename,
  1881. tags=project.tags,
  1882. due_date=project.due_date,
  1883. priority=project.priority,
  1884. budget=project.budget,
  1885. is_template=project.is_template,
  1886. template_source_id=project.template_source_id,
  1887. parent_id=project.parent_id,
  1888. parent_name=None,
  1889. children=[],
  1890. created_at=project.created_at,
  1891. updated_at=project.updated_at,
  1892. stats=stats,
  1893. )
  1894. @router.post("/import/file", response_model=ProjectResponse)
  1895. async def import_project_file(
  1896. file: UploadFile = File(...),
  1897. db: AsyncSession = Depends(get_db),
  1898. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.PROJECTS_CREATE),
  1899. ):
  1900. """Import a project from a ZIP or JSON file."""
  1901. if not file.filename:
  1902. raise HTTPException(status_code=400, detail="No filename provided")
  1903. # Determine file type
  1904. filename_lower = file.filename.lower()
  1905. content = await file.read()
  1906. if filename_lower.endswith(".zip"):
  1907. # Extract project.json from ZIP
  1908. try:
  1909. with zipfile.ZipFile(io.BytesIO(content)) as zf:
  1910. if "project.json" not in zf.namelist():
  1911. raise HTTPException(status_code=400, detail="ZIP must contain project.json")
  1912. project_json = zf.read("project.json")
  1913. data = json.loads(project_json)
  1914. # Get list of files in the ZIP
  1915. zip_files = {name: zf.read(name) for name in zf.namelist() if name.startswith("files/")}
  1916. except zipfile.BadZipFile:
  1917. raise HTTPException(status_code=400, detail="Invalid ZIP file")
  1918. elif filename_lower.endswith(".json"):
  1919. try:
  1920. data = json.loads(content)
  1921. zip_files = {}
  1922. except json.JSONDecodeError:
  1923. raise HTTPException(status_code=400, detail="Invalid JSON file")
  1924. else:
  1925. raise HTTPException(status_code=400, detail="File must be .zip or .json")
  1926. # Create the project
  1927. project = Project(
  1928. name=data.get("name", "Imported Project"),
  1929. description=data.get("description"),
  1930. color=data.get("color"),
  1931. status=_imported_status(data.get("status")),
  1932. target_count=data.get("target_count"),
  1933. target_parts_count=data.get("target_parts_count"),
  1934. target_sets=data.get("target_sets"),
  1935. notes=data.get("notes"),
  1936. tags=data.get("tags"),
  1937. due_date=datetime.fromisoformat(data["due_date"]) if data.get("due_date") else None,
  1938. priority=_imported_priority(data.get("priority")),
  1939. budget=data.get("budget"),
  1940. )
  1941. db.add(project)
  1942. await db.flush()
  1943. # Create BOM items
  1944. for idx, bom_data in enumerate(data.get("bom_items", [])):
  1945. bom_item = ProjectBOMItem(
  1946. project_id=project.id,
  1947. name=bom_data.get("name", "Unnamed"),
  1948. quantity_needed=bom_data.get("quantity_needed", 1),
  1949. quantity_acquired=bom_data.get("quantity_acquired", 0),
  1950. unit_price=bom_data.get("unit_price"),
  1951. sourcing_url=bom_data.get("sourcing_url"),
  1952. stl_filename=bom_data.get("stl_filename"),
  1953. remarks=bom_data.get("remarks"),
  1954. sort_order=idx,
  1955. )
  1956. db.add(bom_item)
  1957. # Create linked folders and files
  1958. library_dir = get_library_dir()
  1959. for folder_data in data.get("linked_folders", []):
  1960. folder_name = folder_data.get("name")
  1961. if not folder_name:
  1962. continue
  1963. # Containment check on the folder name — refuses absolute paths and
  1964. # ``..`` traversal in ``project.json[linked_folders[*].name]``. The
  1965. # previous code did ``library_dir / folder_name`` directly, which
  1966. # collapses to ``Path(folder_name)`` when folder_name is absolute
  1967. # and lets ``..`` escape after mkdir.
  1968. folder_path = safe_join_under(library_dir, folder_name)
  1969. # Check if folder exists
  1970. existing_result = await db.execute(
  1971. select(LibraryFolder).where(
  1972. LibraryFolder.name == folder_name,
  1973. LibraryFolder.parent_id.is_(None),
  1974. )
  1975. )
  1976. existing_folder = existing_result.scalar_one_or_none()
  1977. if existing_folder:
  1978. # Link existing folder to project
  1979. existing_folder.project_id = project.id
  1980. folder = existing_folder
  1981. else:
  1982. # Create new folder
  1983. # A project's folder is shared, like every linked folder (#3201).
  1984. folder = LibraryFolder(
  1985. name=folder_name,
  1986. project_id=project.id,
  1987. is_external=False,
  1988. external_readonly=False,
  1989. external_show_hidden=False,
  1990. created_by_id=current_user.id if current_user else None,
  1991. shared=True,
  1992. )
  1993. db.add(folder)
  1994. await db.flush()
  1995. # Create folder on disk
  1996. folder_path.mkdir(parents=True, exist_ok=True)
  1997. # Import files for this folder from ZIP
  1998. folder_prefix = f"files/{folder_name}/"
  1999. for zip_path, file_content in zip_files.items():
  2000. if not zip_path.startswith(folder_prefix):
  2001. continue
  2002. if "/.thumbnails/" in zip_path:
  2003. continue # Skip thumbnails, we'll regenerate them
  2004. relative_path = zip_path[len(folder_prefix) :]
  2005. if not relative_path:
  2006. continue
  2007. # Containment check on the per-entry relative path. ZIP names
  2008. # can carry ``..`` segments by spec; without resolve + parent
  2009. # containment, ``files/<folder>/../../../etc/x`` escapes
  2010. # ``library_dir`` entirely. ``relative_path`` is split into
  2011. # parts because ``safe_join_under`` rejects parts that start
  2012. # with ``/``, and a single combined string would hide an
  2013. # embedded ``..`` segment behind a forward slash.
  2014. file_disk_path = safe_join_under(
  2015. library_dir,
  2016. folder_name,
  2017. *Path(relative_path).parts,
  2018. )
  2019. file_disk_path.parent.mkdir(parents=True, exist_ok=True)
  2020. file_disk_path.write_bytes(file_content)
  2021. # Determine file type
  2022. ext = Path(relative_path).suffix.lower()
  2023. if ext in [".stl", ".3mf", ".obj"]:
  2024. file_type = "model"
  2025. elif ext in [".gcode"]:
  2026. file_type = "gcode"
  2027. elif ext in [".jpg", ".jpeg", ".png", ".gif", ".webp"]:
  2028. file_type = "image"
  2029. else:
  2030. file_type = "other"
  2031. # Create library file record
  2032. lib_file = LibraryFile(
  2033. folder_id=folder.id,
  2034. filename=relative_path,
  2035. file_path=f"{folder_name}/{relative_path}",
  2036. file_type=file_type,
  2037. file_size=len(file_content),
  2038. is_external=False,
  2039. )
  2040. db.add(lib_file)
  2041. await db.flush()
  2042. await db.refresh(project)
  2043. stats = await compute_project_stats(db, project.id, project.target_count, project.target_parts_count)
  2044. return ProjectResponse(
  2045. id=project.id,
  2046. name=project.name,
  2047. description=project.description,
  2048. color=project.color,
  2049. status=project.status,
  2050. target_count=project.target_count,
  2051. target_parts_count=project.target_parts_count,
  2052. target_sets=project.target_sets,
  2053. notes=project.notes,
  2054. attachments=project.attachments,
  2055. url=project.url,
  2056. cover_image_filename=project.cover_image_filename,
  2057. tags=project.tags,
  2058. due_date=project.due_date,
  2059. priority=project.priority,
  2060. budget=project.budget,
  2061. is_template=project.is_template,
  2062. template_source_id=project.template_source_id,
  2063. parent_id=project.parent_id,
  2064. parent_name=None,
  2065. children=[],
  2066. created_at=project.created_at,
  2067. updated_at=project.updated_at,
  2068. stats=stats,
  2069. )