feat: delete file assets from storage (#373)

This commit is contained in:
pablohashescobar 2023-03-06 18:56:05 +05:30 committed by GitHub
parent 3a81a6c186
commit cecd025a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -689,6 +689,11 @@ urlpatterns = [
FileAssetEndpoint.as_view(), FileAssetEndpoint.as_view(),
name="File Assets", name="File Assets",
), ),
path(
"workspaces/<str:slug>/file-assets/<uuid:pk>/",
FileAssetEndpoint.as_view(),
name="File Assets",
),
## End File Assets ## End File Assets
## Modules ## Modules
path( path(

View File

@ -11,7 +11,6 @@ from plane.api.serializers import FileAssetSerializer
class FileAssetEndpoint(BaseAPIView): class FileAssetEndpoint(BaseAPIView):
parser_classes = (MultiPartParser, FormParser) parser_classes = (MultiPartParser, FormParser)
""" """
@ -27,7 +26,6 @@ class FileAssetEndpoint(BaseAPIView):
try: try:
serializer = FileAssetSerializer(data=request.data) serializer = FileAssetSerializer(data=request.data)
if serializer.is_valid(): if serializer.is_valid():
if request.user.last_workspace_id is None: if request.user.last_workspace_id is None:
return Response( return Response(
{"error": "Workspace id is required"}, {"error": "Workspace id is required"},
@ -43,3 +41,22 @@ class FileAssetEndpoint(BaseAPIView):
{"error": "Something went wrong please try again later"}, {"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST, status=status.HTTP_400_BAD_REQUEST,
) )
def delete(self, request, slug, pk):
try:
file_asset = FileAsset.objects.get(pk=pk, workspace__slug=slug)
# 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,
)