project.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. notes: str | None = None
  24. tags: str | None = None
  25. due_date: datetime | None = None
  26. priority: str = "normal"
  27. budget: float | None = None
  28. parent_id: int | None = None # For sub-projects
  29. url: str | None = None
  30. @field_validator("url")
  31. @classmethod
  32. def _check_url(cls, v: str | None) -> str | None:
  33. return _validate_project_url(v)
  34. class ProjectUpdate(BaseModel):
  35. """Schema for updating a project."""
  36. name: str | None = None
  37. description: str | None = None
  38. color: str | None = None
  39. status: str | None = None # active, completed, archived
  40. target_count: int | None = None
  41. target_parts_count: int | None = None
  42. notes: str | None = None
  43. tags: str | None = None
  44. due_date: datetime | None = None
  45. priority: str | None = None
  46. budget: float | None = None
  47. parent_id: int | None = None
  48. url: str | None = None
  49. @field_validator("url")
  50. @classmethod
  51. def _check_url(cls, v: str | None) -> str | None:
  52. return _validate_project_url(v)
  53. class ProjectStats(BaseModel):
  54. """Statistics for a project."""
  55. total_archives: int = 0 # Number of archive records
  56. total_items: int = 0 # Sum of quantities (total items printed)
  57. completed_prints: int = 0 # Sum of quantities for completed prints
  58. failed_prints: int = 0 # Sum of quantities for failed prints
  59. queued_prints: int = 0
  60. in_progress_prints: int = 0
  61. total_print_time_hours: float = 0.0
  62. total_filament_grams: float = 0.0
  63. progress_percent: float | None = None # Based on target_count (plates)
  64. parts_progress_percent: float | None = None # Based on target_parts_count
  65. # Cost tracking (Phase 6)
  66. estimated_cost: float = 0.0 # Based on filament cost
  67. total_energy_kwh: float = 0.0
  68. total_energy_cost: float = 0.0
  69. remaining_prints: int | None = None # target_count - total_archives
  70. remaining_parts: int | None = None # target_parts_count - completed_prints
  71. # BOM stats (Phase 7)
  72. bom_total_items: int = 0
  73. bom_completed_items: int = 0
  74. bom_cost: float = 0.0 # Total cost of BOM items (sum of unit_price * quantity_needed)
  75. class ProjectChildPreview(BaseModel):
  76. """Minimal project data for child preview."""
  77. id: int
  78. name: str
  79. color: str | None
  80. status: str
  81. progress_percent: float | None = None
  82. class ProjectResponse(BaseModel):
  83. """Schema for project response."""
  84. id: int
  85. name: str
  86. description: str | None
  87. color: str | None
  88. status: str
  89. target_count: int | None
  90. target_parts_count: int | None = None
  91. notes: str | None = None
  92. attachments: list | None = None
  93. tags: str | None = None
  94. due_date: datetime | None = None
  95. priority: str = "normal"
  96. budget: float | None = None
  97. is_template: bool = False
  98. template_source_id: int | None = None
  99. parent_id: int | None = None
  100. parent_name: str | None = None # For display
  101. children: list[ProjectChildPreview] = []
  102. created_at: datetime
  103. updated_at: datetime
  104. stats: ProjectStats | None = None
  105. url: str | None = None
  106. cover_image_filename: str | None = None
  107. class Config:
  108. from_attributes = True
  109. class ArchivePreview(BaseModel):
  110. """Minimal archive data for project preview."""
  111. id: int
  112. print_name: str | None
  113. thumbnail_path: str | None
  114. status: str
  115. filament_type: str | None = None
  116. filament_color: str | None = None
  117. class ProjectListResponse(BaseModel):
  118. """Schema for project list item (lighter weight)."""
  119. id: int
  120. name: str
  121. description: str | None
  122. color: str | None
  123. status: str
  124. target_count: int | None
  125. target_parts_count: int | None = None
  126. budget: float | None = None
  127. # The edit dialog is shared with the project detail page and seeds its fields
  128. # from whichever project object it is handed, so the list payload has to carry
  129. # everything the dialog edits — otherwise a save from the list view submits a
  130. # blank tags field and a default priority over the stored values (#2536).
  131. tags: str | None = None
  132. due_date: datetime | None = None
  133. priority: str = "normal"
  134. created_at: datetime
  135. # Quick stats
  136. archive_count: int = 0 # Number of print jobs
  137. total_items: int = 0 # Sum of quantities (total items printed, including failed)
  138. completed_count: int = 0 # Sum of quantities for completed prints only
  139. failed_count: int = 0 # Sum of quantities for failed prints
  140. queue_count: int = 0
  141. progress_percent: float | None = None
  142. # Preview of archives (up to 5)
  143. archives: list[ArchivePreview] = []
  144. # #1155: card-level metadata
  145. url: str | None = None
  146. cover_image_filename: str | None = None
  147. class Config:
  148. from_attributes = True
  149. class BatchAddArchives(BaseModel):
  150. """Schema for batch adding archives to a project."""
  151. archive_ids: list[int]
  152. class BatchAddQueueItems(BaseModel):
  153. """Schema for batch adding queue items to a project."""
  154. queue_item_ids: list[int]
  155. # Phase 7: BOM Schemas - Tracks sourced/purchased parts
  156. class BOMItemCreate(BaseModel):
  157. """Schema for creating a BOM item."""
  158. name: str
  159. quantity_needed: int = 1
  160. unit_price: float | None = None
  161. sourcing_url: str | None = None
  162. archive_id: int | None = None
  163. stl_filename: str | None = None
  164. remarks: str | None = None
  165. class BOMItemUpdate(BaseModel):
  166. """Schema for updating a BOM item."""
  167. name: str | None = None
  168. quantity_needed: int | None = None
  169. quantity_acquired: int | None = None
  170. unit_price: float | None = None
  171. sourcing_url: str | None = None
  172. archive_id: int | None = None
  173. stl_filename: str | None = None
  174. remarks: str | None = None
  175. class BOMItemResponse(BaseModel):
  176. """Schema for BOM item response."""
  177. id: int
  178. project_id: int
  179. name: str
  180. quantity_needed: int
  181. quantity_acquired: int
  182. unit_price: float | None
  183. sourcing_url: str | None
  184. archive_id: int | None
  185. archive_name: str | None = None
  186. stl_filename: str | None
  187. remarks: str | None
  188. sort_order: int
  189. is_complete: bool = False
  190. created_at: datetime
  191. updated_at: datetime
  192. class Config:
  193. from_attributes = True
  194. # Phase 9: Timeline Schemas
  195. class TimelineEvent(BaseModel):
  196. """Schema for a timeline event."""
  197. event_type: str # archive_added, queue_started, queue_completed, status_changed, note_updated
  198. timestamp: datetime
  199. title: str
  200. description: str | None = None
  201. metadata: dict | None = None # Additional event-specific data
  202. # Phase 10: Import/Export Schemas
  203. class BOMItemExport(BaseModel):
  204. """Schema for exporting a BOM item."""
  205. name: str
  206. quantity_needed: int
  207. quantity_acquired: int
  208. unit_price: float | None
  209. sourcing_url: str | None
  210. stl_filename: str | None
  211. remarks: str | None
  212. class LinkedFolderExport(BaseModel):
  213. """Schema for exporting a linked library folder."""
  214. name: str
  215. class ProjectExport(BaseModel):
  216. """Schema for exporting a project."""
  217. name: str
  218. description: str | None
  219. color: str | None
  220. status: str
  221. target_count: int | None
  222. target_parts_count: int | None
  223. notes: str | None
  224. tags: str | None
  225. due_date: datetime | None
  226. priority: str
  227. budget: float | None
  228. bom_items: list[BOMItemExport] = []
  229. linked_folders: list[LinkedFolderExport] = []
  230. class ProjectImport(BaseModel):
  231. """Schema for importing a project."""
  232. name: str
  233. description: str | None = None
  234. color: str | None = None
  235. status: str = "active"
  236. target_count: int | None = None
  237. target_parts_count: int | None = None
  238. notes: str | None = None
  239. tags: str | None = None
  240. due_date: datetime | None = None
  241. priority: str = "normal"
  242. budget: float | None = None
  243. bom_items: list[BOMItemExport] = []
  244. linked_folders: list[LinkedFolderExport] = []