Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pydantic v2 warnings related to UserGroup #572

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be convenient to reuse field validators but such discussion has never been resolved

Maybe the possible approaches to this task could be explored in future

Copy link
Collaborator Author

@JenySadadia JenySadadia Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the input. Created #579

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