Createsuperuser pre-start: don't do that when _any_ users exist in the DB

Fixes #54
This commit is contained in:
Klaas van Schelven
2025-03-07 09:52:43 +01:00
parent 97f03a8951
commit b560628c19

View File

@@ -12,17 +12,25 @@ User = get_user_model()
class Command(BaseCommand):
help = "Pre-start command to run before the server starts."
def handle(self, *args, **options):
if os.getenv("CREATE_SUPERUSER", ""):
def _create_superuser_if_needed(self):
if not os.getenv("CREATE_SUPERUSER", ""):
return
if ":" not in os.getenv("CREATE_SUPERUSER"):
raise ValueError("CREATE_SUPERUSER should be in the format 'username:password'")
username, password = os.getenv("CREATE_SUPERUSER").split(":")
if not User.objects.filter(username=username).exists():
if User.objects.all().exists():
print("Superuser not created: user(s) already exist.")
return
User.objects.create_superuser(username=username, password=password)
print(f"Superuser created: {username}")
def handle(self, *args, **options):
self._create_superuser_if_needed()
# Similar considerations apply here as those which are documented in bugsink.views._phone_home().
# By putting this in prestart, we add one more location to the list of kick-off locations; with the added
# benefit that this particular location also gives some signal for (Docker) installations that are prematurely