archive.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. from datetime import datetime
  2. from pydantic import BaseModel, model_validator
  3. class ArchiveBase(BaseModel):
  4. print_name: str | None = None
  5. is_favorite: bool | None = None
  6. tags: str | None = None
  7. notes: str | None = None
  8. cost: float | None = None
  9. failure_reason: str | None = None
  10. quantity: int | None = None # Number of items printed
  11. # User-defined link (Printables, Thingiverse, etc.)
  12. external_url: str | None = None
  13. class ArchiveUpdate(ArchiveBase):
  14. printer_id: int | None = None
  15. project_id: int | None = None
  16. # Allow changing status (e.g., clearing failed flag)
  17. status: str | None = None
  18. class ArchiveDuplicate(BaseModel):
  19. """Reference to a duplicate archive."""
  20. id: int
  21. print_name: str | None
  22. created_at: datetime
  23. match_type: str # "exact" (hash match) or "similar" (name match)
  24. class ArchiveResponse(BaseModel):
  25. id: int
  26. printer_id: int | None
  27. project_id: int | None = None
  28. project_name: str | None = None # Included for convenience
  29. filename: str
  30. file_path: str
  31. file_size: int
  32. content_hash: str | None
  33. thumbnail_path: str | None
  34. timelapse_path: str | None
  35. source_3mf_path: str | None = None # Original project 3MF from slicer
  36. f3d_path: str | None = None # Fusion 360 design file
  37. # Duplicate detection
  38. duplicates: list[ArchiveDuplicate] | None = None
  39. duplicate_count: int = 0 # Quick count for list views
  40. # Object count (computed from extra_data.printable_objects)
  41. object_count: int | None = None
  42. print_name: str | None
  43. print_time_seconds: int | None # Estimated time from slicer
  44. actual_time_seconds: int | None = None # Computed from started_at/completed_at
  45. # Percentage: 100 = perfect, >100 = faster than estimated
  46. time_accuracy: float | None = None
  47. filament_used_grams: float | None
  48. filament_type: str | None
  49. filament_color: str | None
  50. layer_height: float | None
  51. total_layers: int | None = None
  52. nozzle_diameter: float | None
  53. bed_temperature: int | None
  54. nozzle_temperature: int | None
  55. sliced_for_model: str | None = None # Printer model this file was sliced for
  56. status: str
  57. started_at: datetime | None
  58. completed_at: datetime | None
  59. extra_data: dict | None
  60. makerworld_url: str | None
  61. designer: str | None
  62. # User-defined link (Printables, Thingiverse, etc.)
  63. external_url: str | None = None
  64. is_favorite: bool
  65. tags: str | None
  66. notes: str | None
  67. cost: float | None
  68. photos: list | None
  69. failure_reason: str | None
  70. quantity: int = 1 # Number of items printed
  71. # Energy tracking
  72. energy_kwh: float | None = None
  73. energy_cost: float | None = None
  74. created_at: datetime
  75. # User tracking (Issue #206)
  76. created_by_id: int | None = None
  77. created_by_username: str | None = None
  78. @model_validator(mode="after")
  79. def compute_object_count(self) -> "ArchiveResponse":
  80. """Compute object_count from extra_data.printable_objects if not set."""
  81. if self.object_count is None and self.extra_data:
  82. printable_objects = self.extra_data.get("printable_objects")
  83. if printable_objects and isinstance(printable_objects, dict):
  84. self.object_count = len(printable_objects)
  85. return self
  86. class Config:
  87. from_attributes = True
  88. class ArchiveSlim(BaseModel):
  89. """Lightweight archive response for stats/dashboard widgets."""
  90. printer_id: int | None
  91. print_name: str | None
  92. print_time_seconds: int | None
  93. actual_time_seconds: int | None = None
  94. filament_used_grams: float | None
  95. filament_type: str | None
  96. filament_color: str | None
  97. status: str
  98. started_at: datetime | None
  99. completed_at: datetime | None
  100. cost: float | None
  101. quantity: int = 1
  102. created_at: datetime
  103. class Config:
  104. from_attributes = True
  105. class ArchiveStats(BaseModel):
  106. total_prints: int
  107. successful_prints: int
  108. failed_prints: int
  109. total_print_time_hours: float
  110. total_filament_grams: float
  111. total_cost: float
  112. prints_by_filament_type: dict
  113. prints_by_printer: dict
  114. # Time accuracy stats
  115. # Average across all prints with data
  116. average_time_accuracy: float | None = None
  117. time_accuracy_by_printer: dict | None = None # Per-printer accuracy
  118. # Energy stats
  119. total_energy_kwh: float = 0.0
  120. total_energy_cost: float = 0.0
  121. class ProjectPageImage(BaseModel):
  122. """Image embedded in 3MF project page."""
  123. name: str
  124. path: str # Path within 3MF
  125. url: str # API URL to fetch image
  126. class ProjectPageResponse(BaseModel):
  127. """Project page data extracted from 3MF file."""
  128. # Model info
  129. title: str | None = None
  130. description: str | None = None # HTML content
  131. designer: str | None = None
  132. designer_user_id: str | None = None
  133. license: str | None = None
  134. copyright: str | None = None
  135. creation_date: str | None = None
  136. modification_date: str | None = None
  137. origin: str | None = None # "original" or "remix"
  138. # Profile info
  139. profile_title: str | None = None
  140. profile_description: str | None = None
  141. profile_cover: str | None = None
  142. profile_user_id: str | None = None
  143. profile_user_name: str | None = None
  144. # MakerWorld info
  145. design_model_id: str | None = None
  146. design_profile_id: str | None = None
  147. design_region: str | None = None
  148. # Images
  149. model_pictures: list[ProjectPageImage] = []
  150. profile_pictures: list[ProjectPageImage] = []
  151. thumbnails: list[ProjectPageImage] = []
  152. class ProjectPageUpdate(BaseModel):
  153. """Update project page data in 3MF file."""
  154. title: str | None = None
  155. description: str | None = None
  156. designer: str | None = None
  157. license: str | None = None
  158. copyright: str | None = None
  159. profile_title: str | None = None
  160. profile_description: str | None = None
  161. class ReprintRequest(BaseModel):
  162. """Request body for reprinting an archive."""
  163. # Plate selection for multi-plate 3MF files
  164. # If not specified, auto-detects from file (legacy behavior for single-plate files)
  165. plate_id: int | None = None
  166. plate_name: str | None = None
  167. # AMS slot mapping: list of tray IDs for each filament slot in the 3MF
  168. # Global tray ID = (ams_id * 4) + slot_id, external = 254
  169. ams_mapping: list[int] | None = None
  170. # Print options
  171. bed_levelling: bool = True
  172. flow_cali: bool = False
  173. vibration_cali: bool = True
  174. layer_inspect: bool = False
  175. timelapse: bool = False
  176. use_ams: bool = True # Not exposed in UI, but needed for API