From be9eec11ddd2ea18fbc04b82d78d53824c5d3c8d Mon Sep 17 00:00:00 2001 From: gurusainath Date: Tue, 3 Dec 2024 14:39:53 +0530 Subject: [PATCH 1/2] chore: updating the workspace slug when we the delete the workspace --- apiserver/plane/app/views/workspace/base.py | 24 +++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/apiserver/plane/app/views/workspace/base.py b/apiserver/plane/app/views/workspace/base.py index 515a3479bbd..38ef1b87535 100644 --- a/apiserver/plane/app/views/workspace/base.py +++ b/apiserver/plane/app/views/workspace/base.py @@ -41,6 +41,7 @@ from plane.utils.constants import RESTRICTED_WORKSPACE_SLUGS from plane.license.utils.instance_value import get_configuration_value + class WorkSpaceViewSet(BaseViewSet): model = Workspace serializer_class = WorkSpaceSerializer @@ -81,12 +82,12 @@ def get_queryset(self): def create(self, request): try: - DISABLE_WORKSPACE_CREATION, = get_configuration_value( + (DISABLE_WORKSPACE_CREATION,) = get_configuration_value( [ { "key": "DISABLE_WORKSPACE_CREATION", "default": os.environ.get("DISABLE_WORKSPACE_CREATION", "0"), - }, + } ] ) @@ -144,8 +145,23 @@ def partial_update(self, request, *args, **kwargs): return super().partial_update(request, *args, **kwargs) @allow_permission([ROLE.ADMIN], level="WORKSPACE") - def destroy(self, request, *args, **kwargs): - return super().destroy(request, *args, **kwargs) + def destroy(self, request, slug): + workspace = Workspace.objects.get(slug=slug) + if workspace is None: + return Response( + {"error": "Workspace not found"}, status=status.HTTP_404_NOT_FOUND + ) + + # Trash the workspace by appending the epoch and `trash` to the slug + epoch = int(timezone.now().timestamp()) + updated_workspace_slug = f"trash-{epoch}-{workspace.slug}" + if len(updated_workspace_slug) > 48: + updated_workspace_slug = updated_workspace_slug[:48] + + workspace.slug = updated_workspace_slug + workspace.save() + workspace.delete() + return Response(status=status.HTTP_204_NO_CONTENT) class UserWorkSpacesEndpoint(BaseAPIView): From f842b732277ebec285be3ce7f89fd61674b05eb2 Mon Sep 17 00:00:00 2001 From: gurusainath Date: Tue, 3 Dec 2024 14:56:28 +0530 Subject: [PATCH 2/2] chore: updated try catch --- apiserver/plane/app/views/workspace/base.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apiserver/plane/app/views/workspace/base.py b/apiserver/plane/app/views/workspace/base.py index 38ef1b87535..5493cf195b9 100644 --- a/apiserver/plane/app/views/workspace/base.py +++ b/apiserver/plane/app/views/workspace/base.py @@ -146,8 +146,9 @@ def partial_update(self, request, *args, **kwargs): @allow_permission([ROLE.ADMIN], level="WORKSPACE") def destroy(self, request, slug): - workspace = Workspace.objects.get(slug=slug) - if workspace is None: + try: + workspace = Workspace.objects.get(slug=slug) + except Workspace.DoesNotExist: return Response( {"error": "Workspace not found"}, status=status.HTTP_404_NOT_FOUND )