Add admin command to create AdminProfile for new superuser

This commit is contained in:
Kumi 2020-06-06 13:46:56 +02:00
parent f2ff66dccb
commit b0c1e3e14f

View file

@ -0,0 +1,22 @@
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth import get_user_model
from core.models.profiles import Profile, AdminProfile
class Command(BaseCommand):
help = 'Add an admin profile to a fresh superuser'
def add_arguments(self, parser):
parser.add_argument('user', type=str)
def handle(self, *args, **options):
try:
user = get_user_model().objects.get(username=options["user"])
except get_user_model().DoesNotExist:
raise ValueError(f"User {options['user']} does not exist")
try:
user.profile
raise ValueError(f"User {options['user']} already has a profile")
except Profile.DoesNotExist:
AdminProfile.objects.create(user=user)