-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_super_user.py
executable file
·37 lines (30 loc) · 1.15 KB
/
create_super_user.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
#!/usr/bin/env python
import os
import sys
from dotenv import load_dotenv
import django
from django.conf import settings
if __name__ == "__main__":
print('deprecating soon; use `./manage.py create_user` instead')
# if dotenv file, load it
dotenv_path = os.environ.get('CATCHPY_DOTENV_PATH', None)
if dotenv_path:
load_dotenv(dotenv_path)
# define settings if not in environment
if os.environ.get("DJANGO_SETTINGS_MODULE", None) is None:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "catchpy.settings.dev")
django.setup()
from django.contrib.auth.models import User
# only creates admin user if it does not exists
username = os.environ.get('CATCHPY_ADMIN_USER', None)
password = os.environ.get('CATCHPY_ADMIN_PASSWORD', None)
if User._default_manager.filter(username=username).count() == 0:
if username and password:
u = User(username=username)
u.set_password(password)
u.is_superuser = True
u.is_staff = True
u.save()
else:
raise NameError(
"username or password missing - admin user not created")