from datetime import timedelta from django.conf import settings from django.core.management.base import BaseCommand from django.utils import timezone from apps.library.similarity import purge_similarity_checks class Command(BaseCommand): help = ( "Delete ephemeral similarity-check files. By default removes checks " "older than SIMILARITY_TTL_MINUTES; use --all to wipe every one." ) def add_arguments(self, parser): parser.add_argument( "--minutes", type=int, default=None, help="Delete checks older than N minutes " "(default: the configured TTL, currently " f"{getattr(settings, 'SIMILARITY_TTL_MINUTES', 30)}).", ) parser.add_argument( "--all", action="store_true", help="Delete every similarity check regardless of age.", ) def handle(self, *args, **options): if options["all"]: deleted = purge_similarity_checks() scope = "all" else: minutes = ( options["minutes"] if options["minutes"] is not None else getattr(settings, "SIMILARITY_TTL_MINUTES", 30) ) cutoff = timezone.now() - timedelta(minutes=minutes) deleted = purge_similarity_checks(cutoff) scope = f"older than {minutes} minute(s)" self.stdout.write( self.style.SUCCESS(f"Deleted {deleted} similarity check(s) ({scope}).") )