Management command improvements

Cherry-picking f4edfc0592
This commit is contained in:
Alieh Rymašeŭski 2020-11-12 16:42:22 +03:00
parent 6379683f77
commit de693dd092
2 changed files with 17 additions and 11 deletions

View file

@ -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 is None:
self.stdout.write("This action will clear all log entries from the database.")
response = input("Are you sure you want to continue? [y/N]: ").lower().strip()
answer = response == 'y'
if answer == 'y':
count = LogEntry.objects.all().count()
LogEntry.objects.all().delete()
print("Deleted %d objects." % count)
if answer:
count, _ = LogEntry.objects.all().delete()
self.stdout.write("Deleted %d objects." % count)
else:
self.stdout.write("Aborted.")

View file

@ -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::