-
Notifications
You must be signed in to change notification settings - Fork 14
/
anonymise_db.py
51 lines (40 loc) · 1.52 KB
/
anonymise_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Anonymise all personal data in the database
"""
from django.core.management import BaseCommand
from ... import app_settings
from ...registry import registry
class Command(BaseCommand):
help = "Anonymises all personal data in the database"
def add_arguments(self, parser):
parser.add_argument(
"--noinput",
"--no-input",
action="store_false",
dest="interactive",
default=True,
help="Tells Django to NOT prompt the user for input of any kind.",
)
def handle(self, *args, **options):
if not app_settings.GDPR_CAN_ANONYMISE_DATABASE:
raise ValueError("Database anonymisation is not enabled")
interactive = options["interactive"]
if interactive: # pragma: no cover
confirm = input(
"""Warning!
You have requested that all personal information in the database is anonymised.
This will IRREVERSIBLY OVERWRITE all personal data currently in the database.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: """
)
else:
confirm = "yes"
if confirm == "yes":
for model in registry.models_allowed_to_anonymise():
model.objects.all().anonymise()
msg = "{} models anonymised.".format(
len(registry.models_allowed_to_anonymise())
)
self.stdout.write(msg)
else: # pragma: no cover
self.stdout.write("Anonymisation cancelled.")