Always return color as hex
This commit is contained in:
parent
b58aa5f2b1
commit
f7a3da7893
|
@ -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]
|
||||
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue