test
This commit is contained in:
parent
8f324d99f5
commit
a84edb734a
@ -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")
|
||||
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
|
||||
|
||||
if existing_superusers.exists():
|
||||
logger.info('A superuser already exists; no admin user was created.')
|
||||
return
|
||||
# Check if superuser already exists
|
||||
existing_superusers = User.objects.filter(is_superuser=True)
|
||||
logger.info(f"Found {existing_superusers.count()} existing superusers")
|
||||
|
||||
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()
|
||||
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}')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user