Skip to content

Commit

Permalink
Improve how to populate users and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed May 16, 2024
1 parent 4ce49af commit 229230c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 229230c

Please sign in to comment.