Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 16s
CodeQL / Analyze (python) (push) Failing after 3s
i18n sync check / check:sync (push) Failing after 3s
React Doctor / react-doctor (push) Failing after 16s
Branch Build CE / Build-Push Admin Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Admin Manifest (push) Has been cancelled
Branch Build CE / Build-Push Web Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Web Manifest (push) Has been cancelled
Branch Build CE / Build-Push Space Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Proxy Manifest (push) Has been cancelled
Branch Build CE / Merge Space Manifest (push) Has been cancelled
Branch Build CE / Prepare AIO Assets (push) Has been cancelled
Branch Build CE / Build Setup (push) Has been cancelled
Branch Build CE / Build-Push Live Collaboration Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Live Collaboration Manifest (push) Has been cancelled
Branch Build CE / Build-Push API Server Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge API Server Manifest (push) Has been cancelled
Branch Build CE / Build-Push Proxy Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Build-Push AIO Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge AIO Manifest (push) Has been cancelled
Branch Build CE / Upload Build Assets (push) Has been cancelled
Branch Build CE / Build Release (push) Has been cancelled
73 lines
2.6 KiB
Python
73 lines
2.6 KiB
Python
# Copyright (c) 2023-present Plane Software, Inc. and contributors
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# See the LICENSE file for details.
|
|
|
|
# Django imports
|
|
from typing import Any
|
|
from django.core.management import BaseCommand, CommandError
|
|
|
|
# Module imports
|
|
from plane.db.models import (
|
|
User,
|
|
WorkspaceMember,
|
|
ProjectMember,
|
|
Project,
|
|
ProjectUserProperty,
|
|
)
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Add a member to a project. If present in the workspace"
|
|
|
|
def add_arguments(self, parser):
|
|
# Positional argument
|
|
parser.add_argument("--project_id", type=str, nargs="?", help="Project ID")
|
|
parser.add_argument("--user_email", type=str, nargs="?", help="User Email")
|
|
parser.add_argument("--role", type=int, nargs="?", help="Role of the user in the project")
|
|
|
|
def handle(self, *args: Any, **options: Any):
|
|
try:
|
|
if not options["project_id"]:
|
|
raise CommandError("Project ID is required")
|
|
if not options["user_email"]:
|
|
raise CommandError("User Email is required")
|
|
|
|
project_id = options["project_id"]
|
|
user_email = options["user_email"]
|
|
role = options.get("role", 20)
|
|
|
|
print(f"Role: {role}")
|
|
|
|
user = User.objects.filter(email=user_email).first()
|
|
if not user:
|
|
raise CommandError("User not found")
|
|
|
|
# Check if the project exists
|
|
project = Project.objects.filter(pk=project_id).first()
|
|
if not project:
|
|
raise CommandError("Project not found")
|
|
|
|
# Check if the user exists in the workspace
|
|
if not WorkspaceMember.objects.filter(workspace=project.workspace, member=user, is_active=True).exists():
|
|
raise CommandError("User not member in workspace")
|
|
|
|
|
|
if ProjectMember.objects.filter(project=project, member=user).exists():
|
|
# Update the project member
|
|
ProjectMember.objects.filter(project=project, member=user).update(
|
|
is_active=True, role=role
|
|
)
|
|
else:
|
|
# Create the project member
|
|
ProjectMember.objects.create(project=project, member=user, role=role)
|
|
|
|
# Issue Property
|
|
ProjectUserProperty.objects.get_or_create(user=user, project=project)
|
|
|
|
# Success message
|
|
self.stdout.write(self.style.SUCCESS(f"User {user_email} added to project {project_id}"))
|
|
return
|
|
except CommandError as e:
|
|
self.stdout.write(self.style.ERROR(e))
|
|
return
|