print_queue.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from datetime import datetime
  2. from typing import Annotated, Literal
  3. from pydantic import BaseModel, PlainSerializer
  4. # Custom serializer to ensure UTC datetimes have Z suffix
  5. def serialize_utc_datetime(dt: datetime | None) -> str | None:
  6. if dt is None:
  7. return None
  8. # Add Z suffix to indicate UTC
  9. return dt.isoformat() + "Z"
  10. UTCDatetime = Annotated[datetime | None, PlainSerializer(serialize_utc_datetime)]
  11. class PrintQueueItemCreate(BaseModel):
  12. printer_id: int | None = None # None = unassigned, user assigns later
  13. target_model: str | None = None # Target printer model (mutually exclusive with printer_id)
  14. required_filament_types: list[str] | None = None # Required filament types for model-based assignment
  15. # Either archive_id OR library_file_id must be provided
  16. archive_id: int | None = None
  17. library_file_id: int | None = None
  18. scheduled_time: datetime | None = None # None = ASAP (next when idle)
  19. require_previous_success: bool = False
  20. auto_off_after: bool = False # Power off printer after print completes
  21. manual_start: bool = False # Requires manual trigger to start (staged)
  22. # AMS mapping: list of global tray IDs for each filament slot
  23. # Format: [5, -1, 2, -1] where position = slot_id-1, value = global tray ID (-1 = unused)
  24. ams_mapping: list[int] | None = None
  25. # Plate ID for multi-plate 3MF files (1-indexed, None = auto-detect/plate 1)
  26. plate_id: int | None = None
  27. # Print options
  28. bed_levelling: bool = True
  29. flow_cali: bool = False
  30. vibration_cali: bool = True
  31. layer_inspect: bool = False
  32. timelapse: bool = False
  33. use_ams: bool = True
  34. class PrintQueueItemUpdate(BaseModel):
  35. printer_id: int | None = None
  36. target_model: str | None = None # Target printer model (mutually exclusive with printer_id)
  37. position: int | None = None
  38. scheduled_time: datetime | None = None
  39. require_previous_success: bool | None = None
  40. auto_off_after: bool | None = None
  41. manual_start: bool | None = None
  42. ams_mapping: list[int] | None = None
  43. plate_id: int | None = None
  44. # Print options
  45. bed_levelling: bool | None = None
  46. flow_cali: bool | None = None
  47. vibration_cali: bool | None = None
  48. layer_inspect: bool | None = None
  49. timelapse: bool | None = None
  50. use_ams: bool | None = None
  51. class PrintQueueItemResponse(BaseModel):
  52. id: int
  53. printer_id: int | None # None = unassigned
  54. target_model: str | None = None # Target printer model for model-based assignment
  55. required_filament_types: list[str] | None = None # Required filament types for model-based assignment
  56. waiting_reason: str | None = None # Why a model-based job hasn't started yet
  57. archive_id: int | None # None if library_file_id is set (archive created at print start)
  58. library_file_id: int | None # For queue items from library files
  59. position: int
  60. scheduled_time: UTCDatetime
  61. require_previous_success: bool
  62. auto_off_after: bool
  63. manual_start: bool
  64. ams_mapping: list[int] | None = None
  65. plate_id: int | None = None # Plate ID for multi-plate 3MF files
  66. # Print options
  67. bed_levelling: bool = True
  68. flow_cali: bool = False
  69. vibration_cali: bool = True
  70. layer_inspect: bool = False
  71. timelapse: bool = False
  72. use_ams: bool = True
  73. status: Literal["pending", "printing", "completed", "failed", "skipped", "cancelled"]
  74. started_at: UTCDatetime
  75. completed_at: UTCDatetime
  76. error_message: str | None
  77. created_at: UTCDatetime
  78. # Nested info for UI (populated in route)
  79. archive_name: str | None = None
  80. archive_thumbnail: str | None = None
  81. library_file_name: str | None = None # Name of library file (if library_file_id is set)
  82. library_file_thumbnail: str | None = None # Thumbnail of library file
  83. printer_name: str | None = None
  84. print_time_seconds: int | None = None # Estimated print time from archive or library file
  85. # User tracking (Issue #206)
  86. created_by_id: int | None = None
  87. created_by_username: str | None = None
  88. class Config:
  89. from_attributes = True
  90. class PrintQueueReorderItem(BaseModel):
  91. id: int
  92. position: int
  93. class PrintQueueReorder(BaseModel):
  94. items: list[PrintQueueReorderItem]
  95. class PrintQueueBulkUpdate(BaseModel):
  96. """Bulk update multiple queue items with the same values."""
  97. item_ids: list[int]
  98. # Fields to update (all optional - only set fields are applied)
  99. printer_id: int | None = None
  100. scheduled_time: datetime | None = None
  101. require_previous_success: bool | None = None
  102. auto_off_after: bool | None = None
  103. manual_start: bool | None = None
  104. # Print options
  105. bed_levelling: bool | None = None
  106. flow_cali: bool | None = None
  107. vibration_cali: bool | None = None
  108. layer_inspect: bool | None = None
  109. timelapse: bool | None = None
  110. use_ams: bool | None = None
  111. class PrintQueueBulkUpdateResponse(BaseModel):
  112. """Response for bulk update operation."""
  113. updated_count: int
  114. skipped_count: int # Items that were not pending
  115. message: str