fix: change verbosity of api key purge mgmt command (#6758)
* fix: change verbosity of api key purge mgmt command * test: adjust mgmt command test arguments
This commit is contained in:
parent
cfaf84f08f
commit
4216c129fe
|
@ -18,9 +18,11 @@ class Command(BaseCommand):
|
|||
parser.add_argument('-n', '--dry-run', action='store_true', default=False,
|
||||
help="Don't delete events, just show what would be done")
|
||||
|
||||
|
||||
def handle(self, *args, **options):
|
||||
keep_days = options['keep_days']
|
||||
dry_run = options['dry_run']
|
||||
verbosity = options.get("verbosity", 1)
|
||||
|
||||
def _format_count(count, unit='day'):
|
||||
return '{} {}{}'.format(count, unit, ('' if count == 1 else 's'))
|
||||
|
@ -28,10 +30,11 @@ class Command(BaseCommand):
|
|||
if keep_days < 0:
|
||||
raise CommandError('Negative keep_days not allowed ({} was specified)'.format(keep_days))
|
||||
|
||||
self.stdout.write('purge_old_personal_api_key_events: Finding events older than {}\n'.format(_format_count(keep_days)))
|
||||
if dry_run:
|
||||
self.stdout.write('Dry run requested, records will not be deleted\n')
|
||||
self.stdout.flush()
|
||||
if verbosity > 1:
|
||||
self.stdout.write('purge_old_personal_api_key_events: Finding events older than {}\n'.format(_format_count(keep_days)))
|
||||
if dry_run:
|
||||
self.stdout.write('Dry run requested, records will not be deleted\n')
|
||||
self.stdout.flush()
|
||||
|
||||
now = timezone.now()
|
||||
old_events = PersonApiKeyEvent.objects.filter(
|
||||
|
@ -41,7 +44,8 @@ class Command(BaseCommand):
|
|||
stats = old_events.aggregate(Min('time'), Max('time'))
|
||||
old_count = old_events.count()
|
||||
if old_count == 0:
|
||||
self.stdout.write('No events older than {} found\n'.format(_format_count(keep_days)))
|
||||
if verbosity > 1:
|
||||
self.stdout.write('No events older than {} found\n'.format(_format_count(keep_days)))
|
||||
return
|
||||
|
||||
oldest_date = stats['time__min']
|
||||
|
@ -50,10 +54,11 @@ class Command(BaseCommand):
|
|||
newest_ago = now - newest_date
|
||||
|
||||
action_fmt = 'Would delete {}\n' if dry_run else 'Deleting {}\n'
|
||||
self.stdout.write(action_fmt.format(_format_count(old_count, 'event')))
|
||||
self.stdout.write(' Oldest at {} ({} ago)\n'.format(oldest_date, _format_count(oldest_ago.days)))
|
||||
self.stdout.write(' Most recent at {} ({} ago)\n'.format(newest_date, _format_count(newest_ago.days)))
|
||||
self.stdout.flush()
|
||||
if verbosity > 1:
|
||||
self.stdout.write(action_fmt.format(_format_count(old_count, 'event')))
|
||||
self.stdout.write(' Oldest at {} ({} ago)\n'.format(oldest_date, _format_count(oldest_ago.days)))
|
||||
self.stdout.write(' Most recent at {} ({} ago)\n'.format(newest_date, _format_count(newest_ago.days)))
|
||||
self.stdout.flush()
|
||||
|
||||
if not dry_run:
|
||||
old_events.delete()
|
||||
|
|
|
@ -76,26 +76,26 @@ class CommandTests(TestCase):
|
|||
num_recent_events = len(recent_events)
|
||||
|
||||
# call with dry run
|
||||
output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '--dry-run')
|
||||
output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '--dry-run', '-v2')
|
||||
self._assert_purge_dry_run_results(output, num_old_events, old_events + recent_events)
|
||||
|
||||
# call for real
|
||||
output = self._call_command('purge_old_personal_api_key_events', str(keep_days))
|
||||
output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '-v2')
|
||||
self._assert_purge_results(output, num_old_events, recent_events)
|
||||
self.assertEqual(PersonEvent.objects.count(), personevents_before + num_recent_events,
|
||||
'PersonEvents were not cleaned up properly')
|
||||
|
||||
# repeat - there should be nothing left to delete
|
||||
output = self._call_command('purge_old_personal_api_key_events', '--dry-run', str(keep_days))
|
||||
output = self._call_command('purge_old_personal_api_key_events', '--dry-run', str(keep_days), '-v2')
|
||||
self._assert_purge_dry_run_results(output, 0, recent_events)
|
||||
|
||||
output = self._call_command('purge_old_personal_api_key_events', str(keep_days))
|
||||
output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '-v2')
|
||||
self._assert_purge_results(output, 0, recent_events)
|
||||
self.assertEqual(PersonEvent.objects.count(), personevents_before + num_recent_events,
|
||||
'PersonEvents were not cleaned up properly')
|
||||
|
||||
# and now delete the remaining events
|
||||
output = self._call_command('purge_old_personal_api_key_events', '0')
|
||||
output = self._call_command('purge_old_personal_api_key_events', '0', '-v2')
|
||||
self._assert_purge_results(output, num_recent_events, [])
|
||||
self.assertEqual(PersonEvent.objects.count(), personevents_before,
|
||||
'PersonEvents were not cleaned up properly')
|
||||
|
|
Loading…
Reference in a new issue