api_key.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_manage_projects: bool = True # Create/update/delete projects + membership (add archives) (#1893)
  16. can_access_cloud: bool = False # Read /cloud/* on the creator's behalf — default off (#1182)
  17. can_update_energy_cost: bool = False # POST /settings/electricity-price only (#1356)
  18. printer_ids: list[int] | None = None # null = all printers
  19. expires_at: datetime | None = None
  20. class APIKeyUpdate(BaseModel):
  21. """Schema for updating an API key."""
  22. name: str | None = None
  23. can_queue: bool | None = None
  24. can_control_printer: bool | None = None
  25. can_read_status: bool | None = None
  26. can_manage_library: bool | None = None
  27. can_manage_inventory: bool | None = None
  28. can_manage_maintenance: bool | None = None
  29. can_manage_archives: bool | None = None
  30. can_manage_projects: bool | None = None
  31. can_access_cloud: bool | None = None
  32. can_update_energy_cost: bool | None = None
  33. printer_ids: list[int] | None = None
  34. enabled: bool | None = None
  35. expires_at: datetime | None = None
  36. class APIKeyResponse(BaseModel):
  37. """Schema for API key response (without full key)."""
  38. id: int
  39. name: str
  40. key_prefix: str # First 8 chars for identification
  41. user_id: int | None # Owner — NULL on legacy keys created before per-user ownership (#1182)
  42. can_queue: bool
  43. can_control_printer: bool
  44. can_read_status: bool
  45. can_manage_library: bool
  46. can_manage_inventory: bool
  47. can_manage_maintenance: bool
  48. can_manage_archives: bool
  49. can_manage_projects: bool
  50. can_access_cloud: bool
  51. can_update_energy_cost: bool
  52. printer_ids: list[int] | None
  53. enabled: bool
  54. last_used: datetime | None
  55. created_at: datetime
  56. expires_at: datetime | None
  57. class Config:
  58. from_attributes = True
  59. class APIKeyCreateResponse(APIKeyResponse):
  60. """Response when creating a key - includes full key (shown only once)."""
  61. key: str # Full API key, only shown on creation