Fix local_datetime filter to handle string datetime values

The filter now accepts both datetime objects and string representations.
This fixes AttributeError when datetime fields are stored/returned as
strings instead of datetime objects from the database.

Changes:
- Added string parsing logic for common datetime formats
- Tries multiple format patterns (ISO, standard, with/without microseconds)
- Returns original string if parsing fails (graceful degradation)
- Maintains full datetime object support

Fixes errors on:
- /customers (autotask_last_sync_at)
- /logging (created_at)
- /admin/all-mail (received_at, parsed_at)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Ivo Oskamp 2026-02-07 00:16:44 +01:00
parent 1cf3564751
commit b27e8db602

View File

@ -68,7 +68,7 @@ def create_app():
"""Convert UTC datetime to UI timezone and format as string.
Args:
utc_dt: datetime object in UTC (or None)
utc_dt: datetime object in UTC, string representation, or None
format: strftime format string (default: '%Y-%m-%d %H:%M:%S')
Returns:
@ -79,6 +79,30 @@ def create_app():
from datetime import datetime
# If input is already a string, try to parse it first
if isinstance(utc_dt, str):
utc_dt = utc_dt.strip()
if not utc_dt:
return ""
try:
# Try common datetime formats
for fmt in [
"%Y-%m-%d %H:%M:%S.%f", # With microseconds
"%Y-%m-%d %H:%M:%S", # Without microseconds
"%Y-%m-%dT%H:%M:%S.%fZ", # ISO format with Z
"%Y-%m-%dT%H:%M:%S", # ISO format without Z
]:
try:
utc_dt = datetime.strptime(utc_dt, fmt)
break
except ValueError:
continue
else:
# If parsing failed, return original string
return str(utc_dt)
except Exception:
return str(utc_dt)
try:
from zoneinfo import ZoneInfo
except ImportError: