slicer.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """Pydantic schemas for slice requests."""
  2. from typing import Literal
  3. from pydantic import BaseModel, Field, model_validator
  4. class PresetRef(BaseModel):
  5. """A source-aware reference to a printer / process / filament preset.
  6. The SliceModal pulls dropdown options from three tiers (cloud / local /
  7. standard). At submit time the client sends one of these per slot so the
  8. backend knows where to fetch the preset content from at slice time.
  9. """
  10. source: Literal["cloud", "local", "standard"]
  11. id: str = Field(..., description=("Cloud setting_id, local DB row id (stringified), or standard preset name."))
  12. class SliceRequest(BaseModel):
  13. """Body for `POST /library/files/{file_id}/slice`.
  14. Two preset shapes are accepted per slot for backwards-compatibility:
  15. - **Legacy** — bare integer ``*_preset_id`` fields point into the
  16. ``local_presets`` table. Existing clients (and stale browser tabs after
  17. a Bambuddy upgrade) keep working unchanged.
  18. - **Source-aware** — ``*_preset`` carries an explicit
  19. ``{source, id}``. Required for cloud / standard tiers; also accepted
  20. (and equivalent) for local presets when the client is on the new modal.
  21. Exactly one of each pair must be set; the validator normalises legacy
  22. integer ids into a ``PresetRef(source='local', id=str(id))`` so the
  23. downstream resolver only deals with one shape.
  24. """
  25. # Legacy fields — kept optional so older clients continue to work.
  26. printer_preset_id: int | None = Field(
  27. default=None,
  28. description="DEPRECATED: prefer printer_preset. LocalPreset id with preset_type='printer'.",
  29. )
  30. process_preset_id: int | None = Field(
  31. default=None,
  32. description="DEPRECATED: prefer process_preset. LocalPreset id with preset_type='process'.",
  33. )
  34. filament_preset_id: int | None = Field(
  35. default=None,
  36. description="DEPRECATED: prefer filament_preset. LocalPreset id with preset_type='filament'.",
  37. )
  38. # Source-aware fields — set by the new SliceModal.
  39. printer_preset: PresetRef | None = None
  40. process_preset: PresetRef | None = None
  41. filament_preset: PresetRef | None = None
  42. # Multi-color: one PresetRef per AMS slot the source plate uses. Order is
  43. # significant — the slicer matches index-by-index against the plate's
  44. # filament slots. Always preferred over the legacy singular field; the
  45. # validator promotes a singular field into ``[singular]`` when the list
  46. # is empty so older clients keep working.
  47. filament_presets: list[PresetRef] = Field(default_factory=list)
  48. plate: int | None = Field(
  49. default=None,
  50. ge=1,
  51. description="Plate number to slice (1-indexed). Defaults to plate 1 on the sidecar.",
  52. )
  53. export_3mf: bool = Field(
  54. default=False,
  55. description="If true, request a 3MF response with embedded G-code instead of raw G-code.",
  56. )
  57. @model_validator(mode="after")
  58. def normalise_preset_refs(self) -> "SliceRequest":
  59. """Each slot must end up with a `PresetRef` set. Legacy integer ids
  60. become `(source='local', id=str(int))` so the route handler only
  61. deals with the canonical shape. For filament: a non-empty
  62. ``filament_presets`` list satisfies the requirement on its own; an
  63. empty list falls back to the singular fields, which then promote
  64. into a one-element list."""
  65. for slot, ref_attr, legacy_attr in (
  66. ("printer", "printer_preset", "printer_preset_id"),
  67. ("process", "process_preset", "process_preset_id"),
  68. ):
  69. ref = getattr(self, ref_attr)
  70. legacy_id = getattr(self, legacy_attr)
  71. if ref is None and legacy_id is None:
  72. raise ValueError(
  73. f"{slot} preset is required: provide '{ref_attr}' (preferred) or legacy '{legacy_attr}'"
  74. )
  75. if ref is None:
  76. setattr(self, ref_attr, PresetRef(source="local", id=str(legacy_id)))
  77. # Filament accepts THREE shapes, in priority order:
  78. # 1. filament_presets — multi-color array (new clients)
  79. # 2. filament_preset — source-aware singular (single-color new clients)
  80. # 3. filament_preset_id — legacy bare integer (old clients)
  81. # The first non-empty shape wins; missing all three raises.
  82. if not self.filament_presets:
  83. if self.filament_preset is not None:
  84. self.filament_presets = [self.filament_preset]
  85. elif self.filament_preset_id is not None:
  86. fallback = PresetRef(source="local", id=str(self.filament_preset_id))
  87. self.filament_preset = fallback
  88. self.filament_presets = [fallback]
  89. else:
  90. raise ValueError(
  91. "filament preset is required: provide 'filament_presets' (preferred), "
  92. "'filament_preset', or legacy 'filament_preset_id'"
  93. )
  94. elif self.filament_preset is None:
  95. # Multi-color caller: backfill the singular from the first slot
  96. # so callers that still read the legacy field see a stable value.
  97. self.filament_preset = self.filament_presets[0]
  98. return self
  99. class SliceResponse(BaseModel):
  100. """Response from `POST /library/files/{file_id}/slice`. The result lands
  101. in the user's library as a new ``LibraryFile`` (in the same folder as
  102. the source)."""
  103. library_file_id: int
  104. name: str
  105. print_time_seconds: int
  106. filament_used_g: float
  107. filament_used_mm: float
  108. used_embedded_settings: bool = False
  109. class SliceArchiveResponse(BaseModel):
  110. """Response from `POST /archives/{archive_id}/slice`. The result lands
  111. in the user's archives as a new ``PrintArchive`` row, inheriting
  112. printer / project metadata from the source archive."""
  113. archive_id: int
  114. name: str
  115. print_time_seconds: int
  116. filament_used_g: float
  117. filament_used_mm: float
  118. used_embedded_settings: bool = False