forked from drunkleen/gatekeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
50 lines (40 loc) · 1.42 KB
/
cli.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
import os
import django
from getpass import getpass
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "GateKeeper.settings")
django.setup()
from core.models import UserAccount
def create_admin_user():
while True:
username = input('Enter username: ')
if username is not None and len(username) > 3:
break
print("username is not acceptable! please try again.")
while True:
email = input('Enter email: ')
if email is not None and "@" in email and "." in email and len(email) > 9:
break
print("Email are not acceptable! please try again.")
while True:
password = getpass('Enter password: ')
repeated_password = getpass('Repeat password: ')
if password == repeated_password:
break
print("Passwords does not match! please try again.")
try:
admin_user = UserAccount.objects.create_user(
username=username,
email=email,
account_type=UserAccount.type_admin,
)
admin_user.is_staff = True
admin_user.is_superuser = True
admin_user.set_password(password)
admin_user.save()
except Exception as e:
print(f"an error occurred while creating\n{e}")
print(f"Admin user '{username}' created successfully.")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "createadmin":
create_admin_user()