From e73b0e2b9eda96c4b7621b24e306c5d44f995184 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Wed, 22 Aug 2018 12:50:26 -0300 Subject: [PATCH] =?UTF-8?q?Cria=20usuario=20admin=20interlegis=20no=20setu?= =?UTF-8?q?p=20padr=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit O script também pode ser usado posteriormente para criar ou atualizar a senha do usuário interlegis --- create_admin.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/create_admin.py b/create_admin.py index f03bbd7c8..e67c8afd4 100755 --- a/create_admin.py +++ b/create_admin.py @@ -7,24 +7,43 @@ import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sapl.settings") +def get_enviroment_admin_password(username): + password = os.environ.get('ADMIN_PASSWORD') + if not password: + print( + "[SUPERUSER] Environment variable $ADMIN_PASSWORD" + " for user %s was not set. Leaving..." % username) + sys.exit('MISSING_ADMIN_PASSWORD') + + +def create_user_interlegis(): + from django.contrib.auth.models import User + + password = get_enviroment_admin_password('interlegis') + print("[SUPERUSER INTERLEGIS] Creating interlegis superuser...") + user, created = User.objects.get_or_create(username='interlegis') + if not created: + print("[SUPERUSER INTERLEGIS] User interlegis already exists." + " Updating password.") + user.is_superuser = True + user.is_staff = True + user.set_password(password) + user.save() + print("[SUPERUSER INTERLEGIS] Done.") + + def create_superuser(): from django.contrib.auth.models import User username = "admin" - password = os.environ[ - 'ADMIN_PASSWORD'] if 'ADMIN_PASSWORD' in os.environ else None - email = os.environ['ADMIN_EMAIL'] if 'ADMIN_EMAIL' in os.environ else '' + email = os.environ.get('ADMIN_EMAIL', '') if User.objects.filter(username=username).exists(): print("[SUPERUSER] User %s already exists." " Exiting without change." % username) sys.exit('ADMIN_USER_EXISTS') else: - if not password: - print( - "[SUPERUSER] Environment variable $ADMIN_PASSWORD" - " for user %s was not set. Leaving..." % username) - sys.exit('MISSING_ADMIN_PASSWORD') + password = get_enviroment_admin_password(username) print("[SUPERUSER] Creating superuser...") @@ -39,4 +58,5 @@ def create_superuser(): if __name__ == '__main__': django.setup() + create_user_interlegis() # must come before create_superuser create_superuser()