|
1 | | -from django.core.management.base import BaseCommand |
| 1 | +""" |
| 2 | +Management command to create a default Django superuser. |
| 3 | +
|
| 4 | +This command creates a superuser if none exist, using either provided command-line |
| 5 | +arguments, environment variables, or fallback defaults. Intended for easy setup |
| 6 | +in development or initial deployments. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +from django.core.management import color_style |
| 12 | +from django.core.management.base import BaseCommand, CommandError |
2 | 13 | from django.contrib.auth import get_user_model |
3 | 14 |
|
| 15 | + |
4 | 16 | class Command(BaseCommand): |
| 17 | + """ |
| 18 | + Django management command to create a default superuser. |
| 19 | +
|
| 20 | + Optional arguments: |
| 21 | + --username: specify the superuser username (default 'admin' or env var) |
| 22 | + --email: specify the superuser email address (default 'admin@email.invalid' or env var) |
| 23 | + --password: specify the superuser password (default 'gradecheck' or env var) |
| 24 | +
|
| 25 | + If any user exists already, user creation will be skipped. |
| 26 | + """ |
| 27 | + |
5 | 28 | help = 'Creates a default superuser' |
6 | 29 |
|
| 30 | + def add_arguments(self, parser): |
| 31 | + parser.add_argument( |
| 32 | + '--username', |
| 33 | + type = str, |
| 34 | + help = 'Specify the superuser username', |
| 35 | + default = os.getenv('DJANGO_SUPERUSER_USERNAME', 'admin') |
| 36 | + ) |
| 37 | + parser.add_argument( |
| 38 | + '--email', |
| 39 | + type=str, |
| 40 | + help='Specify the superuser email', |
| 41 | + default=os.getenv('DJANGO_SUPERUSER_EMAIL', 'admin@email.invalid') |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + '--password', |
| 45 | + type=str, |
| 46 | + help='Specify the superuser password', |
| 47 | + default=os.getenv('DJANGO_SUPERUSER_PASSWORD', 'gradecheck') |
| 48 | + ) |
| 49 | + |
7 | 50 | def handle(self, *args, **options): |
8 | | - User = get_user_model() |
9 | | - if User.objects.exists(): |
10 | | - self.stdout.write(self.style.WARNING('Users already exist - skipping default user creation')) |
| 51 | + user_model = get_user_model() |
| 52 | + style = color_style() |
| 53 | + |
| 54 | + if user_model.objects.exists(): |
| 55 | + self.stdout.write(style.SUCCESS('Skipping default user creation')) # pylint: disable=no-member |
11 | 56 | return |
12 | | - User.objects.create_superuser( |
13 | | - username='admin', |
14 | | - email='admin@email.invalid', |
15 | | - password='gradecheck' |
16 | | - ) |
17 | | - self.stdout.write(self.style.SUCCESS('Default superuser created')) |
| 57 | + |
| 58 | + username = options['username'] |
| 59 | + email = options['email'] |
| 60 | + password = options['password'] |
| 61 | + |
| 62 | + try: |
| 63 | + user_model.objects.create_superuser(username=username, email=email, password=password) |
| 64 | + self.stdout.write(style.SUCCESS(f'Default superuser "{username}" created')) # pylint: disable=no-member |
| 65 | + except Exception as e: |
| 66 | + raise CommandError(f'Failed to create superuser: {e}') from e |
0 commit comments