group.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """Pydantic schemas for Group CRUD operations."""
  2. from datetime import datetime
  3. from typing import Annotated, Literal
  4. from pydantic import BaseModel, StringConstraints
  5. from backend.app.schemas.not_null import IgnoresNullForRequiredColumns
  6. class GroupBrief(BaseModel):
  7. """Brief group info for embedding in other responses."""
  8. id: int
  9. name: str
  10. class Config:
  11. from_attributes = True
  12. class FilamentRuleSchema(BaseModel):
  13. """One filament rule of a group (#3522): a material type or a slicer filament id."""
  14. match: Literal["material", "filament_id"]
  15. # Stripped before the length check, so a blank value is a 422 rather than
  16. # a rule `clean_rules` would silently drop.
  17. value: Annotated[str, StringConstraints(strip_whitespace=True, min_length=1, max_length=64)]
  18. class GroupCreate(BaseModel):
  19. """Schema for creating a new group."""
  20. name: str
  21. description: str | None = None
  22. permissions: list[str] = []
  23. # Printer scope (#1727): when set, members only see these printers plus
  24. # every printer in these locations
  25. restrict_printers: bool = False
  26. printer_ids: list[int] = []
  27. locations: list[str] = []
  28. # Filament scope (#3522): when set, members may only print with the
  29. # filaments these rules allow. In "block" mode that is every filament no
  30. # rule names; in "allow" mode, only those a rule names.
  31. restrict_filaments: bool = False
  32. filament_rule_mode: Literal["allow", "block"] = "block"
  33. filament_rules: list[FilamentRuleSchema] = []
  34. class GroupUpdate(IgnoresNullForRequiredColumns):
  35. """Schema for updating a group."""
  36. __orm_table__ = "groups"
  37. name: str | None = None
  38. description: str | None = None
  39. permissions: list[str] | None = None
  40. restrict_printers: bool | None = None
  41. printer_ids: list[int] | None = None
  42. locations: list[str] | None = None
  43. restrict_filaments: bool | None = None
  44. filament_rule_mode: Literal["allow", "block"] | None = None
  45. filament_rules: list[FilamentRuleSchema] | None = None
  46. class GroupResponse(BaseModel):
  47. """Schema for group response."""
  48. id: int
  49. name: str
  50. description: str | None
  51. permissions: list[str]
  52. is_system: bool
  53. restrict_printers: bool = False
  54. printer_ids: list[int] = []
  55. locations: list[str] = []
  56. restrict_filaments: bool = False
  57. filament_rule_mode: Literal["allow", "block"] = "block"
  58. filament_rules: list[FilamentRuleSchema] = []
  59. user_count: int = 0
  60. created_at: datetime
  61. updated_at: datetime
  62. class Config:
  63. from_attributes = True
  64. class GroupDetailResponse(GroupResponse):
  65. """Schema for detailed group response including users."""
  66. users: list["UserBrief"] = []
  67. class UserBrief(BaseModel):
  68. """Brief user info for embedding in group response."""
  69. id: int
  70. username: str
  71. is_active: bool
  72. class Config:
  73. from_attributes = True
  74. class PermissionInfo(BaseModel):
  75. """Schema for permission information."""
  76. value: str
  77. label: str
  78. class PermissionCategory(BaseModel):
  79. """Schema for a category of permissions."""
  80. name: str
  81. permissions: list[PermissionInfo]
  82. class PermissionsListResponse(BaseModel):
  83. """Schema for listing all permissions by category."""
  84. categories: list[PermissionCategory]
  85. all_permissions: list[str]
  86. # Update forward references
  87. GroupDetailResponse.model_rebuild()