jira/apps/api/plane/db/management/commands/create_instance_admin.py

44 lines
1.4 KiB
Python
Raw Permalink Normal View History

# Copyright (c) 2023-present Plane Software, Inc. and contributors
# SPDX-License-Identifier: AGPL-3.0-only
# See the LICENSE file for details.
2024-04-16 12:36:46 +00:00
# Django imports
from django.core.management.base import BaseCommand, CommandError
# Module imports
from plane.license.models import Instance, InstanceAdmin
from plane.db.models import User
class Command(BaseCommand):
help = "Add a new instance admin"
def add_arguments(self, parser):
# Positional argument
2024-11-21 12:12:44 +00:00
parser.add_argument("admin_email", type=str, help="Instance Admin Email")
2024-04-16 12:36:46 +00:00
def handle(self, *args, **options):
admin_email = options.get("admin_email", False)
if not admin_email:
raise CommandError("Please provide the email of the admin.")
user = User.objects.filter(email=admin_email).first()
if user is None:
raise CommandError("User with the provided email does not exist.")
try:
# Get the instance
instance = Instance.objects.last()
# Get or create an instance admin
_, created = InstanceAdmin.objects.get_or_create(user=user, instance=instance, role=20)
2024-04-16 12:36:46 +00:00
if not created:
2024-11-21 12:12:44 +00:00
raise CommandError("The provided email is already an instance admin.")
2024-04-16 12:36:46 +00:00
2024-11-21 12:12:44 +00:00
self.stdout.write(self.style.SUCCESS("Successfully created the admin"))
2024-04-16 12:36:46 +00:00
except Exception as e:
print(e)
raise CommandError("Failed to create the instance admin.")