diff --git a/auditlog/management/commands/auditlogflush.py b/auditlog/management/commands/auditlogflush.py index 155ba96..00e7647 100644 --- a/auditlog/management/commands/auditlogflush.py +++ b/auditlog/management/commands/auditlogflush.py @@ -4,16 +4,22 @@ from auditlog.models import LogEntry class Command(BaseCommand): - help = 'Deletes all log entries from the database.' + help = "Deletes all log entries from the database." + + def add_arguments(self, parser): + parser.add_argument('-y, --yes', action='store_true', default=None, + help="Continue without asking confirmation.", dest='yes') def handle(self, *args, **options): - answer = None + answer = options['yes'] - while answer not in ['', 'y', 'n']: - answer = input("Are you sure? [y/N]: ").lower().strip() - - if answer == 'y': - count = LogEntry.objects.all().count() - LogEntry.objects.all().delete() + while answer is None: + print("This action will clear all log entries from the database.") + response = input("Are you sure you want to continue? [y/N]: ").lower().strip() + answer = True if response == 'y' else False if response == 'n' else None + if answer: + count, _ = LogEntry.objects.all().delete() print("Deleted %d objects." % count) + else: + print("Aborted.") diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 22bb6f6..8aa15f0 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -191,8 +191,8 @@ Management commands 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. +By default, the command asks for confirmation. It is possible to run the command with the `-y` or `--yes` flag to skip +confirmation and immediately delete all entries. .. warning::