From f5ffd02dea38e84e8094f612e2ff718ea6d7c50f Mon Sep 17 00:00:00 2001 From: Brian Clark Date: Thu, 10 Oct 2024 11:30:22 -0400 Subject: [PATCH] Add admin page setup script --- src/base/management/__init__.py | 0 src/base/management/commands/__init__.py | 0 src/base/management/commands/initadmin.py | 19 +++++++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 src/base/management/__init__.py create mode 100644 src/base/management/commands/__init__.py create mode 100644 src/base/management/commands/initadmin.py diff --git a/src/base/management/__init__.py b/src/base/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/base/management/commands/__init__.py b/src/base/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/base/management/commands/initadmin.py b/src/base/management/commands/initadmin.py new file mode 100644 index 0000000..24160a9 --- /dev/null +++ b/src/base/management/commands/initadmin.py @@ -0,0 +1,19 @@ +import os +from django.core.management.base import BaseCommand +from django.contrib.auth.models import User + + +ADMIN_USERNAME = os.environ.get("ADMIN_USERNAME", "admin") # Default username +ADMIN_EMAIL = os.environ.get("ADMIN_EMAIL", "admin@andrew.cmu.edu") # Default email +ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD", "admin123") # Default password + + +class Command(BaseCommand): + help = "Automatically creates a superuser" + + def handle(self, *args, **kwargs): + if not User.objects.filter(username=ADMIN_USERNAME).exists(): + User.objects.create_superuser(ADMIN_USERNAME, ADMIN_EMAIL, ADMIN_PASSWORD) + self.stdout.write(self.style.SUCCESS("Superuser created successfully")) + else: + self.stdout.write(self.style.WARNING("Superuser already exists"))