--
This commit is contained in:
+14
-3
@@ -2,7 +2,7 @@ import uuid
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr, Field, model_validator
|
||||
|
||||
from app.common.enums import Role
|
||||
from app.common.enums import Role, FIELD_ROLES
|
||||
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
@@ -14,12 +14,16 @@ class UserCreate(BaseModel):
|
||||
phone: str | None = Field(default=None, max_length=20)
|
||||
#: Required for MR, forbidden for every other role (R4/R8).
|
||||
headquarters_id: uuid.UUID | None = None
|
||||
#: SFE only: the other districts they cover besides `headquarters_id`.
|
||||
extra_headquarter_ids: list[uuid.UUID] = []
|
||||
|
||||
@model_validator(mode="after")
|
||||
def check_hq_matches_role(self):
|
||||
if self.role == Role.MR and self.headquarters_id is None:
|
||||
if self.extra_headquarter_ids and self.role != Role.SFE:
|
||||
raise ValueError("extra_headquarter_ids may only be set for an SFE")
|
||||
if self.role in FIELD_ROLES and self.headquarters_id is None:
|
||||
raise ValueError("headquarters_id is required for an MR")
|
||||
if self.role != Role.MR and self.headquarters_id is not None:
|
||||
if self.role not in FIELD_ROLES and self.headquarters_id is not None:
|
||||
raise ValueError("headquarters_id may only be set for an MR")
|
||||
return self
|
||||
|
||||
@@ -32,7 +36,12 @@ class UserUpdate(BaseModel):
|
||||
#: A new password. Signs the user out everywhere and clears a lockout.
|
||||
password: str | None = Field(default=None, min_length=8, max_length=128)
|
||||
phone: str | None = Field(default=None, max_length=20)
|
||||
#: Admin-only. Moving to MR needs `headquarters_id`; leaving MR or AM ends that role's
|
||||
#: assignments, and the user is signed out so they sign in again with the new role.
|
||||
role: Role | None = None
|
||||
headquarters_id: uuid.UUID | None = None
|
||||
#: SFE only. The full list of extra districts; omit to leave it unchanged.
|
||||
extra_headquarter_ids: list[uuid.UUID] | None = None
|
||||
is_active: bool | None = None
|
||||
|
||||
|
||||
@@ -47,6 +56,8 @@ class UserRead(BaseModel):
|
||||
phone: str | None
|
||||
headquarters_id: uuid.UUID | None
|
||||
is_active: bool
|
||||
is_superadmin: bool = False
|
||||
extra_headquarter_ids: list[uuid.UUID] = []
|
||||
|
||||
|
||||
class MyProfile(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user