From d1db8259957cc8b51bfc2b4a043bfcb3605c6917 Mon Sep 17 00:00:00 2001 From: Alejandro Diaz Date: Tue, 12 Sep 2023 16:42:43 -0600 Subject: [PATCH] improve superuser creation --- ui/homemanager/settings.py | 5 +++-- ui/homemanager/wsgi.py | 24 +++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/ui/homemanager/settings.py b/ui/homemanager/settings.py index 5b5a7b8..e9f95c7 100644 --- a/ui/homemanager/settings.py +++ b/ui/homemanager/settings.py @@ -14,6 +14,7 @@ from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +DATA_DIR = Path("/tmp").resolve() # Quick-start development settings - unsuitable for production @@ -77,7 +78,7 @@ WSGI_APPLICATION = 'homemanager.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': '/tmp/db.sqlite3', + 'NAME': DATA_DIR / 'db.sqlite3', } } @@ -117,7 +118,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/4.2/howto/static-files/ STATIC_URL = 'static/' -STATIC_ROOT = '/tmp/static' +STATIC_ROOT = DATA_DIR / 'static' # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/ui/homemanager/wsgi.py b/ui/homemanager/wsgi.py index 3adf15f..cdbf624 100644 --- a/ui/homemanager/wsgi.py +++ b/ui/homemanager/wsgi.py @@ -9,18 +9,20 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ import os from pathlib import Path +from homemanager.settings import DATABASES os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'homemanager.settings') -data = Path("/tmp/db.sqlite3") -if not data.is_file(): - from django.core.management import call_command - - os.environ.setdefault('DJANGO_SUPERUSER_PASSWORD', 'password') - call_command("migrate --noinput") - call_command("collectstatic --noinput") - call_command("createsuperuser --noinput --username adp --email a@a.com") - call_command("createsuperuser --noinput --username fabian --email b@a.com") - - from django.core.wsgi import get_wsgi_application application = get_wsgi_application() + +data = DATABASES["default"]["NAME"] +if not data.is_file(): + from django.core.management import call_command + call_command("migrate", "--noinput") + call_command("collectstatic", "--noinput") + + from django.contrib.auth import get_user_model + User = get_user_model() + User.objects.create_superuser('adp', 'a@a.com', 'password') + User.objects.create_superuser('fabian', 'b@a.com', 'password') +