archive.py 5.8 KB

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