Skip to content

Commit

Permalink
api.models: define different user schema models
Browse files Browse the repository at this point in the history
Define and use different user models for API
endpoint and fastapi-users router, i.e. different
models as request schema for API request model and schema
for request to `fastapi-users` router.
For create user request, user needs to send
`UserCreateRequest` schema as API router request
and the router will use `UserCreate` schema to
send the request to `fastapi-users` router.
This is due to we intend to have a list of user group
name strings as API router request and `UserGroup`
instance as `fastapi-users` router request as `user.groups`
field value.

Signed-off-by: Jeny Sadadia <[email protected]>
  • Loading branch information
Jeny Sadadia committed Nov 21, 2024
1 parent b6d612b commit 577a83e
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])

Expand All @@ -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=[])
Expand All @@ -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):
Expand Down

0 comments on commit 577a83e

Please sign in to comment.