ctl: make wipe_data simulate its action by default (#29929) #1144

Merged
fpeters merged 1 commits from wip/29929-wipe-data-dry-run into main 2024-02-23 19:03:41 +01:00
2 changed files with 34 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import io
import json
import os
import pickle
@ -124,25 +125,42 @@ def test_wipe_formdata(pub):
with pytest.raises(CommandError):
call_command('wipe_data', '--all-tenants')
# dry-run mode
output = io.StringIO()
call_command('wipe_data', '--domain=example.net', '--all', stdout=output)
assert form_1.data_class().count() == 1
assert form_2.data_class().count() == 1
assert (
output.getvalue()
== '''SIMULATION MODE: no actual wiping will happen.
(use --no-simulate after checking results)
example: 1
example2: 1
'''
)
# test with no options
call_command('wipe_data', '--domain=example.net')
call_command('wipe_data', '--domain=example.net', '--no-simulate')
assert form_1.data_class().count() == 1
assert form_2.data_class().count() == 1
# wipe one form formdatas
call_command('wipe_data', '--domain=example.net', '--forms=%s' % form_1.url_name)
call_command('wipe_data', '--domain=example.net', '--no-simulate', '--forms=%s' % form_1.url_name)
assert form_1.data_class().count() == 0
assert form_2.data_class().count() == 1
# wipe all formdatas
call_command('wipe_data', '--domain=example.net', '--all')
call_command('wipe_data', '--domain=example.net', '--no-simulate', '--all')
assert form_1.data_class().count() == 0
assert form_2.data_class().count() == 0
# exclude some forms
formdata_1.store()
formdata_2.store()
call_command('wipe_data', '--domain=example.net', '--all', '--exclude-forms=%s' % form_2.url_name)
call_command(
'wipe_data', '--domain=example.net', '--no-simulate', '--all', '--exclude-forms=%s' % form_2.url_name
)
assert form_1.data_class().count() == 0
assert form_2.data_class().count() == 1

View File

@ -31,12 +31,17 @@ class Command(TenantCommand):
parser.add_argument(
'--exclude-forms', metavar='FORMS', help='list of forms to exclude (slugs, separated by commas)'
)
parser.add_argument('--no-simulate', action='store_true', help='perform the wipe for real')
def handle(self, *args, **options):
if not options.get('no_simulate'):
self.stdout.write('SIMULATION MODE: no actual wiping will happen.\n')
self.stdout.write('(use --no-simulate after checking results)\n\n')
for domain in self.get_domains(**options):
self.init_tenant_publisher(domain, register_tld_names=False)
if options.get('all'):
formdefs = FormDef.select()
formdefs = FormDef.select(order_by='url_name')
elif options.get('forms'):
formdefs = [FormDef.get_by_urlname(x) for x in options['forms'].split(',')]
else:
@ -44,4 +49,9 @@ class Command(TenantCommand):
if options.get('exclude_forms'):
formdefs = [x for x in formdefs if x.url_name not in options['exclude_forms'].split(',')]
for formdef in formdefs:
formdef.data_class().wipe()
if options.get('no_simulate'):
formdef.data_class().wipe()
else:
count = formdef.data_class().count()
if count:
self.stdout.write(f'{formdef.url_name}: {count}\n')