Skip to content

Commit

Permalink
Merge pull request #933 from to-sta/populate_db
Browse files Browse the repository at this point in the history
populating the database with dummy data
  • Loading branch information
andrewtavis authored Jul 31, 2024
2 parents 3120078 + f779fc1 commit 24c24fe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/authentication/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
31 changes: 31 additions & 0 deletions backend/backend/management/commands/populate_db.py
Original file line number Diff line number Diff line change
@@ -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}"
)
)
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 24c24fe

Please sign in to comment.