from __future__ import annotations from datetime import timedelta from django.db.models import Q from django.utils import timezone from apps.jobs.models import JobPosting def update_lifecycle( *, uncertain_after_days: int = 3, removed_after_days: int = 14 ) -> dict[str, int]: now = timezone.now() expired = JobPosting.objects.filter( status__in=[JobPosting.Status.ACTIVE, JobPosting.Status.UNCERTAIN], valid_through__lt=now, ).update(status=JobPosting.Status.EXPIRED) uncertain_cutoff = now - timedelta(days=uncertain_after_days) uncertain = JobPosting.objects.filter( status=JobPosting.Status.ACTIVE, last_seen__lt=uncertain_cutoff, valid_through__isnull=True, ).update(status=JobPosting.Status.UNCERTAIN) removed_cutoff = now - timedelta(days=removed_after_days) removed = JobPosting.objects.filter( Q(status=JobPosting.Status.UNCERTAIN), last_seen__lt=removed_cutoff, ).update(status=JobPosting.Status.REMOVED) return {"expired": expired, "uncertain": uncertain, "removed": removed}