Illustrations/web/core/management/commands/init_users.py
2025-08-12 20:50:46 -05:00

28 lines
1.1 KiB
Python

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
import os
class Command(BaseCommand):
def handle(self, *args, **kwargs):
admin_user = os.getenv("INIT_ADMIN_USERNAME")
admin_pass = os.getenv("INIT_ADMIN_PASSWORD")
editor_user = os.getenv("INIT_EDITOR_USERNAME")
editor_pass = os.getenv("INIT_EDITOR_PASSWORD")
if admin_user and admin_pass:
u, created = User.objects.get_or_create(username=admin_user)
u.is_staff = True
u.is_superuser = True
if admin_pass:
u.set_password(admin_pass)
u.save()
self.stdout.write(self.style.SUCCESS(f"Admin ready: {admin_user}"))
if editor_user and editor_pass:
u, created = User.objects.get_or_create(username=editor_user)
u.is_staff = False
u.is_superuser = False
if editor_pass:
u.set_password(editor_pass)
u.save()
self.stdout.write(self.style.SUCCESS(f"Editor ready: {editor_user}"))