api_key.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from datetime import datetime
  2. from pydantic import BaseModel
  3. class APIKeyCreate(BaseModel):
  4. """Schema for creating a new API key."""
  5. name: str
  6. can_queue: bool = True
  7. can_control_printer: bool = False
  8. can_read_status: bool = True
  9. can_manage_library: bool = True # Upload / rename / delete own library files + MakerWorld import
  10. can_manage_inventory: bool = True # Inventory writes — SpoolBuddy NFC/scale/system, manual stock edits via API
  11. can_manage_maintenance: bool = (
  12. True # Log/reset maintenance items, edit intervals, manage type catalog (#1832 follow-up)
  13. )
  14. can_manage_archives: bool = True # Create/update/delete print archives — not purge (#1888)
  15. can_access_cloud: bool = False # Read /cloud/* on the creator's behalf — default off (#1182)
  16. can_update_energy_cost: bool = False # POST /settings/electricity-price only (#1356)
  17. printer_ids: list[int] | None = None # null = all printers
  18. expires_at: datetime | None = None
  19. class APIKeyUpdate(BaseModel):
  20. """Schema for updating an API key."""
  21. name: str | None = None
  22. can_queue: bool | None = None
  23. can_control_printer: bool | None = None
  24. can_read_status: bool | None = None
  25. can_manage_library: bool | None = None
  26. can_manage_inventory: bool | None = None
  27. can_manage_maintenance: bool | None = None
  28. can_manage_archives: bool | None = None
  29. can_access_cloud: bool | None = None
  30. can_update_energy_cost: bool | None = None
  31. printer_ids: list[int] | None = None
  32. enabled: bool | None = None
  33. expires_at: datetime | None = None
  34. class APIKeyResponse(BaseModel):
  35. """Schema for API key response (without full key)."""
  36. id: int
  37. name: str
  38. key_prefix: str # First 8 chars for identification
  39. user_id: int | None # Owner — NULL on legacy keys created before per-user ownership (#1182)
  40. can_queue: bool
  41. can_control_printer: bool
  42. can_read_status: bool
  43. can_manage_library: bool
  44. can_manage_inventory: bool
  45. can_manage_maintenance: bool
  46. can_manage_archives: bool
  47. can_access_cloud: bool
  48. can_update_energy_cost: bool
  49. printer_ids: list[int] | None
  50. enabled: bool
  51. last_used: datetime | None
  52. created_at: datetime
  53. expires_at: datetime | None
  54. class Config:
  55. from_attributes = True
  56. class APIKeyCreateResponse(APIKeyResponse):
  57. """Response when creating a key - includes full key (shown only once)."""
  58. key: str # Full API key, only shown on creation