Source code for intranet.apps.auth.management.commands.grant_admin
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from ....groups.models import Group
[docs]class Command(BaseCommand):
help = "Adds the specified user to the specified admin group"
[docs] def add_arguments(self, parser):
parser.add_argument("username")
parser.add_argument("admin_group")
[docs] def handle(self, *args, **options):
g = Group.objects.get_or_create(name=f"admin_{options['admin_group']}")[0]
get_user_model().objects.get_or_create(username=options["username"])[0].groups.add(g)
self.stdout.write(f"Added {options['username']} to {options['admin_group']}")