project.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. from datetime import datetime
  2. from pydantic import BaseModel, field_validator
  3. def _validate_project_url(value: str | None) -> str | None:
  4. """Reject anything that isn't an http(s) URL — the URL is rendered as a
  5. clickable `<a href>` so a `javascript:` / `data:` / `file:` value would be
  6. an XSS vector even with React's default escaping (#1155)."""
  7. if value is None:
  8. return value
  9. trimmed = value.strip()
  10. if not trimmed:
  11. return None
  12. lowered = trimmed.lower()
  13. if not (lowered.startswith("http://") or lowered.startswith("https://")):
  14. raise ValueError("url must start with http:// or https://")
  15. return trimmed
  16. class ProjectCreate(BaseModel):
  17. """Schema for creating a new project."""
  18. name: str
  19. description: str | None = None
  20. color: str | None = None
  21. target_count: int | None = None
  22. target_parts_count: int | None = None
  23. target_sets: int | None = None # Copies-per-file target (#1897)
  24. notes: str | None = None
  25. tags: str | None = None
  26. due_date: datetime | None = None
  27. priority: str = "normal"
  28. budget: float | None = None
  29. parent_id: int | None = None # For sub-projects
  30. url: str | None = None
  31. @field_validator("url")
  32. @classmethod
  33. def _check_url(cls, v: str | None) -> str | None:
  34. return _validate_project_url(v)
  35. class ProjectUpdate(BaseModel):
  36. """Schema for updating a project."""
  37. name: str | None = None
  38. description: str | None = None
  39. color: str | None = None
  40. status: str | None = None # active, completed, archived
  41. target_count: int | None = None
  42. target_parts_count: int | None = None
  43. target_sets: int | None = None # Copies-per-file target (#1897)
  44. notes: str | None = None
  45. tags: str | None = None
  46. due_date: datetime | None = None
  47. priority: str | None = None
  48. budget: float | None = None
  49. parent_id: int | None = None
  50. url: str | None = None
  51. @field_validator("url")
  52. @classmethod
  53. def _check_url(cls, v: str | None) -> str | None:
  54. return _validate_project_url(v)
  55. class ProjectStats(BaseModel):
  56. """Statistics for a project."""
  57. total_archives: int = 0 # Number of archive records
  58. total_items: int = 0 # Sum of quantities (total items printed)
  59. completed_prints: int = 0 # Sum of quantities for completed prints
  60. failed_prints: int = 0 # Sum of quantities for failed prints
  61. queued_prints: int = 0
  62. in_progress_prints: int = 0
  63. total_print_time_hours: float = 0.0
  64. total_filament_grams: float = 0.0
  65. progress_percent: float | None = None # Based on target_count (plates)
  66. parts_progress_percent: float | None = None # Based on target_parts_count
  67. # Cost tracking (Phase 6)
  68. estimated_cost: float = 0.0 # Based on filament cost
  69. total_energy_kwh: float = 0.0
  70. total_energy_cost: float = 0.0
  71. total_wear_cost: float = 0.0 # Printer wear (#694)
  72. remaining_prints: int | None = None # target_count - total_archives
  73. remaining_parts: int | None = None # target_parts_count - completed_prints
  74. # BOM stats (Phase 7)
  75. bom_total_items: int = 0
  76. bom_completed_items: int = 0
  77. bom_cost: float = 0.0 # Total cost of BOM items (sum of unit_price * quantity_needed)
  78. class ProjectChildPreview(BaseModel):
  79. """A sub-project as listed on its parent's page.
  80. The figures cover the child's *own* subtree, not just its own prints, so
  81. the listed rows add up to the parent's roll-up minus the parent's own
  82. prints (#1264).
  83. """
  84. id: int
  85. name: str
  86. color: str | None
  87. status: str
  88. progress_percent: float | None = None
  89. descendant_count: int = 0 # Sub-projects nested under this one, at any depth
  90. total_archives: int = 0
  91. completed_prints: int = 0
  92. total_print_time_hours: float = 0.0
  93. total_filament_grams: float = 0.0
  94. total_cost: float = 0.0 # Filament + energy + BOM, matching the parent's cost card
  95. class ProjectResponse(BaseModel):
  96. """Schema for project response."""
  97. id: int
  98. name: str
  99. description: str | None
  100. color: str | None
  101. status: str
  102. target_count: int | None
  103. target_parts_count: int | None = None
  104. target_sets: int | None = None # Copies-per-file target (#1897)
  105. notes: str | None = None
  106. attachments: list | None = None
  107. tags: str | None = None
  108. due_date: datetime | None = None
  109. priority: str = "normal"
  110. budget: float | None = None
  111. is_template: bool = False
  112. template_source_id: int | None = None
  113. parent_id: int | None = None
  114. parent_name: str | None = None # For display
  115. children: list[ProjectChildPreview] = []
  116. descendant_count: int = 0 # Sub-projects at any depth beneath this one (#1264)
  117. created_at: datetime
  118. updated_at: datetime
  119. stats: ProjectStats | None = None
  120. # This project's numbers combined with every sub-project's. Null when there
  121. # are none, since it would only repeat ``stats`` (#1264).
  122. rollup_stats: ProjectStats | None = None
  123. url: str | None = None
  124. cover_image_filename: str | None = None
  125. class Config:
  126. from_attributes = True
  127. class ProjectFileProgress(BaseModel):
  128. """Completed-run count for one library file inside a project (#1897)."""
  129. file_id: int
  130. completed_count: int
  131. class ArchivePreview(BaseModel):
  132. """Minimal archive data for project preview."""
  133. id: int
  134. print_name: str | None
  135. thumbnail_path: str | None
  136. status: str
  137. filament_type: str | None = None
  138. filament_color: str | None = None
  139. class ProjectListResponse(BaseModel):
  140. """Schema for project list item (lighter weight)."""
  141. id: int
  142. name: str
  143. description: str | None
  144. color: str | None
  145. status: str
  146. target_count: int | None
  147. target_parts_count: int | None = None
  148. target_sets: int | None = None # Copies-per-file target (#1897); the shared edit dialog needs it
  149. budget: float | None = None
  150. # The edit dialog is shared with the project detail page and seeds its fields
  151. # from whichever project object it is handed, so the list payload has to carry
  152. # everything the dialog edits — otherwise a save from the list view submits a
  153. # blank tags field and a default priority over the stored values (#2536).
  154. tags: str | None = None
  155. due_date: datetime | None = None
  156. priority: str = "normal"
  157. created_at: datetime
  158. # Quick stats
  159. archive_count: int = 0 # Number of print jobs
  160. total_items: int = 0 # Sum of quantities (total items printed, including failed)
  161. completed_count: int = 0 # Sum of quantities for completed prints only
  162. failed_count: int = 0 # Sum of quantities for failed prints
  163. queue_count: int = 0
  164. progress_percent: float | None = None
  165. # Nesting (#1264) — the grid needs both to tell a sub-project apart from a
  166. # top-level one without fetching every project's detail.
  167. parent_id: int | None = None
  168. child_count: int = 0 # Direct sub-projects only
  169. # Preview of archives (up to 5)
  170. archives: list[ArchivePreview] = []
  171. # #1155: card-level metadata
  172. url: str | None = None
  173. cover_image_filename: str | None = None
  174. class Config:
  175. from_attributes = True
  176. class BatchAddArchives(BaseModel):
  177. """Schema for batch adding archives to a project."""
  178. archive_ids: list[int]
  179. class BatchAddQueueItems(BaseModel):
  180. """Schema for batch adding queue items to a project."""
  181. queue_item_ids: list[int]
  182. # Phase 7: BOM Schemas - Tracks sourced/purchased parts
  183. class BOMItemCreate(BaseModel):
  184. """Schema for creating a BOM item."""
  185. name: str
  186. quantity_needed: int = 1
  187. unit_price: float | None = None
  188. sourcing_url: str | None = None
  189. archive_id: int | None = None
  190. stl_filename: str | None = None
  191. remarks: str | None = None
  192. class BOMItemUpdate(BaseModel):
  193. """Schema for updating a BOM item."""
  194. name: str | None = None
  195. quantity_needed: int | None = None
  196. quantity_acquired: int | None = None
  197. unit_price: float | None = None
  198. sourcing_url: str | None = None
  199. archive_id: int | None = None
  200. stl_filename: str | None = None
  201. remarks: str | None = None
  202. class BOMItemResponse(BaseModel):
  203. """Schema for BOM item response."""
  204. id: int
  205. project_id: int
  206. name: str
  207. quantity_needed: int
  208. quantity_acquired: int
  209. unit_price: float | None
  210. sourcing_url: str | None
  211. archive_id: int | None
  212. archive_name: str | None = None
  213. stl_filename: str | None
  214. remarks: str | None
  215. sort_order: int
  216. is_complete: bool = False
  217. created_at: datetime
  218. updated_at: datetime
  219. class Config:
  220. from_attributes = True
  221. # Phase 9: Timeline Schemas
  222. class TimelineEvent(BaseModel):
  223. """Schema for a timeline event."""
  224. event_type: str # archive_added, queue_started, queue_completed, status_changed, note_updated
  225. timestamp: datetime
  226. title: str
  227. description: str | None = None
  228. metadata: dict | None = None # Additional event-specific data
  229. # Phase 10: Import/Export Schemas
  230. class BOMItemExport(BaseModel):
  231. """Schema for exporting a BOM item."""
  232. name: str
  233. quantity_needed: int
  234. quantity_acquired: int
  235. unit_price: float | None
  236. sourcing_url: str | None
  237. stl_filename: str | None
  238. remarks: str | None
  239. class LinkedFolderExport(BaseModel):
  240. """Schema for exporting a linked library folder."""
  241. name: str
  242. class ProjectExport(BaseModel):
  243. """Schema for exporting a project."""
  244. name: str
  245. description: str | None
  246. color: str | None
  247. status: str
  248. target_count: int | None
  249. target_parts_count: int | None
  250. target_sets: int | None = None
  251. notes: str | None
  252. tags: str | None
  253. due_date: datetime | None
  254. priority: str
  255. budget: float | None
  256. bom_items: list[BOMItemExport] = []
  257. linked_folders: list[LinkedFolderExport] = []
  258. class ProjectImport(BaseModel):
  259. """Schema for importing a project."""
  260. name: str
  261. description: str | None = None
  262. color: str | None = None
  263. status: str = "active"
  264. target_count: int | None = None
  265. target_parts_count: int | None = None
  266. target_sets: int | None = None
  267. notes: str | None = None
  268. tags: str | None = None
  269. due_date: datetime | None = None
  270. priority: str = "normal"
  271. budget: float | None = None
  272. bom_items: list[BOMItemExport] = []
  273. linked_folders: list[LinkedFolderExport] = []