Spaces:
Sleeping
Sleeping
File size: 660 Bytes
1cff1e5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | from bson import ObjectId
from datetime import datetime, timezone
import uuid
def generate_id() -> str:
"""Generate a unique string ID."""
return str(uuid.uuid4())
def utc_now() -> str:
"""Get current UTC timestamp as ISO string."""
return datetime.now(timezone.utc).isoformat()
def str_objectid(doc: dict) -> dict:
"""Convert MongoDB ObjectId to string in a document."""
if doc and "_id" in doc:
doc["id"] = str(doc["_id"])
del doc["_id"]
return doc
def str_objectids(docs: list) -> list:
"""Convert MongoDB ObjectIds to strings in a list of documents."""
return [str_objectid(doc) for doc in docs]
|