diff --git a/src/api/categories.py b/src/api/categories.py index 549c38d..d9717ec 100644 --- a/src/api/categories.py +++ b/src/api/categories.py @@ -3,7 +3,7 @@ from typing import Annotated, cast, final from beanie import Link, PydanticObjectId from fastapi import APIRouter, Body, HTTPException, Response, status -from pydantic import BaseModel, StringConstraints, field_validator +from pydantic import BaseModel, StringConstraints, field_serializer, field_validator from pydantic_extra_types.color import Color from src.api.auth.dependencies import LoggedInDep @@ -37,6 +37,11 @@ class _BaseCategoryData(BaseModel): raise ValueError("Alpha channel is not allowed in colors") return value + @field_serializer("color") + def serialize_color(self, value: Color) -> str: + """Serialize the color to a long hex string (#abcdef).""" + return value.as_hex(format="long") + @final class CategoryData(_BaseCategoryData): @@ -116,7 +121,7 @@ async def get_user_categories(user_id: PydanticObjectId, user: CurrentUserDep) - detail="You can only access your own categories", ) - categories = await Category.find(expr(Category.user).id == user.id).to_list() + categories = await Category.find(expr(Category.user).id == user.id, fetch_links=True).to_list() return [CategoryData.from_category(category) for category in categories] diff --git a/src/api/events.py b/src/api/events.py index 0815b68..e00eed3 100644 --- a/src/api/events.py +++ b/src/api/events.py @@ -4,7 +4,7 @@ from typing import Annotated, cast, final from beanie import DeleteRules, Link, PydanticObjectId from fastapi import APIRouter, Body, HTTPException, Query, status from fastapi.responses import Response -from pydantic import BaseModel, StringConstraints, field_validator +from pydantic import BaseModel, StringConstraints, field_serializer, field_validator from pydantic_extra_types.color import Color from src.api.auth.dependencies import LoggedInDep @@ -45,6 +45,11 @@ class _BaseEventData(BaseModel): raise ValueError("Alpha channel is not allowed in colors") return value + @field_serializer("color") + def serialize_color(self, value: Color) -> str: + """Serialize the color to a long hex string (#abcdef).""" + return value.as_hex(format="long") + @final class EventData(_BaseEventData):