Skip to content

create_api_user#

Command#

Bases: BaseCommand

Source code in src/apps/users/management/commands/create_api_user.py
class Command(BaseCommand):
    def add_arguments(self, parser: ArgumentParser):
        parser.add_argument("username", type=str)
        parser.add_argument(
            "--groups",
            "-g",
            nargs="+",
            type=str,
            help="List of groups to add user to",
        )
        parser.add_argument(
            "--token-override",
            action="store_true",
            required=False,
            default=False,
            help="Override authentication token with value from AUTH_TOKEN_VALUE env-var",
        )

    def handle(self, *args, **options):
        with transaction.atomic():
            User = get_user_model()
            user, created = User.objects.get_or_create(username=options["username"])
            groups = options.get("groups")
            token_override = options.get("token_override")

            if groups:
                user.groups.clear()
                for group in groups:
                    obj, created = Group.objects.get_or_create(name=group)
                    user.groups.add(obj)
            if token_override:
                Token.objects.filter(user=user).delete()
                token_value = os.environ.get("AUTH_TOKEN_VALUE")
                Token.objects.create(user=user, key=token_value)