project.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. remaining_prints: int | None = None # target_count - total_archives
  72. remaining_parts: int | None = None # target_parts_count - completed_prints
  73. # BOM stats (Phase 7)
  74. bom_total_items: int = 0
  75. bom_completed_items: int = 0
  76. bom_cost: float = 0.0 # Total cost of BOM items (sum of unit_price * quantity_needed)
  77. class ProjectChildPreview(BaseModel):
  78. """A sub-project as listed on its parent's page.
  79. The figures cover the child's *own* subtree, not just its own prints, so
  80. the listed rows add up to the parent's roll-up minus the parent's own
  81. prints (#1264).
  82. """
  83. id: int
  84. name: str
  85. color: str | None
  86. status: str
  87. progress_percent: float | None = None
  88. descendant_count: int = 0 # Sub-projects nested under this one, at any depth
  89. total_archives: int = 0
  90. completed_prints: int = 0
  91. total_print_time_hours: float = 0.0
  92. total_filament_grams: float = 0.0
  93. total_cost: float = 0.0 # Filament + energy + BOM, matching the parent's cost card
  94. class ProjectResponse(BaseModel):
  95. """Schema for project response."""
  96. id: int
  97. name: str
  98. description: str | None
  99. color: str | None
  100. status: str
  101. target_count: int | None
  102. target_parts_count: int | None = None
  103. target_sets: int | None = None # Copies-per-file target (#1897)
  104. notes: str | None = None
  105. attachments: list | None = None
  106. tags: str | None = None
  107. due_date: datetime | None = None
  108. priority: str = "normal"
  109. budget: float | None = None
  110. is_template: bool = False
  111. template_source_id: int | None = None
  112. parent_id: int | None = None
  113. parent_name: str | None = None # For display
  114. children: list[ProjectChildPreview] = []
  115. descendant_count: int = 0 # Sub-projects at any depth beneath this one (#1264)
  116. created_at: datetime
  117. updated_at: datetime
  118. stats: ProjectStats | None = None
  119. # This project's numbers combined with every sub-project's. Null when there
  120. # are none, since it would only repeat ``stats`` (#1264).
  121. rollup_stats: ProjectStats | None = None
  122. url: str | None = None
  123. cover_image_filename: str | None = None
  124. class Config:
  125. from_attributes = True
  126. class ProjectFileProgress(BaseModel):
  127. """Completed-run count for one library file inside a project (#1897)."""
  128. file_id: int
  129. completed_count: int
  130. class ArchivePreview(BaseModel):
  131. """Minimal archive data for project preview."""
  132. id: int
  133. print_name: str | None
  134. thumbnail_path: str | None
  135. status: str
  136. filament_type: str | None = None
  137. filament_color: str | None = None
  138. class ProjectListResponse(BaseModel):
  139. """Schema for project list item (lighter weight)."""
  140. id: int
  141. name: str
  142. description: str | None
  143. color: str | None
  144. status: str
  145. target_count: int | None
  146. target_parts_count: int | None = None
  147. target_sets: int | None = None # Copies-per-file target (#1897); the shared edit dialog needs it
  148. budget: float | None = None
  149. # The edit dialog is shared with the project detail page and seeds its fields
  150. # from whichever project object it is handed, so the list payload has to carry
  151. # everything the dialog edits — otherwise a save from the list view submits a
  152. # blank tags field and a default priority over the stored values (#2536).
  153. tags: str | None = None
  154. due_date: datetime | None = None
  155. priority: str = "normal"
  156. created_at: datetime
  157. # Quick stats
  158. archive_count: int = 0 # Number of print jobs
  159. total_items: int = 0 # Sum of quantities (total items printed, including failed)
  160. completed_count: int = 0 # Sum of quantities for completed prints only
  161. failed_count: int = 0 # Sum of quantities for failed prints
  162. queue_count: int = 0
  163. progress_percent: float | None = None
  164. # Nesting (#1264) — the grid needs both to tell a sub-project apart from a
  165. # top-level one without fetching every project's detail.
  166. parent_id: int | None = None
  167. child_count: int = 0 # Direct sub-projects only
  168. # Preview of archives (up to 5)
  169. archives: list[ArchivePreview] = []
  170. # #1155: card-level metadata
  171. url: str | None = None
  172. cover_image_filename: str | None = None
  173. class Config:
  174. from_attributes = True
  175. class BatchAddArchives(BaseModel):
  176. """Schema for batch adding archives to a project."""
  177. archive_ids: list[int]
  178. class BatchAddQueueItems(BaseModel):
  179. """Schema for batch adding queue items to a project."""
  180. queue_item_ids: list[int]
  181. # Phase 7: BOM Schemas - Tracks sourced/purchased parts
  182. class BOMItemCreate(BaseModel):
  183. """Schema for creating a BOM item."""
  184. name: str
  185. quantity_needed: int = 1
  186. unit_price: float | None = None
  187. sourcing_url: str | None = None
  188. archive_id: int | None = None
  189. stl_filename: str | None = None
  190. remarks: str | None = None
  191. class BOMItemUpdate(BaseModel):
  192. """Schema for updating a BOM item."""
  193. name: str | None = None
  194. quantity_needed: int | None = None
  195. quantity_acquired: int | None = None
  196. unit_price: float | None = None
  197. sourcing_url: str | None = None
  198. archive_id: int | None = None
  199. stl_filename: str | None = None
  200. remarks: str | None = None
  201. class BOMItemResponse(BaseModel):
  202. """Schema for BOM item response."""
  203. id: int
  204. project_id: int
  205. name: str
  206. quantity_needed: int
  207. quantity_acquired: int
  208. unit_price: float | None
  209. sourcing_url: str | None
  210. archive_id: int | None
  211. archive_name: str | None = None
  212. stl_filename: str | None
  213. remarks: str | None
  214. sort_order: int
  215. is_complete: bool = False
  216. created_at: datetime
  217. updated_at: datetime
  218. class Config:
  219. from_attributes = True
  220. # Phase 9: Timeline Schemas
  221. class TimelineEvent(BaseModel):
  222. """Schema for a timeline event."""
  223. event_type: str # archive_added, queue_started, queue_completed, status_changed, note_updated
  224. timestamp: datetime
  225. title: str
  226. description: str | None = None
  227. metadata: dict | None = None # Additional event-specific data
  228. # Phase 10: Import/Export Schemas
  229. class BOMItemExport(BaseModel):
  230. """Schema for exporting a BOM item."""
  231. name: str
  232. quantity_needed: int
  233. quantity_acquired: int
  234. unit_price: float | None
  235. sourcing_url: str | None
  236. stl_filename: str | None
  237. remarks: str | None
  238. class LinkedFolderExport(BaseModel):
  239. """Schema for exporting a linked library folder."""
  240. name: str
  241. class ProjectExport(BaseModel):
  242. """Schema for exporting a project."""
  243. name: str
  244. description: str | None
  245. color: str | None
  246. status: str
  247. target_count: int | None
  248. target_parts_count: int | None
  249. target_sets: int | None = None
  250. notes: str | None
  251. tags: str | None
  252. due_date: datetime | None
  253. priority: str
  254. budget: float | None
  255. bom_items: list[BOMItemExport] = []
  256. linked_folders: list[LinkedFolderExport] = []
  257. class ProjectImport(BaseModel):
  258. """Schema for importing a project."""
  259. name: str
  260. description: str | None = None
  261. color: str | None = None
  262. status: str = "active"
  263. target_count: int | None = None
  264. target_parts_count: int | None = None
  265. target_sets: int | None = None
  266. notes: str | None = None
  267. tags: str | None = None
  268. due_date: datetime | None = None
  269. priority: str = "normal"
  270. budget: float | None = None
  271. bom_items: list[BOMItemExport] = []
  272. linked_folders: list[LinkedFolderExport] = []