Add admin command to create AdminProfile for new superuser
This commit is contained in:
parent
f2ff66dccb
commit
b0c1e3e14f
1 changed files with 22 additions and 0 deletions
22
core/management/commands/makeadmin.py
Normal file
22
core/management/commands/makeadmin.py
Normal 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)
|
Loading…
Reference in a new issue