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

Closed
fpeters wants to merge 1 commits from wip/29929-wipe-data-dry-run into wip/31942-wipe-data
2 changed files with 22 additions and 6 deletions

View File

@ -1,4 +1,5 @@
import collections
import io
import json
import os
import pickle
@ -127,25 +128,34 @@ 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() == 'example: 1\nexample2: 1\n'
# 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,13 @@ 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):
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 +45,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')