Fix asyncpg datetime type error in create_asset and create_activity
asyncpg requires datetime objects, not ISO strings. Added _parse_timestamp helper to convert string timestamps to datetime objects. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
20
db.py
20
db.py
@@ -14,6 +14,22 @@ from uuid import UUID
|
|||||||
import asyncpg
|
import asyncpg
|
||||||
|
|
||||||
# Connection pool (initialized on startup)
|
# Connection pool (initialized on startup)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_timestamp(ts) -> datetime:
|
||||||
|
"""Parse a timestamp string or datetime to datetime object."""
|
||||||
|
if ts is None:
|
||||||
|
return datetime.now(timezone.utc)
|
||||||
|
if isinstance(ts, datetime):
|
||||||
|
return ts
|
||||||
|
# Parse ISO format string
|
||||||
|
if isinstance(ts, str):
|
||||||
|
if ts.endswith('Z'):
|
||||||
|
ts = ts[:-1] + '+00:00'
|
||||||
|
return datetime.fromisoformat(ts)
|
||||||
|
return datetime.now(timezone.utc)
|
||||||
|
|
||||||
|
|
||||||
_pool: Optional[asyncpg.Pool] = None
|
_pool: Optional[asyncpg.Pool] = None
|
||||||
|
|
||||||
# Configuration from environment
|
# Configuration from environment
|
||||||
@@ -246,7 +262,7 @@ async def create_asset(asset: dict) -> dict:
|
|||||||
asset.get("description"),
|
asset.get("description"),
|
||||||
json.dumps(asset.get("origin")) if asset.get("origin") else None,
|
json.dumps(asset.get("origin")) if asset.get("origin") else None,
|
||||||
asset["owner"],
|
asset["owner"],
|
||||||
asset.get("created_at") or datetime.now(timezone.utc).isoformat()
|
_parse_timestamp(asset.get("created_at"))
|
||||||
)
|
)
|
||||||
return _parse_asset_row(row)
|
return _parse_asset_row(row)
|
||||||
|
|
||||||
@@ -376,7 +392,7 @@ async def create_activity(activity: dict) -> dict:
|
|||||||
activity["activity_type"],
|
activity["activity_type"],
|
||||||
activity["actor_id"],
|
activity["actor_id"],
|
||||||
json.dumps(activity["object_data"]),
|
json.dumps(activity["object_data"]),
|
||||||
activity["published"],
|
_parse_timestamp(activity["published"]),
|
||||||
json.dumps(activity.get("signature")) if activity.get("signature") else None
|
json.dumps(activity.get("signature")) if activity.get("signature") else None
|
||||||
)
|
)
|
||||||
return _parse_activity_row(row)
|
return _parse_activity_row(row)
|
||||||
|
|||||||
Reference in New Issue
Block a user