From b0c1e3e14f4dfea55aa63d541676a0f380264db1 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sat, 6 Jun 2020 13:46:56 +0200 Subject: [PATCH] Add admin command to create AdminProfile for new superuser --- core/management/commands/makeadmin.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 core/management/commands/makeadmin.py diff --git a/core/management/commands/makeadmin.py b/core/management/commands/makeadmin.py new file mode 100644 index 0000000..0a32bf8 --- /dev/null +++ b/core/management/commands/makeadmin.py @@ -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) \ No newline at end of file