-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathadd_user.py
49 lines (36 loc) · 1.24 KB
/
add_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
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import django
import string
import random
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE", "course_management.settings"
)
django.setup()
from django.contrib.auth import get_user_model # noqa: E402
User = get_user_model()
def generate_password(length=12):
characters = string.ascii_letters + string.digits
return "".join(random.choice(characters) for _ in range(length))
def create_user():
print("Creating a new Django user")
email = input("Enter email: ")
password = generate_password()
try:
user = User.objects.create_user(
username=email, email=email, password=password
)
print(f"User with email {email} created successfully.")
print(f"Generated password: {password}")
print("Please make sure to save this password securely.")
is_superuser = (
input("Make this user a superuser? (y/n): ").lower() == "y"
)
if is_superuser:
user.is_superuser = True
user.is_staff = True
user.save()
print("User has been granted superuser privileges.")
except django.db.utils.IntegrityError:
print(f"A user with email {email} already exists.")
if __name__ == "__main__":
create_user()