Merge pull request #241 from makeplane/feat/api_token_workspace

feat: segregate api token workspace
This commit is contained in:
pablohashescobar 2023-02-07 01:13:55 +05:30 committed by GitHub
commit d256472776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -15,12 +15,16 @@ from plane.api.serializers import APITokenSerializer
class ApiTokenEndpoint(BaseAPIView): class ApiTokenEndpoint(BaseAPIView):
def post(self, request): def post(self, request):
try: try:
label = request.data.get("label", str(uuid4().hex)) 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( api_token = APIToken.objects.create(
label=label, label=label, user=request.user, workspace_id=workspace
user=request.user,
) )
serializer = APITokenSerializer(api_token) serializer = APITokenSerializer(api_token)

View File

@ -17,7 +17,6 @@ def generate_token():
class APIToken(BaseModel): class APIToken(BaseModel):
token = models.CharField(max_length=255, unique=True, default=generate_token) token = models.CharField(max_length=255, unique=True, default=generate_token)
label = models.CharField(max_length=255, default=generate_label_token) label = models.CharField(max_length=255, default=generate_label_token)
user = models.ForeignKey( user = models.ForeignKey(
@ -28,6 +27,9 @@ class APIToken(BaseModel):
user_type = models.PositiveSmallIntegerField( user_type = models.PositiveSmallIntegerField(
choices=((0, "Human"), (1, "Bot")), default=0 choices=((0, "Human"), (1, "Bot")), default=0
) )
workspace = models.ForeignKey(
"db.Workspace", related_name="api_tokens", on_delete=models.CASCADE, null=True
)
class Meta: class Meta:
verbose_name = "API Token" verbose_name = "API Token"