Always return color as hex

This commit is contained in:
Peter Vacho 2024-12-29 21:59:59 +01:00
parent b58aa5f2b1
commit f7a3da7893
Signed by: school
GPG key ID: 8CFC3837052871B4
2 changed files with 13 additions and 3 deletions

View file

@ -3,7 +3,7 @@ from typing import Annotated, cast, final
from beanie import Link, PydanticObjectId from beanie import Link, PydanticObjectId
from fastapi import APIRouter, Body, HTTPException, Response, status 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 pydantic_extra_types.color import Color
from src.api.auth.dependencies import LoggedInDep from src.api.auth.dependencies import LoggedInDep
@ -37,6 +37,11 @@ class _BaseCategoryData(BaseModel):
raise ValueError("Alpha channel is not allowed in colors") raise ValueError("Alpha channel is not allowed in colors")
return value 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 @final
class CategoryData(_BaseCategoryData): 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", 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] return [CategoryData.from_category(category) for category in categories]

View file

@ -4,7 +4,7 @@ from typing import Annotated, cast, final
from beanie import DeleteRules, Link, PydanticObjectId from beanie import DeleteRules, Link, PydanticObjectId
from fastapi import APIRouter, Body, HTTPException, Query, status from fastapi import APIRouter, Body, HTTPException, Query, status
from fastapi.responses import Response 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 pydantic_extra_types.color import Color
from src.api.auth.dependencies import LoggedInDep from src.api.auth.dependencies import LoggedInDep
@ -45,6 +45,11 @@ class _BaseEventData(BaseModel):
raise ValueError("Alpha channel is not allowed in colors") raise ValueError("Alpha channel is not allowed in colors")
return value 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 @final
class EventData(_BaseEventData): class EventData(_BaseEventData):