32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
from django.core.management.base import BaseCommand
|
|
from django.contrib.auth.models import User
|
|
from decouple import config
|
|
from django.db import transaction
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Check if any users exist and create admin if none exist'
|
|
|
|
def handle(self, *args, **options):
|
|
# Check if any users exist
|
|
if User.objects.exists():
|
|
self.stdout.write(
|
|
self.style.WARNING('Users already exist, skipping admin creation')
|
|
)
|
|
return
|
|
|
|
# Get credentials from environment variables or use defaults
|
|
username = config('ADMIN_USERNAME', default='admin')
|
|
password = config('ADMIN_PASSWORD', default='admin123')
|
|
email = config('ADMIN_EMAIL', default='admin@boulangerie.fr')
|
|
|
|
# Create the admin user
|
|
with transaction.atomic():
|
|
user = User.objects.create_superuser(
|
|
username=username,
|
|
email=email,
|
|
password=password
|
|
)
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(f'Successfully created admin user "{username}"')
|
|
) |