api_key.py 2.9 KB

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