diff --git a/static/js/components/RepeatableContentListing.tsx b/static/js/components/RepeatableContentListing.tsx index 33b4bbdca..380c04267 100644 --- a/static/js/components/RepeatableContentListing.tsx +++ b/static/js/components/RepeatableContentListing.tsx @@ -270,23 +270,26 @@ export default function RepeatableContentListing(props: { - {listing.results.map((item: WebsiteContentListItem) => ( - - ))} + {listing.results.map((item: WebsiteContentListItem) => { + const showDelete = isDeletable && item.is_deletable_by_resourcetype + return ( + + ) + })} ({ type: casual.word, updated_on: randomISO8601(), is_deletable: true, + is_deletable_by_resourcetype: true, }) export const makeWebsiteContentDetail = (): WebsiteContent => ({ diff --git a/websites/serializers.py b/websites/serializers.py index 372b58348..6ffa24a2f 100644 --- a/websites/serializers.py +++ b/websites/serializers.py @@ -476,6 +476,7 @@ class WebsiteContentSerializer(serializers.ModelSerializer): website_name = serializers.CharField(source="website.name") is_deletable = serializers.SerializerMethodField() + is_deletable_by_resourcetype = serializers.SerializerMethodField() def get_is_deletable(self, obj): request = self.context.get("request", None) @@ -488,6 +489,14 @@ def get_is_deletable(self, obj): return len(refs) == 0 return True + def get_is_deletable_by_resourcetype(self, obj): + # if not a resource, always OK, because we're using config var to + # control deletable content types + if obj.type != CONTENT_TYPE_RESOURCE: + return True + # for resources only, check metadata.resourcetype === "Video" + return (obj.metadata or {}).get("resourcetype") == "Video" + class Meta: model = WebsiteContent read_only_fields = [ @@ -497,6 +506,7 @@ class Meta: "type", "updated_on", "is_deletable", + "is_deletable_by_resourcetype", ] # See WebsiteContentCreateSerializer below for creating new WebsiteContent objects # noqa: E501 fields = read_only_fields @@ -620,9 +630,22 @@ def to_representation(self, instance): result[file_field["name"]] = instance.file.url return result + is_deletable_by_resourcetype = serializers.SerializerMethodField() + + def get_is_deletable_by_resourcetype(self, obj): + if obj.type != CONTENT_TYPE_RESOURCE: + return True + return (obj.metadata or {}).get("resourcetype") == "Video" + class Meta: model = WebsiteContent - read_only_fields = ["text_id", "type", "content_context", "url_path"] + read_only_fields = [ + "text_id", + "type", + "content_context", + "url_path", + "is_deletable_by_resourcetype", + ] fields = [ *read_only_fields, "title", diff --git a/websites/views.py b/websites/views.py index 6e4f1b810..9e92e18dd 100644 --- a/websites/views.py +++ b/websites/views.py @@ -28,6 +28,7 @@ from content_sync.constants import VERSION_DRAFT, VERSION_LIVE from content_sync.tasks import update_mass_build_pipelines_on_publish from gdrive_sync.constants import WebsiteSyncStatus +from gdrive_sync.models import DriveFile from gdrive_sync.tasks import import_website_files from main import features from main.permissions import ReadonlyPermission @@ -692,6 +693,44 @@ def get_serializer_context(self): ) return {**super().get_serializer_context(), **added_context} + def destroy(self, request, *args, **kwargs): + content: WebsiteContent = self.get_object() + + is_video = ( + content.type == "resource" + and (content.metadata or {}).get("resourcetype") == RESOURCE_TYPE_VIDEO + ) + if not is_video: + return super().destroy(request, *args, **kwargs) + + drive_file = DriveFile.objects.filter(resource=content).first() + # print("drive file") # noqa: ERA001 + # print("drive file") # noqa: ERA001 + # print("drive file") # noqa: ERA001 + # print(drive_file) # noqa: ERA001 + video = drive_file.video if drive_file else None + # print("Vide") # noqa: ERA001 + # print("Vide") # noqa: ERA001 + # print("Vide") # noqa: ERA001 + # print(video) # noqa: ERA001 + + # content.updated_by = request.user # noqa: ERA001 + # super().perform_destroy(content) # noqa: ERA001 + + # if drive_file and drive_file.file_id: + # ds = get_drive_service() # noqa: ERA001 + # ds.files().delete(fileId=drive_file.file_id).execute() # noqa: ERA001 + + if video: + video.delete() + + # api.delete_drive_file(drive_file, sync_datetime=now_in_utc()) # noqa: ERA001 + # (drivefile pre_delete ..S3 cleanup) + # drive_file.delete() # noqa: ERA001 # noqa: ERA001 + + update_website_backend(content.website) + return Response(status=status.HTTP_204_NO_CONTENT) + def perform_destroy(self, instance: WebsiteContent): """(soft) deletes a WebsiteContent record""" instance.updated_by = self.request.user