expephalon/core/management/commands/makeadmin.py

22 lines
786 B
Python
Raw Normal View History

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)