Skip to content

Commit

Permalink
fix: correct image deletion functionality (#19)
Browse files Browse the repository at this point in the history
* fix: remove code to delete files in course assets

* fix: synchronize course assets with images in the block

* feat: sync course assets when inside edit view

* docs: update changelog for latest changes and bumping the version
  • Loading branch information
BryanttV authored Nov 16, 2023
1 parent 0503872 commit de02ab0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ Unreleased

*

0.4.2 - 2023-11-16
**********************************************

Changed
=======

* Update documentation
* Correct image deletion functionality

0.4.1 - 2023-10-24
**********************************************

Expand Down
2 changes: 1 addition & 1 deletion imagesgallery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

from .imagesgallery import ImagesGalleryXBlock

__version__ = '0.4.1'
__version__ = '0.4.2'
52 changes: 39 additions & 13 deletions imagesgallery/imagesgallery.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""TO-DO: Write a description of what this XBlock is."""
from __future__ import annotations

import logging
import os
Expand All @@ -10,19 +11,17 @@
from django.utils import translation
from webob.response import Response
from xblock.core import XBlock
from xblock.fields import Scope, List
from xblock.fields import List, Scope
from xblock.fragment import Fragment
from xblock.reference.user_service import XBlockUser
from xblockutils.resources import ResourceLoader


try:
from cms.djangoapps.contentstore.exceptions import AssetNotFoundException
from opaque_keys.edx.keys import AssetKey
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from xmodule.contentstore.content import StaticContent
from xmodule.contentstore.django import contentstore
except ImportError:
AssetNotFoundException = None
configuration_helpers = None
StaticContent = None
contentstore = None
Expand Down Expand Up @@ -51,8 +50,10 @@
}
]
}
ATTR_KEY_ANONYMOUS_USER_ID = 'edx-platform.anonymous_user_id'


@XBlock.wants("user")
class ImagesGalleryXBlock(XBlock):
"""XBlock for displaying a gallery of images."""

Expand All @@ -68,6 +69,12 @@ class ImagesGalleryXBlock(XBlock):
scope=Scope.settings,
)

def get_current_user(self) -> XBlockUser:
"""
Get the current user.
"""
return self.runtime.service(self, "user").get_current_user()

@property
def block_id(self):
"""
Expand Down Expand Up @@ -263,6 +270,10 @@ def update_contents(self, content):
@XBlock.json_handler
def get_files(self, data, suffix=''): # pylint: disable=unused-argument
"""Handler for getting images from the course assets."""
# When inside the component edit view where there is no anonymous user ID, synchronize the assets.
if not self.get_current_user().opt_attrs.get(ATTR_KEY_ANONYMOUS_USER_ID):
self.sync_course_assets()

paginated_contents = self.get_paginated_contents(
current_page=int(data.get("current_page", 0)),
page_size=int(data.get("page_size", 10)),
Expand All @@ -275,19 +286,10 @@ def get_files(self, data, suffix=''): # pylint: disable=unused-argument
@XBlock.json_handler
def remove_files(self, data, suffix=''): # pylint: disable=unused-argument
"""Handler for removing images from the course assets."""
try:
from cms.djangoapps.contentstore.views.assets import delete_asset # pylint: disable=import-outside-toplevel
except ImportError:
from cms.djangoapps.contentstore.asset_storage_handler import delete_asset # pylint: disable=import-outside-toplevel

assets = data.get("assets")

for asset_key_id in assets:
asset_key = AssetKey.from_string(asset_key_id)
try:
delete_asset(self.course_id, asset_key)
except AssetNotFoundException as e:
log.exception(e)

for content in self.contents:
if content["asset_key"] == str(asset_key):
Expand Down Expand Up @@ -326,6 +328,30 @@ def get_paginated_contents(self, current_page=0, page_size=10):
"""
return self.contents[current_page * page_size: (current_page + 1) * page_size]

def sync_course_assets(self) -> None:
"""
Synchronize images according to the course assets.
This method does the following:
- Get all course assets id.
- Remove the images that are not in the course assets.
"""
course_assets_id = self.get_all_course_assets_id()

for content in self.contents:
if content["id"] not in course_assets_id:
self.contents.remove(content)
self.content_names.remove(content["display_name"])

def get_all_course_assets_id(self) -> list[str]:
"""Return all course assets id.
Returns:
list[str]: List of all course assets id.
"""
course_assets, _ = contentstore().get_all_content_for_course(self.course_id)
return [asset["_id"] for asset in course_assets]

def _get_assets_for_page(self, course_key, options):
"""Return course content for given course and options."""
current_page = options["current_page"]
Expand Down

0 comments on commit de02ab0

Please sign in to comment.