Files
paperless-ngx/src/documents/management/commands/prune_audit_logs.py
T
Trenton HandClaude Sonnet 4.6 e30676f889 Feature: Migrate import/export to rich progress (#12260)
* Refactor: migrate exporter/importer from tqdm to PaperlessCommand.track()

Replace direct tqdm usage in document_exporter and document_importer with
the PaperlessCommand base class and its track() method, which is backed by
Rich and handles --no-progress-bar automatically. Also removes the unused
ProgressBarMixin from mixins.py.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Refactor: add explicit supports_progress_bar and supports_multiprocessing to all PaperlessCommand subclasses

Each management command now explicitly declares both class attributes
rather than relying on defaults, making intent unambiguous at a glance.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-09 08:59:17 -07:00

37 lines
1.3 KiB
Python

from auditlog.models import LogEntry
from django.db import transaction
from documents.management.commands.base import PaperlessCommand
class Command(PaperlessCommand):
"""Prune the audit logs of objects that no longer exist."""
help = "Prunes the audit logs of objects that no longer exist."
supports_progress_bar = True
supports_multiprocessing = False
def handle(self, *args, **options):
with transaction.atomic():
for log_entry in self.track(
LogEntry.objects.all(),
description="Pruning audit logs...",
):
model_class = log_entry.content_type.model_class()
objects = (
model_class.global_objects
if hasattr(model_class, "global_objects")
else model_class.objects
)
if (
log_entry.object_id
and not objects.filter(pk=log_entry.object_id).exists()
):
log_entry.delete()
self.console.print(
f"Deleted audit log entry for "
f"{model_class.__name__} #{log_entry.object_id}",
style="yellow",
)