42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
# app/schemas/group.py
|
|
from pydantic import BaseModel, ConfigDict, computed_field
|
|
from datetime import datetime
|
|
from typing import Optional, List
|
|
|
|
from .user import UserPublic # Import UserPublic to represent members
|
|
|
|
# Properties to receive via API on creation
|
|
class GroupCreate(BaseModel):
|
|
name: str
|
|
|
|
# Properties to return to client
|
|
class GroupPublic(BaseModel):
|
|
id: int
|
|
name: str
|
|
created_by_id: int
|
|
created_at: datetime
|
|
member_associations: Optional[List["UserGroupPublic"]] = None
|
|
|
|
@computed_field
|
|
@property
|
|
def members(self) -> Optional[List[UserPublic]]:
|
|
if not self.member_associations:
|
|
return None
|
|
return [assoc.user for assoc in self.member_associations if assoc.user]
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
# Properties for UserGroup association
|
|
class UserGroupPublic(BaseModel):
|
|
id: int
|
|
user_id: int
|
|
group_id: int
|
|
role: str
|
|
joined_at: datetime
|
|
user: Optional[UserPublic] = None
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
# Properties stored in DB (if needed, often GroupPublic is sufficient)
|
|
# class GroupInDB(GroupPublic):
|
|
# pass |