spool.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. from datetime import datetime
  2. from pydantic import BaseModel, Field, field_validator
  3. # Visual variant applied to a spool's swatch — purely cosmetic, does not
  4. # affect MQTT/firmware. Kept independent of `subtype` so users can override
  5. # the rendering hint without touching Bambu's categorical filament label.
  6. # Mirrors the visual variants the spool form's `KNOWN_VARIANTS` exposes so
  7. # the catalog and spool form share one vocabulary; structural variants like
  8. # gradient/dual-color/tri-color/multicolor combine with `extra_colors` for
  9. # rendering, surface effects (sparkle/wood/marble/glow/matte) layer overlays.
  10. ALLOWED_EFFECT_TYPES = frozenset(
  11. {
  12. # Surface effects
  13. "sparkle",
  14. "wood",
  15. "marble",
  16. "glow",
  17. "matte",
  18. # Sheen / finish variants
  19. "silk",
  20. "galaxy",
  21. "rainbow",
  22. "metal",
  23. "translucent",
  24. # Multi-colour structures (drive gradient rendering when paired with extra_colors)
  25. "gradient",
  26. "dual-color",
  27. "tri-color",
  28. "multicolor",
  29. }
  30. )
  31. # Cap how many gradient stops we accept on input so a paste of arbitrary text
  32. # can't blow up the stored value or downstream rendering.
  33. MAX_EXTRA_COLOR_STOPS = 8
  34. def normalize_extra_colors(value: str | None) -> str | None:
  35. """Parse comma-separated hex tokens into canonical lowercase form.
  36. Accepts 6- or 8-char hex per token, with or without leading `#`. Returns
  37. None for blank input, raises ValueError for malformed tokens or too many
  38. stops. Output is the comma-joined canonical form (no `#`, lowercase).
  39. """
  40. if value is None:
  41. return None
  42. raw = value.strip()
  43. if not raw:
  44. return None
  45. tokens = [tok.strip().lstrip("#").lower() for tok in raw.split(",") if tok.strip()]
  46. if not tokens:
  47. return None
  48. if len(tokens) > MAX_EXTRA_COLOR_STOPS:
  49. raise ValueError(f"extra_colors accepts at most {MAX_EXTRA_COLOR_STOPS} stops")
  50. for tok in tokens:
  51. if len(tok) not in (6, 8):
  52. raise ValueError(f"extra_colors token '{tok}' must be 6 or 8 hex chars")
  53. try:
  54. int(tok, 16)
  55. except ValueError as exc:
  56. raise ValueError(f"extra_colors token '{tok}' is not valid hex") from exc
  57. return ",".join(tokens)
  58. def normalize_effect_type(value: str | None) -> str | None:
  59. if value is None:
  60. return None
  61. trimmed = value.strip().lower()
  62. if not trimmed:
  63. return None
  64. # Tolerate "Dual Color" / "dual_color" / "dual color" → "dual-color" so
  65. # users pasting from spool-subtype labels don't hit a validation wall.
  66. canonical = trimmed.replace("_", "-").replace(" ", "-")
  67. if canonical not in ALLOWED_EFFECT_TYPES:
  68. raise ValueError(f"effect_type must be one of: {sorted(ALLOWED_EFFECT_TYPES)}")
  69. return canonical
  70. class SpoolBase(BaseModel):
  71. material: str = Field(..., min_length=1, max_length=50)
  72. subtype: str | None = None
  73. color_name: str | None = None
  74. rgba: str | None = Field(None, pattern=r"^[0-9A-Fa-f]{8}$")
  75. extra_colors: str | None = None
  76. effect_type: str | None = None
  77. brand: str | None = None
  78. @field_validator("extra_colors")
  79. @classmethod
  80. def _validate_extra_colors(cls, v: str | None) -> str | None:
  81. return normalize_extra_colors(v)
  82. @field_validator("effect_type")
  83. @classmethod
  84. def _validate_effect_type(cls, v: str | None) -> str | None:
  85. return normalize_effect_type(v)
  86. label_weight: int = 1000
  87. core_weight: int = 250
  88. core_weight_catalog_id: int | None = None
  89. weight_used: float = 0
  90. slicer_filament: str | None = None
  91. slicer_filament_name: str | None = None
  92. nozzle_temp_min: int | None = None
  93. nozzle_temp_max: int | None = None
  94. note: str | None = None
  95. tag_uid: str | None = None
  96. tray_uuid: str | None = None
  97. data_origin: str | None = None
  98. tag_type: str | None = None
  99. cost_per_kg: float | None = Field(default=None, ge=0)
  100. weight_locked: bool = False
  101. last_scale_weight: int | None = None
  102. last_weighed_at: datetime | None = None
  103. # User-defined category + per-spool low-stock threshold override (#729).
  104. category: str | None = Field(default=None, max_length=50)
  105. low_stock_threshold_pct: int | None = Field(default=None, ge=1, le=99)
  106. # Free-text storage location, distinct from `location` (AMS slot
  107. # assignment). Column has lived on the ORM since the inventory rework
  108. # but was missing from this schema, so writes were silently dropped (#1291).
  109. storage_location: str | None = Field(default=None, max_length=255)
  110. class SpoolCreate(SpoolBase):
  111. pass
  112. class SpoolBulkCreate(BaseModel):
  113. spool: SpoolCreate
  114. quantity: int = Field(default=1, ge=1, le=100)
  115. class SpoolUpdate(BaseModel):
  116. material: str | None = None
  117. subtype: str | None = None
  118. color_name: str | None = None
  119. rgba: str | None = Field(None, pattern=r"^[0-9A-Fa-f]{8}$")
  120. extra_colors: str | None = None
  121. effect_type: str | None = None
  122. brand: str | None = None
  123. @field_validator("extra_colors")
  124. @classmethod
  125. def _validate_extra_colors(cls, v: str | None) -> str | None:
  126. return normalize_extra_colors(v)
  127. @field_validator("effect_type")
  128. @classmethod
  129. def _validate_effect_type(cls, v: str | None) -> str | None:
  130. return normalize_effect_type(v)
  131. label_weight: int | None = None
  132. core_weight: int | None = None
  133. core_weight_catalog_id: int | None = None
  134. weight_used: float | None = None
  135. slicer_filament: str | None = None
  136. slicer_filament_name: str | None = None
  137. nozzle_temp_min: int | None = None
  138. nozzle_temp_max: int | None = None
  139. note: str | None = None
  140. tag_uid: str | None = None
  141. tray_uuid: str | None = None
  142. data_origin: str | None = None
  143. tag_type: str | None = None
  144. cost_per_kg: float | None = Field(default=None, ge=0)
  145. weight_locked: bool | None = None
  146. # User-defined category + per-spool low-stock threshold override (#729).
  147. category: str | None = Field(default=None, max_length=50)
  148. low_stock_threshold_pct: int | None = Field(default=None, ge=1, le=99)
  149. storage_location: str | None = Field(default=None, max_length=255)
  150. class SpoolKProfileBase(BaseModel):
  151. printer_id: int
  152. extruder: int = 0
  153. nozzle_diameter: str = "0.4"
  154. nozzle_type: str | None = None
  155. k_value: float
  156. name: str | None = None
  157. cali_idx: int | None = None
  158. setting_id: str | None = None
  159. class SpoolKProfileResponse(SpoolKProfileBase):
  160. id: int
  161. spool_id: int
  162. created_at: datetime
  163. class Config:
  164. from_attributes = True
  165. class SpoolResponse(SpoolBase):
  166. id: int
  167. # rgba is intentionally unconstrained on the response side: the write paths
  168. # (SpoolCreate, SpoolUpdate) enforce the 8-char hex pattern, but legacy rows
  169. # or data sourced from AMS firmware / backups may carry malformed values.
  170. # A single bad row must not 500 the entire inventory list endpoint (#1055).
  171. rgba: str | None = None
  172. added_full: bool | None = None
  173. last_used: datetime | None = None
  174. encode_time: datetime | None = None
  175. tag_uid: str | None = None
  176. tray_uuid: str | None = None
  177. data_origin: str | None = None
  178. tag_type: str | None = None
  179. archived_at: datetime | None = None
  180. created_at: datetime
  181. updated_at: datetime
  182. k_profiles: list[SpoolKProfileResponse] = []
  183. class Config:
  184. from_attributes = True
  185. class SpoolAssignmentCreate(BaseModel):
  186. spool_id: int
  187. printer_id: int
  188. ams_id: int
  189. tray_id: int
  190. class SpoolAssignmentResponse(BaseModel):
  191. id: int
  192. spool_id: int
  193. printer_id: int
  194. printer_name: str | None = None
  195. ams_id: int
  196. tray_id: int
  197. fingerprint_color: str | None = None
  198. fingerprint_type: str | None = None
  199. created_at: datetime
  200. spool: SpoolResponse | None = None
  201. configured: bool = False
  202. pending_config: bool = False # True when slot was empty at assign time; will configure on insert
  203. ams_label: str | None = None # User-defined friendly name for the AMS unit
  204. class Config:
  205. from_attributes = True