From 229230c836f120de62ccd0697d84760bf07fc4b9 Mon Sep 17 00:00:00 2001 From: Kai Schlamp Date: Thu, 16 May 2024 20:43:44 +0000 Subject: [PATCH] Improve how to populate users and groups --- .../commands/populate_users_and_groups.py | 27 ++++++++++++++++--- tasks.py | 4 +-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/adit_radis_shared/common/management/commands/populate_users_and_groups.py b/adit_radis_shared/common/management/commands/populate_users_and_groups.py index 196c990..8530664 100644 --- a/adit_radis_shared/common/management/commands/populate_users_and_groups.py +++ b/adit_radis_shared/common/management/commands/populate_users_and_groups.py @@ -13,6 +13,13 @@ fake = Faker() +PREDFINED_GROUPS = [ + "Thoraxklinik", + "Allgemeinradiologie", + "Neuroradiologie", + "Studienzentrum", +] + def create_admin() -> User: if "ADMIN_USERNAME" not in environ or "ADMIN_PASSWORD" not in environ: @@ -46,7 +53,7 @@ def create_users(users_count: int) -> list[User]: if i == 0: user = create_admin() else: - user = UserFactory.create() + user = UserFactory.create(username=fake.unique.user_name()) users.append(user) return users @@ -55,11 +62,23 @@ def create_users(users_count: int) -> list[User]: def create_groups(users: list[User], groups_count: int) -> list[Group]: groups: list[Group] = [] - for _ in range(groups_count): - group = GroupFactory.create() + for i in range(groups_count): + if i < len(PREDFINED_GROUPS): + group_name = PREDFINED_GROUPS[i] + group = GroupFactory.create(name=group_name) + else: + group = GroupFactory.create() groups.append(group) - for user in users: + # Add admin to all groups and make the first one active + admin = users[0] + for group in groups: + admin.groups.add(group) + if not admin.active_group: + admin.change_active_group(group) + + # Add all users to a random group and make it active + for user in users[1:]: group: Group = fake.random_element(elements=groups) user.groups.add(group) if not user.active_group: diff --git a/tasks.py b/tasks.py index cc9a178..dd7a1f3 100644 --- a/tasks.py +++ b/tasks.py @@ -52,8 +52,8 @@ def reset_db(ctx: Context): def populate_db(ctx: Context): """Populate database with users and groups""" cmd = f"{manage_cmd} populate_users_and_groups" - cmd += " --users 100" - cmd += " --groups 3" + cmd += " --users 30" + cmd += " --groups 5" ctx.run(cmd, pty=True)