forked from github/plane
dev: separate endpoints for workspace assets and user assets (#420)
This commit is contained in:
parent
b6ee197b40
commit
88754e6fc0
@ -43,6 +43,7 @@ from plane.api.views import (
|
|||||||
## End Workspaces
|
## End Workspaces
|
||||||
# File Assets
|
# File Assets
|
||||||
FileAssetEndpoint,
|
FileAssetEndpoint,
|
||||||
|
UserAssetsEndpoint,
|
||||||
## End File Assets
|
## End File Assets
|
||||||
# Projects
|
# Projects
|
||||||
ProjectViewSet,
|
ProjectViewSet,
|
||||||
@ -787,12 +788,22 @@ urlpatterns = [
|
|||||||
path(
|
path(
|
||||||
"workspaces/<str:slug>/file-assets/",
|
"workspaces/<str:slug>/file-assets/",
|
||||||
FileAssetEndpoint.as_view(),
|
FileAssetEndpoint.as_view(),
|
||||||
name="File Assets",
|
name="file-assets",
|
||||||
),
|
),
|
||||||
path(
|
path(
|
||||||
"workspaces/<str:slug>/file-assets/<uuid:pk>/",
|
"workspaces/file-assets/<uuid:workspace_id>/<str:asset_key>/",
|
||||||
FileAssetEndpoint.as_view(),
|
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/<str:asset_key>/",
|
||||||
|
UserAssetsEndpoint.as_view(),
|
||||||
|
name="user-file-assets",
|
||||||
),
|
),
|
||||||
## End File Assets
|
## End File Assets
|
||||||
## Modules
|
## Modules
|
||||||
|
@ -51,7 +51,7 @@ from .cycle import (
|
|||||||
CycleFavoriteViewSet,
|
CycleFavoriteViewSet,
|
||||||
DraftCyclesEndpoint,
|
DraftCyclesEndpoint,
|
||||||
)
|
)
|
||||||
from .asset import FileAssetEndpoint
|
from .asset import FileAssetEndpoint, UserAssetsEndpoint
|
||||||
from .issue import (
|
from .issue import (
|
||||||
IssueViewSet,
|
IssueViewSet,
|
||||||
WorkSpaceIssuesEndpoint,
|
WorkSpaceIssuesEndpoint,
|
||||||
|
@ -17,8 +17,9 @@ class FileAssetEndpoint(BaseAPIView):
|
|||||||
A viewset for viewing and editing task instances.
|
A viewset for viewing and editing task instances.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def get(self, request, slug):
|
def get(self, request, workspace_id, asset_key):
|
||||||
files = FileAsset.objects.filter(workspace__slug=slug)
|
asset_key = str(workspace_id) + "/" + asset_key
|
||||||
|
files = FileAsset.objects.filter(asset=asset_key)
|
||||||
serializer = FileAssetSerializer(files, context={"request": request}, many=True)
|
serializer = FileAssetSerializer(files, context={"request": request}, many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
@ -42,9 +43,57 @@ class FileAssetEndpoint(BaseAPIView):
|
|||||||
status=status.HTTP_400_BAD_REQUEST,
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
)
|
)
|
||||||
|
|
||||||
def delete(self, request, slug, pk):
|
def delete(self, request, workspace_id, asset_key):
|
||||||
try:
|
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
|
# Delete the file from storage
|
||||||
file_asset.asset.delete(save=False)
|
file_asset.asset.delete(save=False)
|
||||||
# Delete the file object
|
# Delete the file object
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
# Python imports
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
# Django import
|
# Django import
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
@ -7,7 +10,9 @@ from . import BaseModel
|
|||||||
|
|
||||||
|
|
||||||
def get_upload_path(instance, filename):
|
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):
|
def file_size(value):
|
||||||
@ -15,6 +20,7 @@ def file_size(value):
|
|||||||
if value.size > limit:
|
if value.size > limit:
|
||||||
raise ValidationError("File too large. Size should not exceed 5 MB.")
|
raise ValidationError("File too large. Size should not exceed 5 MB.")
|
||||||
|
|
||||||
|
|
||||||
class FileAsset(BaseModel):
|
class FileAsset(BaseModel):
|
||||||
"""
|
"""
|
||||||
A file asset.
|
A file asset.
|
||||||
|
Loading…
Reference in New Issue
Block a user