Files
nimbusflow/backend/models/enums.py

42 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ------------------------------------------------------------
# Centralised enumeration definitions for the datamodel layer.
# Keeping them in one module avoids circular imports and makes
# typechecking / IDE completion straightforward.
# ------------------------------------------------------------
from __future__ import annotations
from enum import StrEnum
from typing import Any, Iterable
class ScheduleStatus(StrEnum):
"""
Canonical status values for a ``Schedule`` row.
Using ``StrEnum`` means the enum members behave like regular strings
(e.g. they can be written directly to SQLite) while still giving us
the safety and autocomplete of an enum.
"""
PENDING = "pending"
ACCEPTED = "accepted"
DECLINED = "declined"
@classmethod
def from_raw(cls, value: Any) -> "ScheduleStatus":
"""
Convert an arbitrary value (often a plain string coming from the DB)
into a ``ScheduleStatus`` member.
Raises
------
ValueError
If the value does not correspond to any defined member.
"""
if isinstance(value, cls):
return value
try:
# ``cls(value)`` works because ``StrEnum`` subclasses ``str``.
return cls(value)
except ValueError as exc:
raise ValueError(f"Invalid ScheduleStatus: {value!r}") from exc