Skip to content

Commit

Permalink
feat: save image names to keep track of unicity (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariajgrimaldi authored Oct 18, 2023
1 parent b13b810 commit 7aed57c
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion imagesgallery/imagesgallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class ImagesGalleryXBlock(XBlock):
scope=Scope.settings,
)

content_names = List(
display_name="Content ids of the static contents uploaded by the instructor.",
default=[],
scope=Scope.settings,
)

@property
def block_id(self):
"""
Expand Down Expand Up @@ -221,6 +227,7 @@ def file_upload(self, request, suffix=''): # pylint: disable=unused-argument
uploaded_content = []
for _, file in request.params.items():
try:
file.file._set_name(self.generate_file_name(file.file.name)) # pylint: disable=protected-access
content = update_course_run_asset(self.course_id, file.file)
uploaded_content.append(self.get_asset_json_from_content(content))
self.update_contents(content)
Expand All @@ -232,12 +239,26 @@ def file_upload(self, request, suffix=''): # pylint: disable=unused-argument
json_body=uploaded_content,
)

def generate_file_name(self, file_name):
"""Generate a new file name if the file name already exists.
Args:
file_name (str): The file name to check.
Returns:
str: The new file name.
"""
if file_name in self.content_names:
file_name = f"{file_name} ({len(self.content_names)})"
return file_name

def update_contents(self, content):
"""
Serializes the content object to a dictionary and appends it to the
contents list.
"""
self.contents.append(self.get_asset_json_from_content(content))
self.content_names.append(content.name)

@XBlock.json_handler
def get_files(self, data, suffix=''): # pylint: disable=unused-argument
Expand Down Expand Up @@ -265,12 +286,13 @@ def remove_files(self, data, suffix=''): # pylint: disable=unused-argument
asset_key = AssetKey.from_string(asset_key_id)
try:
delete_asset(self.course_id, asset_key)
except AssetNotFoundException as e: # pylint: disable=broad-except
except AssetNotFoundException as e:
log.exception(e)

for content in self.contents:
if content["asset_key"] == str(asset_key):
self.contents.remove(content)
self.content_names.remove(content["display_name"])
break

def get_asset_json_from_content(self, content):
Expand Down

0 comments on commit 7aed57c

Please sign in to comment.