Skip to content

Commit

Permalink
Resolve "Create super-user if absent at the app start"
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergii Borsuk authored and druzhynin-oleksii committed Sep 9, 2020
1 parent 3b5b189 commit 47f1a88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
23 changes: 23 additions & 0 deletions backend/createuser_ifabsent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging
import os

from django import setup
from django.conf import settings
from django.contrib.auth import get_user_model

logger = logging.getLogger(__name__)

if __name__ == "__main__":
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'model_garden.settings')
setup()

username = settings.DJANGO_ROOT_USER
email = settings.DJANGO_ROOT_EMAIL
password = settings.DJANGO_ROOT_PASSWORD

if username is not None and email is not None and password is not None:
User = get_user_model()

if not User.objects.filter(username=username).exists():
User.objects.create_superuser(username, email, password)
logger.info(f"Super user created: {username} - {email}")
5 changes: 5 additions & 0 deletions backend/model_garden/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@

STATIC_URL = '/static/'

# SUPER USER
DJANGO_ROOT_USER = env('DJANGO_ROOT_USER', default=None)
DJANGO_ROOT_EMAIL = env('DJANGO_ROOT_EMAIL', default=None)
DJANGO_ROOT_PASSWORD = env('DJANGO_ROOT_PASSWORD', default=None)

# AWS
AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID', default=None)
AWS_SECRET_KEY = env('AWS_SECRET_KEY', default=None)
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
build:
context: backend
restart: always
command: bash -c "python3 manage.py migrate && python3 manage.py loaddata default_bucket && python3 manage.py runserver 0.0.0.0:9000"
command: bash -c "python3 manage.py migrate && python3 manage.py loaddata default_bucket && python3 createuser_ifabsent.py && python3 manage.py runserver 0.0.0.0:9000"
environment:
DJANGO_DB_HOST: postgres
DJANGO_DB_PORT: 5432
Expand Down

0 comments on commit 47f1a88

Please sign in to comment.