40 lines
1.7 KiB
Python
40 lines
1.7 KiB
Python
import sys
|
|
from django.apps import AppConfig
|
|
|
|
class UsersConfig(AppConfig):
|
|
default_auto_field = 'django.db.models.BigAutoField'
|
|
name = 'users'
|
|
|
|
def ready(self):
|
|
# Only run this logic when Django is ready and not during migrations/tests
|
|
if 'test' in sys.argv or 'migrate' in sys.argv or 'makemigrations' in sys.argv:
|
|
return
|
|
|
|
# Only run when the server is actually starting
|
|
if 'runserver' in sys.argv:
|
|
try:
|
|
# Import here to avoid issues during Django startup
|
|
from django.contrib.auth.models import User
|
|
from decouple import config
|
|
from django.db import transaction
|
|
|
|
# Check if any users exist
|
|
if not User.objects.exists():
|
|
# 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
|
|
)
|
|
|
|
print(f'Successfully created admin user "{username}"')
|
|
except Exception as e:
|
|
# Don't fail the entire Django startup if there's an error
|
|
print(f'Error creating admin user: {e}')
|