test
This commit is contained in:
parent
8f324d99f5
commit
a84edb734a
@ -1,6 +1,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
from django.core.management import execute_from_command_line
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -15,52 +16,65 @@ class UsersConfig(AppConfig):
|
|||||||
|
|
||||||
# Only run when the server is actually starting
|
# Only run when the server is actually starting
|
||||||
if 'runserver' in sys.argv:
|
if 'runserver' in sys.argv:
|
||||||
try:
|
# Defer the superuser creation to avoid database access during app initialization
|
||||||
from .models import User
|
# This prevents the Django warning about accessing database during app initialization
|
||||||
from decouple import config
|
import threading
|
||||||
from django.db import transaction
|
import time
|
||||||
|
|
||||||
# Check if superuser already exists
|
def create_superuser_if_needed():
|
||||||
existing_superusers = User.objects.filter(is_superuser=True)
|
# Wait a bit to ensure Django is fully ready
|
||||||
logger.info(f"Found {existing_superusers.count()} existing superusers")
|
time.sleep(0.1)
|
||||||
|
try:
|
||||||
if existing_superusers.exists():
|
from .models import User
|
||||||
logger.info('A superuser already exists; no admin user was created.')
|
from decouple import config
|
||||||
return
|
from django.db import transaction
|
||||||
|
|
||||||
username = config('ADMIN_USERNAME', default='').strip()
|
# Check if superuser already exists
|
||||||
password = config('ADMIN_PASSWORD', default='')
|
existing_superusers = User.objects.filter(is_superuser=True)
|
||||||
first_name = config('ADMIN_FIRST_NAME', default='').strip()
|
logger.info(f"Found {existing_superusers.count()} existing superusers")
|
||||||
last_name = config('ADMIN_LAST_NAME', default='').strip()
|
|
||||||
|
if existing_superusers.exists():
|
||||||
|
logger.info('A superuser already exists; no admin user was created.')
|
||||||
|
return
|
||||||
|
|
||||||
if not username or not password:
|
username = config('ADMIN_USERNAME', default='').strip()
|
||||||
logger.info('ADMIN_USERNAME and ADMIN_PASSWORD are required; no admin was created.')
|
password = config('ADMIN_PASSWORD', default='')
|
||||||
return
|
first_name = config('ADMIN_FIRST_NAME', default='').strip()
|
||||||
|
last_name = config('ADMIN_LAST_NAME', default='').strip()
|
||||||
|
|
||||||
with transaction.atomic():
|
if not username or not password:
|
||||||
user, created = User.objects.get_or_create(
|
logger.info('ADMIN_USERNAME and ADMIN_PASSWORD are required; no admin was created.')
|
||||||
username=username,
|
return
|
||||||
defaults={
|
|
||||||
'first_name': first_name,
|
|
||||||
'last_name': last_name,
|
|
||||||
'is_staff': True,
|
|
||||||
'is_superuser': True,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
if created:
|
with transaction.atomic():
|
||||||
user.set_password(password)
|
user, created = User.objects.get_or_create(
|
||||||
else:
|
username=username,
|
||||||
user.first_name = first_name
|
defaults={
|
||||||
user.last_name = last_name
|
'first_name': first_name,
|
||||||
user.is_staff = True
|
'last_name': last_name,
|
||||||
user.is_superuser = True
|
'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'
|
user.save()
|
||||||
logger.info(f'Admin user "{username}" {action}.')
|
|
||||||
|
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}')
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user