From a84edb734aaf822e67adef08ba9968b19cff6b3e Mon Sep 17 00:00:00 2001 From: Louis Mazin Date: Mon, 21 Sep 2026 14:44:20 +0200 Subject: [PATCH] test --- backend/users/apps.py | 96 +++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 41 deletions(-) diff --git a/backend/users/apps.py b/backend/users/apps.py index 65d4e0a..153cf12 100644 --- a/backend/users/apps.py +++ b/backend/users/apps.py @@ -1,6 +1,7 @@ import sys import logging from django.apps import AppConfig +from django.core.management import execute_from_command_line logger = logging.getLogger(__name__) @@ -15,52 +16,65 @@ class UsersConfig(AppConfig): # Only run when the server is actually starting if 'runserver' in sys.argv: - try: - from .models import User - from decouple import config - from django.db import transaction + # Defer the superuser creation to avoid database access during app initialization + # This prevents the Django warning about accessing database during app initialization + import threading + import time - # Check if superuser already exists - existing_superusers = User.objects.filter(is_superuser=True) - logger.info(f"Found {existing_superusers.count()} existing superusers") - - if existing_superusers.exists(): - logger.info('A superuser already exists; no admin user was created.') - return + def create_superuser_if_needed(): + # Wait a bit to ensure Django is fully ready + time.sleep(0.1) + try: + from .models import User + from decouple import config + from django.db import transaction - username = config('ADMIN_USERNAME', default='').strip() - password = config('ADMIN_PASSWORD', default='') - first_name = config('ADMIN_FIRST_NAME', default='').strip() - last_name = config('ADMIN_LAST_NAME', default='').strip() + # Check if superuser already exists + existing_superusers = User.objects.filter(is_superuser=True) + logger.info(f"Found {existing_superusers.count()} existing superusers") + + if existing_superusers.exists(): + logger.info('A superuser already exists; no admin user was created.') + return - if not username or not password: - logger.info('ADMIN_USERNAME and ADMIN_PASSWORD are required; no admin was created.') - return + username = config('ADMIN_USERNAME', default='').strip() + password = config('ADMIN_PASSWORD', default='') + first_name = config('ADMIN_FIRST_NAME', default='').strip() + last_name = config('ADMIN_LAST_NAME', default='').strip() - with transaction.atomic(): - user, created = User.objects.get_or_create( - username=username, - defaults={ - 'first_name': first_name, - 'last_name': last_name, - 'is_staff': True, - 'is_superuser': True, - }, - ) + if not username or not password: + logger.info('ADMIN_USERNAME and ADMIN_PASSWORD are required; no admin was created.') + return - if created: - user.set_password(password) - else: - user.first_name = first_name - user.last_name = last_name - user.is_staff = True - user.is_superuser = True + with transaction.atomic(): + user, created = User.objects.get_or_create( + username=username, + defaults={ + 'first_name': first_name, + 'last_name': last_name, + 'is_staff': True, + 'is_superuser': True, + }, + ) - user.save() + if created: + user.set_password(password) + else: + user.first_name = first_name + user.last_name = last_name + user.is_staff = True + user.is_superuser = True - action = 'created' if created else 'synchronized' - logger.info(f'Admin user "{username}" {action}.') + user.save() + + action = 'created' if created else 'synchronized' + logger.info(f'Admin user "{username}" {action}.') + + except Exception as e: + # Don't fail the entire Django startup if there's an error + logger.error(f'Error creating admin user: {e}') + + # Start the superuser creation in a separate thread to avoid the warning + thread = threading.Thread(target=create_superuser_if_needed, daemon=True) + thread.start() - except Exception as e: - # Don't fail the entire Django startup if there's an error - logger.error(f'Error creating admin user: {e}')