refactors
This commit is contained in:
parent
f46d2ae8b3
commit
168b67acee
12 changed files with 471 additions and 126 deletions
1
backend/utils/__init__.py
Normal file
1
backend/utils/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
"""Utility modules for common functionality."""
|
||||
27
backend/utils/date_queries.py
Normal file
27
backend/utils/date_queries.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
"""Utilities for date/time query operations."""
|
||||
|
||||
from datetime import UTC, date, datetime, time
|
||||
|
||||
|
||||
def date_to_start_datetime(d: date) -> datetime:
|
||||
"""Convert a date to datetime at start of day (00:00:00) in UTC."""
|
||||
return datetime.combine(d, time.min, tzinfo=UTC)
|
||||
|
||||
|
||||
def date_to_end_datetime(d: date) -> datetime:
|
||||
"""Convert a date to datetime at end of day (23:59:59.999999) in UTC."""
|
||||
return datetime.combine(d, time.max, tzinfo=UTC)
|
||||
|
||||
|
||||
def date_range_to_datetime_range(
|
||||
start_date: date, end_date: date
|
||||
) -> tuple[datetime, datetime]:
|
||||
"""
|
||||
Convert a date range to datetime range.
|
||||
|
||||
Returns:
|
||||
Tuple of (start_datetime, end_datetime) where:
|
||||
- start_datetime is start_date at 00:00:00 UTC
|
||||
- end_datetime is end_date at 23:59:59.999999 UTC
|
||||
"""
|
||||
return date_to_start_datetime(start_date), date_to_end_datetime(end_date)
|
||||
32
backend/utils/enum_validation.py
Normal file
32
backend/utils/enum_validation.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
"""Utilities for validating enum values from strings."""
|
||||
|
||||
from enum import Enum
|
||||
from typing import TypeVar
|
||||
|
||||
from exceptions import BadRequestError
|
||||
|
||||
T = TypeVar("T", bound=Enum)
|
||||
|
||||
|
||||
def validate_enum(enum_class: type[T], value: str, field_name: str = "value") -> T:
|
||||
"""
|
||||
Validate and convert string to enum.
|
||||
|
||||
Args:
|
||||
enum_class: The enum class to validate against
|
||||
value: The string value to validate
|
||||
field_name: Name of the field for error messages
|
||||
|
||||
Returns:
|
||||
The validated enum value
|
||||
|
||||
Raises:
|
||||
BadRequestError: If the value is not a valid enum member
|
||||
"""
|
||||
try:
|
||||
return enum_class(value)
|
||||
except ValueError:
|
||||
valid_values = ", ".join(e.value for e in enum_class)
|
||||
raise BadRequestError(
|
||||
f"Invalid {field_name}: {value}. Must be one of: {valid_values}"
|
||||
) from None
|
||||
Loading…
Add table
Add a link
Reference in a new issue