Skip to content

Commit 741d10a

Browse files
chore(admin): add new command to create admin user (#1172)
* add new command to create admin user * improved on AI suggestion suggestion * rename command --------- Co-authored-by: Mario Behling <[email protected]>
1 parent e768ee0 commit 741d10a

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ After running ``uv sync```, activate a virtual environment
127127

128128
Do **not** edit the database numbers (0, 1, etc.).
129129

130-
8. **Create a superuser account** (for accessing the admin panel):
130+
8. **Create a admin user account** (for accessing the admin panel):
131131

132132
.. code-block:: bash
133133
134-
python manage.py createsuperuser
134+
python manage.py create_admin_user
135135
136136
9. **Run the development server**:
137137

app/eventyay/base/management/__init__.py

Whitespace-only changes.

app/eventyay/base/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
from django.core.validators import validate_email
3+
from django.core.exceptions import ValidationError
4+
from django.contrib.auth.password_validation import validate_password
5+
from getpass import getpass
6+
7+
from eventyay.base.models.auth import User
8+
9+
10+
class Command(BaseCommand):
11+
help = 'Creates an admin user without setting is_superuser to True.'
12+
13+
def handle(self, *args, **options):
14+
email = self.get_email()
15+
password = self.get_password()
16+
17+
# Prevent duplicate users
18+
if User.objects.filter(email=email).exists():
19+
raise CommandError(f"A user with email {email} already exists.")
20+
21+
# Create admin user
22+
user = User.objects.create_adminuser(email=email, password=password)
23+
24+
self.stdout.write(self.style.SUCCESS(f"Successfully created admin user: {user.email}"))
25+
26+
def get_email(self):
27+
"""Prompt for email and validate format."""
28+
while True:
29+
email = input("E-mail: ").strip()
30+
if not email:
31+
self.stderr.write(self.style.ERROR("The email field cannot be empty."))
32+
continue
33+
try:
34+
validate_email(email)
35+
return email
36+
except ValidationError:
37+
self.stderr.write(self.style.ERROR("Please enter a valid email address."))
38+
39+
def get_password(self):
40+
"""Prompt for password and apply Django's password validation."""
41+
while True:
42+
password1 = self._get_password("Password: ")
43+
password2 = self._get_password("Confirm password: ")
44+
45+
if password1 != password2:
46+
self.stderr.write(self.style.ERROR("Passwords do not match. Please try again."))
47+
continue
48+
49+
try:
50+
validate_password(password1)
51+
return password1
52+
except ValidationError as e:
53+
for error in e.messages:
54+
self.stderr.write(self.style.ERROR(error))
55+
56+
def _get_password(self, prompt):
57+
"""Securely read a password input."""
58+
return getpass(prompt)

app/eventyay/base/models/auth.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ def create_superuser(self, email: str, password: str = None): # NOQA
9797
user.save()
9898
return user
9999

100+
def create_adminuser(self, email: str, password: str = None):
101+
"""
102+
Command: python manage.py create_admin_user
103+
Create an admin user without setting is_superuser to True.
104+
"""
105+
if password is None:
106+
raise ValueError("You must provide a password")
107+
108+
user = self.model(email=email)
109+
user.is_staff = True
110+
user.is_administrator = True
111+
user.set_password(password)
112+
user.save(using=self._db)
113+
return user
114+
100115

101116
def generate_notifications_token():
102117
return get_random_string(length=32)

0 commit comments

Comments
 (0)