From 06cf055f505530dfb2aae0dc958fed92a659bf4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Tue, 7 Jun 2022 10:38:45 +0200
Subject: [PATCH] tunning: Command for users anonymization

---
 tuning/management/__init__.py                 |  0
 tuning/management/commands/__init__.py        |  0
 tuning/management/commands/anonymize_users.py | 29 +++++++++++++++++++
 3 files changed, 29 insertions(+)
 create mode 100644 tuning/management/__init__.py
 create mode 100644 tuning/management/commands/__init__.py
 create mode 100644 tuning/management/commands/anonymize_users.py

diff --git a/tuning/management/__init__.py b/tuning/management/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tuning/management/commands/__init__.py b/tuning/management/commands/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tuning/management/commands/anonymize_users.py b/tuning/management/commands/anonymize_users.py
new file mode 100644
index 00000000..ae1fa330
--- /dev/null
+++ b/tuning/management/commands/anonymize_users.py
@@ -0,0 +1,29 @@
+from django.conf import settings
+from django.contrib.auth import get_user_model
+from django.core.management.base import BaseCommand, CommandError
+from faker import Faker
+
+
+class Command(BaseCommand):
+    def add_arguments(self, parser):
+        parser.add_argument("-f", action="store_true")
+
+    def handle(self, *args, **options):
+        if settings.MAJAK_ENV != "dev":
+            raise CommandError("Possible to run only in dev environment.")
+
+        if not options["f"]:
+            self.stdout.write("Use param -f to actually anonymize users.")
+            return
+
+        fake = Faker()
+        User = get_user_model()
+
+        for u in User.objects.all():
+            old = f"{u} | {u.email}"
+            u.first_name = fake.first_name()
+            u.last_name = fake.last_name()
+            u.email = fake.email()
+            u.save()
+            new = f"{u} | {u.email}"
+            self.stdout.write(f"{old} -> {new}")
-- 
GitLab