Skip to content

Commit

Permalink
fix: file asset delete (#2804)
Browse files Browse the repository at this point in the history
  • Loading branch information
NarayanBavisetti authored and sriramveeraghanta committed Dec 7, 2023
1 parent b210fc8 commit 2f75611
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
40 changes: 19 additions & 21 deletions apiserver/plane/app/views/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,34 @@ def post(self, request, slug):
def delete(self, request, workspace_id, asset_key):
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()
file_asset.is_deleted = True
file_asset.save()
return Response(status=status.HTTP_204_NO_CONTENT)


class UserAssetsEndpoint(BaseAPIView):
parser_classes = (MultiPartParser, FormParser)

def get(self, request, asset_key):
files = FileAsset.objects.filter(asset=asset_key, created_by=request.user)
if files.exists():
serializer = FileAssetSerializer(files, context={"request": request})
return Response({"data": serializer.data, "status": True}, status=status.HTTP_200_OK)
else:
return Response({"error": "Asset key does not exist", "status": False}, status=status.HTTP_200_OK)
files = FileAsset.objects.filter(asset=asset_key, created_by=request.user)
if files.exists():
serializer = FileAssetSerializer(files, context={"request": request})
return Response({"data": serializer.data, "status": True}, status=status.HTTP_200_OK)
else:
return Response({"error": "Asset key does not exist", "status": False}, status=status.HTTP_200_OK)

def post(self, request):
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)
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)


def delete(self, request, 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
file_asset.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
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
file_asset.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
29 changes: 29 additions & 0 deletions apiserver/plane/bgtasks/file_asset_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Python imports
from datetime import timedelta

# Django imports
from django.utils import timezone
from django.db.models import Q

# Third party imports
from celery import shared_task

# Module imports
from plane.db.models import FileAsset


@shared_task
def delete_file_asset():

# file assets to delete
file_assets_to_delete = FileAsset.objects.filter(
Q(is_deleted=True) & Q(updated_at__lte=timezone.now() - timedelta(days=7))
)

# Delete the file from storage and the file object from the database
for file_asset in file_assets_to_delete:
# Delete the file from storage
file_asset.asset.delete(save=False)
# Delete the file object
file_asset.delete()

4 changes: 4 additions & 0 deletions apiserver/plane/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
"task": "plane.bgtasks.exporter_expired_task.delete_old_s3_link",
"schedule": crontab(hour=0, minute=0),
},
"check-every-day-to-delete-file-asset": {
"task": "plane.bgtasks.file_asset_task.delete_file_asset",
"schedule": crontab(hour=0, minute=0),
},
}

# Load task modules from all registered Django app configs.
Expand Down
18 changes: 18 additions & 0 deletions apiserver/plane/db/migrations/0051_fileasset_is_deleted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-11-20 08:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('db', '0050_user_use_case_alter_workspace_organization_size'),
]

operations = [
migrations.AddField(
model_name='fileasset',
name='is_deleted',
field=models.BooleanField(default=False),
),
]
1 change: 1 addition & 0 deletions apiserver/plane/db/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class FileAsset(BaseModel):
workspace = models.ForeignKey(
"db.Workspace", on_delete=models.CASCADE, null=True, related_name="assets"
)
is_deleted = models.BooleanField(default=False)

class Meta:
verbose_name = "File Asset"
Expand Down
1 change: 1 addition & 0 deletions apiserver/plane/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
CELERY_IMPORTS = (
"plane.bgtasks.issue_automation_task",
"plane.bgtasks.exporter_expired_task",
"plane.bgtasks.file_asset_task",
)

# Sentry Settings
Expand Down

0 comments on commit 2f75611

Please sign in to comment.