Skip to content

Commit

Permalink
add user script
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeygrigorev committed Sep 15, 2024
1 parent d6222c7 commit 47d8ea7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ admin:
pipenv run python manage.py createsuperuser


user:
pipenv run python add_user.py


tests: ## Run tests
tests:
pipenv run python manage.py test courses.tests
Expand Down
49 changes: 49 additions & 0 deletions add_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()

0 comments on commit 47d8ea7

Please sign in to comment.