From 2d718c5e3a29e552f26e2ed4672bb5d3541912c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sat, 23 Nov 2024 17:08:38 +0100 Subject: [PATCH] Add new management command to create superuser if not present. Use that command in the entrypoint scripts --- entrypoint.sh | 2 ++ entrypoint_self_hosted.sh | 1 + .../commands/create_kenkyusho_admin_user.py | 23 +++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 shimatta_kenkyusho/parts/management/commands/create_kenkyusho_admin_user.py diff --git a/entrypoint.sh b/entrypoint.sh index 9295252..2051376 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,4 +3,6 @@ source /home/shimatta/kenkyusho/.venv/bin/activate cd /home/shimatta/kenkyusho/shimatta_kenkyusho python manage.py migrate --settings shimatta_kenkyusho.settings_production python manage.py collectstatic --settings shimatta_kenkyusho.settings_production --noinput +python manage.py create_kenkyusho_admin_user --settings shimatta_kenkyusho.settings_production + gunicorn -w 4 --bind 0.0.0.0:8000 shimatta_kenkyusho.wsgi:application diff --git a/entrypoint_self_hosted.sh b/entrypoint_self_hosted.sh index e78916d..ffb06b3 100755 --- a/entrypoint_self_hosted.sh +++ b/entrypoint_self_hosted.sh @@ -2,5 +2,6 @@ source /home/shimatta/kenkyusho/.venv/bin/activate cd /home/shimatta/kenkyusho/shimatta_kenkyusho python manage.py migrate --settings shimatta_kenkyusho.settings_production +python manage.py create_kenkyusho_admin_user --settings shimatta_kenkyusho.settings_production python manage.py runserver 0.0.0.0:8000 --settings shimatta_kenkyusho.settings_production diff --git a/shimatta_kenkyusho/parts/management/commands/create_kenkyusho_admin_user.py b/shimatta_kenkyusho/parts/management/commands/create_kenkyusho_admin_user.py new file mode 100644 index 0000000..fdf6092 --- /dev/null +++ b/shimatta_kenkyusho/parts/management/commands/create_kenkyusho_admin_user.py @@ -0,0 +1,23 @@ +from django.core.management.base import BaseCommand, CommandParser +from django.contrib.auth import get_user_model + +class Command(BaseCommand): + help = "Create a default superuser if no superuser is already present. This aids automatic deployment inside a container." + + def add_arguments(self, parser: CommandParser): + parser.add_argument('--user', + help='Username to create if no admin account is present', + default='admin') + parser.add_argument('--password', + help='Password to set for newly created user. Ignored, if any admin user is already present', + default='admin') + + def handle(self, *args, **options): + User = get_user_model() + + # Query if there is any admin user + if not User.objects.filter(is_superuser=True).exists(): + self.stdout.write(f'No superuser present. Creating {options['user']} with supplied password') + User.objects.create_superuser(username=options['user'], password=options['password']) + else: + self.stdout.write('At least one superuser already exists. Skipping superuser creation') \ No newline at end of file