Database stores times in UTC but serialized without timezone info, causing frontend to misinterpret as local time. Now all datetime fields include 'Z' suffix to indicate UTC, enabling proper timezone conversion in the browser. - Add UTCDatetimeBaseModel base class for Pydantic schemas - Update model to_dict() methods to append 'Z' suffix - Affects: tasks, users, sessions, audit logs, translations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
"""
|
|
Tool_OCR - Base Schema with UTC Datetime Serialization
|
|
|
|
All datetime fields stored in the database are in UTC (using datetime.utcnow()).
|
|
This base class ensures proper serialization with 'Z' suffix to indicate UTC,
|
|
allowing frontend to correctly convert to local timezone.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
from typing import Any
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
|
|
def serialize_datetime_utc(dt: datetime) -> str:
|
|
"""Serialize datetime to ISO format with UTC indicator (Z suffix).
|
|
|
|
Database stores all times in UTC without timezone info (naive datetime).
|
|
This serializer adds the 'Z' suffix to indicate UTC, enabling proper
|
|
timezone conversion in the frontend.
|
|
"""
|
|
if dt is None:
|
|
return None
|
|
# Ensure ISO format with 'Z' suffix for UTC
|
|
return dt.isoformat() + 'Z'
|
|
|
|
|
|
class UTCDatetimeBaseModel(BaseModel):
|
|
"""Base model that serializes all datetime fields with UTC indicator.
|
|
|
|
Use this as base class for any response schema containing datetime fields
|
|
to ensure consistent timezone handling across the API.
|
|
"""
|
|
model_config = ConfigDict(
|
|
from_attributes=True,
|
|
json_encoders={
|
|
datetime: serialize_datetime_utc
|
|
}
|
|
)
|