diff --git a/api/models.py b/api/models.py index 07c57eb2..9f5b063d 100644 --- a/api/models.py +++ b/api/models.py @@ -121,8 +121,8 @@ def validate_groups(cls, groups): # pylint: disable=no-self-argument return groups -class UserCreate(schemas.BaseUserCreate): - """Schema for creating a user""" +class UserCreateRequest(schemas.BaseUserCreate): + """Create user request schema for API router""" username: Annotated[str, Indexed(unique=True)] groups: List[str] = Field(default=[]) @@ -135,8 +135,22 @@ def validate_groups(cls, groups): # pylint: disable=no-self-argument return groups -class UserUpdate(schemas.BaseUserUpdate): - """Schema for updating a user""" +class UserCreate(schemas.BaseUserCreate): + """Schema used for sending create user request to 'fastapi-users' router""" + username: Annotated[str, Indexed(unique=True)] + groups: List[UserGroup] = Field(default=[]) + + @field_validator('groups') + def validate_groups(cls, groups): # pylint: disable=no-self-argument + """Unique group constraint""" + unique_names = {group.name for group in groups} + if len(unique_names) != len(groups): + raise ValueError("Groups must have unique names.") + return groups + + +class UserUpdateRequest(schemas.BaseUserUpdate): + """Update user request schema for API router""" username: Annotated[Optional[str], Indexed(unique=True), Field(default=None)] groups: List[str] = Field(default=[]) @@ -150,6 +164,21 @@ def validate_groups(cls, groups): # pylint: disable=no-self-argument return groups +class UserUpdate(schemas.BaseUserUpdate): + """Schema used for sending update user request to 'fastapi-users' router""" + username: Annotated[Optional[str], Indexed(unique=True), + Field(default=None)] + groups: List[UserGroup] = Field(default=[]) + + @field_validator('groups') + def validate_groups(cls, groups): # pylint: disable=no-self-argument + """Unique group constraint""" + unique_names = {group.name for group in groups} + if len(unique_names) != len(groups): + raise ValueError("Groups must have unique names.") + return groups + + # Pagination models class CustomLimitOffsetParams(LimitOffsetParams):