Files
rf4-spotter/apps/api/app/source_lifecycle.py
T
ik bc3ceb5dfe
CI / backend-and-migrations (push) Canceled after 0s
CI / astro-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
CI / compose-e2e (push) Canceled after 0s
feat: manage external source lifecycle
2026-09-13 16:26:34 +07:00

39 lines
1.3 KiB
Python

from __future__ import annotations
from datetime import datetime, timezone
from typing import Literal
from sqlalchemy.orm import Session
from .models import ExternalObservation, ModerationStatus
SourceCheckStatus = Literal["available", "missing", "temporary_error", "blocked"]
def record_source_check(
session: Session,
observation: ExternalObservation,
status: SourceCheckStatus,
*,
checked_at: datetime | None = None,
) -> ExternalObservation:
"""Persist a check performed during an already scheduled source request.
Only an authoritative 404/410-style ``missing`` result withdraws published
data. Transient errors and access blocks remain diagnostic and never remove
an observation from activity.
"""
current = checked_at or datetime.now(timezone.utc)
observation.source_check_status = status
observation.source_checked_at = current
if status == "missing" and observation.status != "withdrawn":
if observation.catch_report is not None:
observation.catch_report.moderation_status = ModerationStatus.pending
observation.status = "withdrawn"
observation.review_note = "Source record missing; withdrawn pending moderator review"
observation.reviewed_at = current
observation.moderation_version += 1
session.commit()
return observation