From dff1ec921d17fd3deaeb6bde36a6cf4d459c7c97 Mon Sep 17 00:00:00 2001 From: bonamin Date: Tue, 17 Feb 2026 17:00:30 +0200 Subject: [PATCH] Fix more vars, and re-worked Timestamp problems --- backend/devices/service.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/backend/devices/service.py b/backend/devices/service.py index 5b12203..8289d56 100644 --- a/backend/devices/service.py +++ b/backend/devices/service.py @@ -1,7 +1,8 @@ import secrets import string -from google.cloud.firestore_v1 import GeoPoint +from datetime import datetime +from google.cloud.firestore_v1 import GeoPoint, DocumentReference from shared.firebase import get_db from shared.exceptions import NotFoundError @@ -39,14 +40,15 @@ def _ensure_unique_serial(db) -> str: def _convert_firestore_value(val): - """Convert Firestore-specific types (Timestamp, GeoPoint) to strings.""" - if hasattr(val, "seconds") and hasattr(val, "nanos"): - # Firestore Timestamp → ISO string - from datetime import datetime, timezone - dt = datetime.fromtimestamp(val.seconds + val.nanos / 1e9, tz=timezone.utc) - return dt.strftime("%d %B %Y at %H:%M:%S UTC") + """Convert Firestore-specific types (Timestamp, GeoPoint, DocumentReference) to strings.""" + if isinstance(val, datetime): + # Firestore DatetimeWithNanoseconds is a datetime subclass + return val.strftime("%d %B %Y at %H:%M:%S UTC%z") if isinstance(val, GeoPoint): return f"{val.latitude}° N, {val.longitude}° E" + if isinstance(val, DocumentReference): + # Store the document path (e.g. "users/abc123") + return val.path return val