Skip to content

Commit

Permalink
feat: create superuser only if it doesn't already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed Jan 10, 2024
1 parent 5b84649 commit dec4de7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions benefits/superuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import logging

from django.contrib.auth import get_user_model

logger = logging.getLogger(__name__)

User = get_user_model() # get the currently active user model

username = os.environ.get("DJANGO_SUPERUSER_USERNAME")
user = User.objects.filter(username=username)

if user.exists():
logger.debug("Skipping superuser creation since it already exists")
else:
email = os.environ.get("DJANGO_SUPERUSER_EMAIL")
password = os.environ.get("DJANGO_SUPERUSER_PASSWORD")

logger.debug("Creating superuser")
User.objects.create_superuser(username, email, password)
2 changes: 1 addition & 1 deletion bin/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ python manage.py migrate
# check DJANGO_ADMIN = true, default to false if empty or unset

if [[ ${DJANGO_ADMIN:-false} = true ]]; then
python manage.py createsuperuser --no-input
cat benefits/superuser.py | python manage.py shell
else
echo "superuser: Django not configured for Admin access"
fi
Expand Down

0 comments on commit dec4de7

Please sign in to comment.