diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 126c5d0..1d06658 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -121,3 +121,17 @@ related objects:: obj = MyModel.objects.first() rel_history = LogEntry.objects.get_for_objects(obj.related.all()) full_history = (obj.history.all() | rel_history.all()).order_by('-timestamp') + +Management commands +------------------- + +.. versionadded:: 0.4.0 + +Auditlog provides the ``auditlogflush`` management command to clear all log entries from the database. + +The command asks for confirmation, it is not possible to execute the command without giving any form of (simulated) user +input. + +.. warning:: + + Using the ``auditlogflush`` command deletes all log entries permanently and irreversibly from the database. diff --git a/src/auditlog/management/__init__.py b/src/auditlog/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/auditlog/management/commands/__init__.py b/src/auditlog/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/auditlog/management/commands/auditlogflush.py b/src/auditlog/management/commands/auditlogflush.py new file mode 100644 index 0000000..e7b1f73 --- /dev/null +++ b/src/auditlog/management/commands/auditlogflush.py @@ -0,0 +1,20 @@ +from django.core.management.base import NoArgsCommand +from six import moves + +from auditlog.models import LogEntry + + +class Command(NoArgsCommand): + help = 'Deletes all log entries from the database.' + + def handle_noargs(self, **options): + answer = None + + while answer not in ['', 'y', 'n']: + answer = moves.input("Are you sure? [y/N]: ").lower().strip() + + if answer == 'y': + count = LogEntry.objects.all().count() + LogEntry.objects.all().delete() + + print("Deleted %d objects." % count) diff --git a/src/auditlog_tests/manage.py b/src/auditlog_tests/manage.py new file mode 100644 index 0000000..48371db --- /dev/null +++ b/src/auditlog_tests/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "auditlog_tests.test_settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv)