mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-02-28 01:19:36 -06:00
34 lines
1.2 KiB
Python
34 lines
1.2 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."
|
|
|
|
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",
|
|
)
|