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

api.models: remove user model serializers #570

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
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
21 changes: 1 addition & 20 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
"""Server-side model definitions"""

from datetime import datetime
from typing import Optional, TypeVar, Dict, Any, List
from typing import Optional, TypeVar, List
from pydantic import (
BaseModel,
Field,
model_serializer,
field_validator,
)
from typing_extensions import Annotated
Expand All @@ -29,7 +28,7 @@
Document,
PydanticObjectId,
)
from kernelci.api.models_base import DatabaseModel, ModelId

Check failure on line 31 in api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Unable to import 'kernelci.api.models_base'


# PubSub model definitions
Expand Down Expand Up @@ -88,7 +87,7 @@
)

@field_validator('groups')
def validate_groups(cls, groups): # pylint: disable=no-self-argument

Check warning on line 90 in api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Method could be a function
"""Unique group constraint"""
unique_names = {group.name for group in groups}
if len(unique_names) != len(groups):
Expand All @@ -107,38 +106,20 @@
cls.Index('email', {'unique': True}),
]

@model_serializer(when_used='json')
def serialize_model(self) -> Dict[str, Any]:
"""Serialize model by converting PyObjectId to string"""
values = self.__dict__.copy()
for field_name, value in values.items():
if isinstance(value, PydanticObjectId):
values[field_name] = str(value)
return values


class UserRead(schemas.BaseUser[PydanticObjectId], ModelId):

Check failure on line 110 in api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Inheriting 'schemas.BaseUser[PydanticObjectId]', which is not a class.
"""Schema for reading a user"""
username: Annotated[str, Indexed(unique=True)]
groups: List[UserGroup] = Field(default=[])

@field_validator('groups')
def validate_groups(cls, groups): # pylint: disable=no-self-argument

Check warning on line 116 in api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Method could be a function
"""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

@model_serializer(when_used='json')
def serialize_model(self) -> Dict[str, Any]:
"""Serialize model by converting PyObjectId to string"""
values = self.__dict__.copy()
for field_name, value in values.items():
if isinstance(value, PydanticObjectId):
values[field_name] = str(value)
return values


class UserCreate(schemas.BaseUserCreate):
"""Schema for creating a user"""
Expand All @@ -146,7 +127,7 @@
groups: List[str] = Field(default=[])

@field_validator('groups')
def validate_groups(cls, groups): # pylint: disable=no-self-argument

Check warning on line 130 in api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Method could be a function
"""Unique group constraint"""
unique_names = set(groups)
if len(unique_names) != len(groups):
Expand All @@ -161,7 +142,7 @@
groups: List[str] = Field(default=[])

@field_validator('groups')
def validate_groups(cls, groups): # pylint: disable=no-self-argument

Check warning on line 145 in api/models.py

View workflow job for this annotation

GitHub Actions / Lint

Method could be a function
"""Unique group constraint"""
unique_names = set(groups)
if len(unique_names) != len(groups):
Expand Down
Loading