From 96a2229a6993dc4ce1aaa8e3079cb0d9c2ab1ccb Mon Sep 17 00:00:00 2001
From: Martin Mares <mj@ucw.cz>
Date: Sun, 12 Mar 2023 13:12:03 +0100
Subject: [PATCH] =?UTF-8?q?Skript=20na=20anonymizaci=20testovac=C3=AD=20in?=
 =?UTF-8?q?stance?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 bin/test-anonymize | 80 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100755 bin/test-anonymize

diff --git a/bin/test-anonymize b/bin/test-anonymize
new file mode 100755
index 00000000..643b7afb
--- /dev/null
+++ b/bin/test-anonymize
@@ -0,0 +1,80 @@
+#!/usr/bin/env python3
+
+import argparse
+import random
+from unidecode import unidecode
+
+import mo.db as db
+import mo.util
+from mo.util import die
+
+parser = argparse.ArgumentParser(description='Anonymizuje všechny účastníky')
+parser.add_argument('--really', default=False, action='store_true', help='potvrdí, že to opravdu chceme udělat')
+parser.add_argument('--seed', type=str, default='seedme', help='random seed')
+parser.add_argument('-n', '--dry-run', default=False, action='store_true', help='vyzkouší sloučení nanečisto (necommitne)')
+
+args = parser.parse_args()
+mo.util.init_standalone()
+
+if not args.really:
+    die("Nejsem si jistý, jestli to chci udělat. Řekni mi, že opravdu.")
+
+
+def load(name):
+    with open(f'etc/names/{name}') as f:
+        return [line.strip() for line in f]
+
+
+first_names = [load('first-f'), load('first-m')]
+last_names = [load('last-f'), load('last-m')]
+used_names = set()
+rng = random.Random(args.seed)
+
+
+def norm(name):
+    return unidecode(name).lower()
+
+
+def gen_name(orig_last):
+    if orig_last.endswith('á'):
+        gender = 0
+    else:
+        gender = 1
+
+    while True:
+        f = rng.choice(first_names[gender])
+        l = rng.choice(last_names[gender])
+        ff, ll = norm(f), norm(l)
+        stripped = f'{ff} {ll}'
+        if stripped not in used_names:
+            used_names.add(stripped)
+            return f, l, ff, ll
+
+
+sess = db.get_session()
+
+users = sess.query(db.User).all()
+cnt_anoned = 0
+cnt_orgs = 0
+cnt_already = 0
+
+for u in users:
+    if u.is_org or u.is_admin:
+        cnt_orgs += 1
+    elif u.email.endswith('@test'):
+        cnt_already += 1
+    else:
+        first, last, first_stripped, last_stripped = gen_name(u.last_name)
+        email = f'{first_stripped}.{last_stripped}@test'
+        print(f'{u.first_name} {u.last_name} -> {first} {last} <{email}>')
+        u.first_name = first
+        u.last_name = last
+        u.email = email
+        cnt_anoned += 1
+
+if not args.dry_run:
+    sess.commit()
+else:
+    sess.rollback()
+
+print(f'Hotovo: {cnt_anoned} anonymizováno, {cnt_already} už bylo, ponecháno {cnt_orgs} organizátorů')
-- 
GitLab