From 41c664ed56ac90b298b037c593e5e38fe59c961b Mon Sep 17 00:00:00 2001 From: Erik Vroon Date: Sat, 17 Feb 2024 19:40:39 +0100 Subject: [PATCH] Fix dashboard link unique bug (#487) The frontend will send `"dashboard_link": ""` (empty string) but it should be converted to `None` instead. Otherwise tournaments quickly have non-unique endpoints. --- backend/bracket/models/db/tournament.py | 3 ++- backend/bracket/utils/pydantic.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 backend/bracket/utils/pydantic.py diff --git a/backend/bracket/models/db/tournament.py b/backend/bracket/models/db/tournament.py index 3f525474f..7fe61b88b 100644 --- a/backend/bracket/models/db/tournament.py +++ b/backend/bracket/models/db/tournament.py @@ -2,6 +2,7 @@ from pydantic import Field from bracket.models.db.shared import BaseModelORM +from bracket.utils.pydantic import EmptyStrToNone class Tournament(BaseModelORM): @@ -23,7 +24,7 @@ class TournamentUpdateBody(BaseModelORM): start_time: datetime_utc name: str dashboard_public: bool - dashboard_endpoint: str | None = None + dashboard_endpoint: EmptyStrToNone | str = None players_can_be_in_multiple_teams: bool auto_assign_courts: bool duration_minutes: int = Field(..., ge=1) diff --git a/backend/bracket/utils/pydantic.py b/backend/bracket/utils/pydantic.py new file mode 100644 index 000000000..2fcc20d1a --- /dev/null +++ b/backend/bracket/utils/pydantic.py @@ -0,0 +1,13 @@ +from typing import Annotated + +from pydantic import PlainValidator + + +def accept_none_and_empty_str(value: str | None) -> None: + if value is None or value == "": + return None + + raise ValueError("Not an empty str or None") + + +EmptyStrToNone = Annotated[None, PlainValidator(accept_none_and_empty_str)]