From 0b7f0640c9cc80eaa6592d4ba41034e781eb5b3a Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Mon, 6 Feb 2023 20:22:08 +0530 Subject: [PATCH] feat: segregate api token workspace --- apiserver/plane/api/views/api_token.py | 10 +++++++--- apiserver/plane/db/models/api_token.py | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apiserver/plane/api/views/api_token.py b/apiserver/plane/api/views/api_token.py index 4ed3d9de0..209fa2401 100644 --- a/apiserver/plane/api/views/api_token.py +++ b/apiserver/plane/api/views/api_token.py @@ -15,12 +15,16 @@ from plane.api.serializers import APITokenSerializer class ApiTokenEndpoint(BaseAPIView): def post(self, request): try: - label = request.data.get("label", str(uuid4().hex)) + workspace = request.data.get("workspace", False) + + if not workspace: + return Response( + {"error": "Workspace is required"}, status=status.HTTP_200_OK + ) api_token = APIToken.objects.create( - label=label, - user=request.user, + label=label, user=request.user, workspace=workspace ) serializer = APITokenSerializer(api_token) diff --git a/apiserver/plane/db/models/api_token.py b/apiserver/plane/db/models/api_token.py index 32ba013bc..1b0dda55e 100644 --- a/apiserver/plane/db/models/api_token.py +++ b/apiserver/plane/db/models/api_token.py @@ -17,7 +17,6 @@ def generate_token(): class APIToken(BaseModel): - token = models.CharField(max_length=255, unique=True, default=generate_token) label = models.CharField(max_length=255, default=generate_label_token) user = models.ForeignKey( @@ -28,6 +27,9 @@ class APIToken(BaseModel): user_type = models.PositiveSmallIntegerField( choices=((0, "Human"), (1, "Bot")), default=0 ) + workspace = models.ForeignKey( + "db.workspace", related_name="api_tokens", on_delete=models.SET_NULL, null=True + ) class Meta: verbose_name = "API Token"