forked from interlegis/sapl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_admin.py
executable file
·63 lines (46 loc) · 1.74 KB
/
create_admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
import os
import sys
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')
return 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"
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:
password = get_enviroment_admin_password(username)
print("[SUPERUSER] Creating superuser...")
u = User.objects.create_superuser(
username=username, password=password, email=email)
u.save()
print("[SUPERUSER] Done.")
sys.exit(0)
if __name__ == '__main__':
django.setup()
create_user_interlegis() # must come before create_superuser
create_superuser()