From 88754e6fc0ee6d75bbedfe1436ced99a1dfcc8e7 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Wed, 15 Mar 2023 23:25:23 +0530 Subject: [PATCH] dev: separate endpoints for workspace assets and user assets (#420) --- apiserver/plane/api/urls.py | 17 ++++++-- apiserver/plane/api/views/__init__.py | 2 +- apiserver/plane/api/views/asset.py | 57 +++++++++++++++++++++++++-- apiserver/plane/db/models/asset.py | 8 +++- 4 files changed, 75 insertions(+), 9 deletions(-) diff --git a/apiserver/plane/api/urls.py b/apiserver/plane/api/urls.py index f9b99f181..febe3f2c0 100644 --- a/apiserver/plane/api/urls.py +++ b/apiserver/plane/api/urls.py @@ -43,6 +43,7 @@ from plane.api.views import ( ## End Workspaces # File Assets FileAssetEndpoint, + UserAssetsEndpoint, ## End File Assets # Projects ProjectViewSet, @@ -787,12 +788,22 @@ urlpatterns = [ path( "workspaces//file-assets/", FileAssetEndpoint.as_view(), - name="File Assets", + name="file-assets", ), path( - "workspaces//file-assets//", + "workspaces/file-assets///", FileAssetEndpoint.as_view(), - name="File Assets", + name="file-assets", + ), + path( + "users/file-assets/", + UserAssetsEndpoint.as_view(), + name="user-file-assets", + ), + path( + "users/file-assets/user-profile//", + UserAssetsEndpoint.as_view(), + name="user-file-assets", ), ## End File Assets ## Modules diff --git a/apiserver/plane/api/views/__init__.py b/apiserver/plane/api/views/__init__.py index 8a95d69f2..1943511d3 100644 --- a/apiserver/plane/api/views/__init__.py +++ b/apiserver/plane/api/views/__init__.py @@ -51,7 +51,7 @@ from .cycle import ( CycleFavoriteViewSet, DraftCyclesEndpoint, ) -from .asset import FileAssetEndpoint +from .asset import FileAssetEndpoint, UserAssetsEndpoint from .issue import ( IssueViewSet, WorkSpaceIssuesEndpoint, diff --git a/apiserver/plane/api/views/asset.py b/apiserver/plane/api/views/asset.py index e5af2c080..be464c2d4 100644 --- a/apiserver/plane/api/views/asset.py +++ b/apiserver/plane/api/views/asset.py @@ -17,8 +17,9 @@ class FileAssetEndpoint(BaseAPIView): A viewset for viewing and editing task instances. """ - def get(self, request, slug): - files = FileAsset.objects.filter(workspace__slug=slug) + def get(self, request, workspace_id, asset_key): + asset_key = str(workspace_id) + "/" + asset_key + files = FileAsset.objects.filter(asset=asset_key) serializer = FileAssetSerializer(files, context={"request": request}, many=True) return Response(serializer.data) @@ -42,9 +43,57 @@ class FileAssetEndpoint(BaseAPIView): status=status.HTTP_400_BAD_REQUEST, ) - def delete(self, request, slug, pk): + def delete(self, request, workspace_id, asset_key): try: - file_asset = FileAsset.objects.get(pk=pk, workspace__slug=slug) + asset_key = str(workspace_id) + "/" + asset_key + file_asset = FileAsset.objects.get(asset=asset_key) + # Delete the file from storage + file_asset.asset.delete(save=False) + # Delete the file object + file_asset.delete() + return Response(status=status.HTTP_204_NO_CONTENT) + except FileAsset.DoesNotExist: + return Response( + {"error": "File Asset doesn't exist"}, status=status.HTTP_404_NOT_FOUND + ) + except Exception as e: + capture_exception(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_400_BAD_REQUEST, + ) + + +class UserAssetsEndpoint(BaseAPIView): + def get(self, request, asset_key): + try: + files = FileAsset.objects.filter(asset=asset_key, created_by=request.user) + serializer = FileAssetSerializer(files, context={"request": request}) + return Response(serializer.data) + except FileAsset.DoesNotExist: + return Response( + {"error": "File Asset does not exist"}, status=status.HTTP_404_NOT_FOUND + ) + + def post(self, request): + try: + serializer = FileAssetSerializer(data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + except Exception as e: + capture_exception(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_400_BAD_REQUEST, + ) + + def delete(self, request, asset_key): + try: + asset_key = "user-profile" + "/" + asset_key + print(asset_key) + file_asset = FileAsset.objects.get(asset=asset_key, created_by=request.user) # Delete the file from storage file_asset.asset.delete(save=False) # Delete the file object diff --git a/apiserver/plane/db/models/asset.py b/apiserver/plane/db/models/asset.py index ff33dc9e0..eba8beefc 100644 --- a/apiserver/plane/db/models/asset.py +++ b/apiserver/plane/db/models/asset.py @@ -1,3 +1,6 @@ +# Python imports +from uuid import uuid4 + # Django import from django.db import models from django.core.exceptions import ValidationError @@ -7,7 +10,9 @@ from . import BaseModel def get_upload_path(instance, filename): - return f"{instance.workspace.id}/{filename}" + if instance.workspace_id is not None: + return f"{instance.workspace.id}/{uuid4().hex}-{filename}" + return f"user-profile/{uuid4().hex}-{filename}" def file_size(value): @@ -15,6 +20,7 @@ def file_size(value): if value.size > limit: raise ValidationError("File too large. Size should not exceed 5 MB.") + class FileAsset(BaseModel): """ A file asset.