fix: resolve TypeError in disclosure revoke due to naive/aware datetime comparison

PyMySQL returns naive datetimes from MySQL DATETIME columns, but
revoke_disclosure() compared them with timezone-aware datetime.now(timezone.utc),
causing an unhandled TypeError (not caught by except ValueError) and a 500 error.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
CCS Admin 2026-02-27 07:57:59 +00:00
parent 95e84a6978
commit 1bedbcf243

View file

@ -113,6 +113,11 @@ def review_disclosure_request(
return dr
def _utcnow_naive() -> datetime:
"""Return current UTC time as a naive datetime, matching MySQL DATETIME columns."""
return datetime.now(timezone.utc).replace(tzinfo=None)
def revoke_disclosure(db: Session, request_id: int, user_id: int, *, admin: bool = False) -> DisclosureRequest:
"""Revoke an active disclosure by setting expires_at to now.
@ -124,12 +129,12 @@ def revoke_disclosure(db: Session, request_id: int, user_id: int, *, admin: bool
raise ValueError("Disclosure request not found")
if dr.status != "approved":
raise ValueError("Only approved disclosures can be revoked")
if dr.expires_at and dr.expires_at <= datetime.now(timezone.utc):
if dr.expires_at and dr.expires_at <= _utcnow_naive():
raise ValueError("Disclosure already expired")
if not admin and dr.requester_id != user_id:
raise ValueError("Not authorized to revoke this disclosure")
dr.expires_at = datetime.now(timezone.utc)
dr.expires_at = _utcnow_naive()
db.commit()
db.refresh(dr)
return dr