From f779fc11548168efcb597b0b07bb284e410970f2 Mon Sep 17 00:00:00 2001 From: tosta Date: Sun, 14 Jul 2024 10:45:46 +0200 Subject: [PATCH] populating the database with 100 users --- backend/authentication/factories.py | 1 + .../management/commands/populate_db.py | 31 +++++++++++++++++++ docker-compose.yml | 1 + 3 files changed, 33 insertions(+) create mode 100644 backend/backend/management/commands/populate_db.py diff --git a/backend/authentication/factories.py b/backend/authentication/factories.py index e5388ce71..fd8a9fcd0 100644 --- a/backend/authentication/factories.py +++ b/backend/authentication/factories.py @@ -35,6 +35,7 @@ class UserFactory(factory.django.DjangoModelFactory): class Meta: model = UserModel exclude = ("plaintext_password",) + django_get_or_create = ("username",) username = factory.Faker("user_name") name = factory.Faker("name") diff --git a/backend/backend/management/commands/populate_db.py b/backend/backend/management/commands/populate_db.py new file mode 100644 index 000000000..e9f78415f --- /dev/null +++ b/backend/backend/management/commands/populate_db.py @@ -0,0 +1,31 @@ +from argparse import ArgumentParser +from typing import TypedDict, Unpack + +from django.core.management.base import BaseCommand + +from authentication.factories import UserFactory + + +class Options(TypedDict): + users: int + + +class Command(BaseCommand): + help = "Populate the database with dummy data" + + def add_arguments(self, parser: ArgumentParser) -> None: + parser.add_argument("--users", type=int, default=100) + + def handle(self, *args: str, **options: Unpack[Options]) -> None: + number_of_users = options["users"] + try: + UserFactory.create_batch(number_of_users) + self.stdout.write( + self.style.ERROR(f"Number of users created: {number_of_users}") + ) + except Exception as error: + self.stdout.write( + self.style.ERROR( + f"An error occured during the creation of dummy data: {error}" + ) + ) diff --git a/docker-compose.yml b/docker-compose.yml index d54f48665..74c399df2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,6 +14,7 @@ services: python manage.py loaddata fixtures/superuser.json && python manage.py loaddata fixtures/status_types.json && python manage.py loaddata fixtures/iso_code_map.json && + python manage.py populate_db --users=100 && python manage.py runserver 0.0.0.0:${BACKEND_PORT}" ports: - "${BACKEND_PORT}:${BACKEND_PORT}"