diff --git a/backend/users/apps.py b/backend/users/apps.py index 6aa7f3c..65d4e0a 100644 --- a/backend/users/apps.py +++ b/backend/users/apps.py @@ -1,6 +1,9 @@ import sys +import logging from django.apps import AppConfig +logger = logging.getLogger(__name__) + class UsersConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'users' @@ -17,10 +20,12 @@ class UsersConfig(AppConfig): from decouple import config from django.db import transaction - # Don't create/synchronize the configured admin if another - # superuser already exists. - if User.objects.filter(is_superuser=True).exists(): - print('A superuser already exists; no admin user was created.') + # 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 username = config('ADMIN_USERNAME', default='').strip() @@ -29,7 +34,7 @@ class UsersConfig(AppConfig): last_name = config('ADMIN_LAST_NAME', default='').strip() if not username or not password: - print('ADMIN_USERNAME and ADMIN_PASSWORD are required; no admin was created.') + logger.info('ADMIN_USERNAME and ADMIN_PASSWORD are required; no admin was created.') return with transaction.atomic(): @@ -54,8 +59,8 @@ class UsersConfig(AppConfig): user.save() action = 'created' if created else 'synchronized' - print(f'Admin user "{username}" {action}.') + logger.info(f'Admin user "{username}" {action}.') except Exception as e: # Don't fail the entire Django startup if there's an error - print(f'Error creating admin user: {e}') + logger.error(f'Error creating admin user: {e}')