From 21b59467ea5dfcc73375ee9f60c426a208f251c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Wed, 22 Jan 2025 22:35:09 +0100 Subject: [PATCH] Add a way to make it easy to reset the database with some testing data. --- Justfile | 14 ++++++++ src/utils/management/__init__.py | 0 src/utils/management/commands/__init__.py | 0 .../management/commands/bootstrap_dev_data.py | 33 +++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 src/utils/management/__init__.py create mode 100644 src/utils/management/commands/__init__.py create mode 100644 src/utils/management/commands/bootstrap_dev_data.py diff --git a/Justfile b/Justfile index 5171ea2..bb624e4 100644 --- a/Justfile +++ b/Justfile @@ -10,6 +10,20 @@ build: @echo "Building the app" docker compose build +down_with_volumes: + @echo "Taking the docker compose stack down, including volumes" + docker compose down -v + +migrate: + @echo "Migrating the database" + docker compose run -w /app/src --rm -u `id -u` app python manage.py migrate + +bootstrap_dev_data: + @echo "Bootstrapping development data" + docker compose run -w /app/src --rm -u `id -u` app python manage.py bootstrap_dev_data + +reset_db: down_with_volumes migrate bootstrap_dev_data + typecheck: docker compose run -w /app/src --rm app mypy . diff --git a/src/utils/management/__init__.py b/src/utils/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/management/commands/__init__.py b/src/utils/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/management/commands/bootstrap_dev_data.py b/src/utils/management/commands/bootstrap_dev_data.py new file mode 100644 index 0000000..a1d04f8 --- /dev/null +++ b/src/utils/management/commands/bootstrap_dev_data.py @@ -0,0 +1,33 @@ +"""Command to bootstrap development data.""" + +from django.contrib.auth.models import User +from django.core.management import BaseCommand + + +class Command(BaseCommand): + """Command to bootstrap development data.""" + + help = "Bootstrap dev data" + + superuser: User + normal_users: list[User] + + def handle(self, *args: str, **options: str) -> None: + """Handle the command.""" + self.create_superuser() + self.create_normal_users() + + def create_superuser(self) -> None: + """Create superuser.""" + self.stdout.write("Creating superuser") + self.superuser = User.objects.create_superuser("admin", "admin@example.com") + self.superuser.set_password("admin") + self.superuser.save() + + def create_normal_users(self) -> None: + """Create normal users.""" + self.stdout.write("Creating normal users") + self.normal_users = [User.objects.create_user(f"user{i}", email=f"user{i}@example.com") for i in range(1, 4)] + for user in self.normal_users: + user.set_password(user.username) + user.save()