Skip to content

Commit

Permalink
fix: wrap into try/except block getting icon for xblock (#2509)
Browse files Browse the repository at this point in the history
* fix: wrap into try/except block getting icon for xblock

* fix: revision after review
  • Loading branch information
ruzniaievdm committed Mar 6, 2024
1 parent d8a42c9 commit 8979134
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2,247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@

CREATE_IF_NOT_FOUND = ["course_info"]

# List of categories to check for presence in the children of the XBlock.
# This list is used to determine if all of the specified categories are absent
# in the categories of the children XBlock instances otherwise icon class variable will be set to `None`.
CATEGORIES_WITH_ABSENT_ICON = ["split_test"]

# Useful constants for defining predicates
NEVER = lambda x: False
ALWAYS = lambda x: True
Expand Down Expand Up @@ -1068,10 +1063,6 @@ def create_xblock_info( # lint-amnesty, pylint: disable=too-many-statements
)
else:
user_partitions = get_user_partition_info(xblock, course=course)
all_excluded_categories_absent = all(
category not in [child.category for child in xblock.get_children()]
for category in CATEGORIES_WITH_ABSENT_ICON
)
xblock_info.update(
{
"edited_on": get_default_time_display(xblock.subtree_edited_on)
Expand Down Expand Up @@ -1101,7 +1092,7 @@ def create_xblock_info( # lint-amnesty, pylint: disable=too-many-statements
"show_correctness": xblock.show_correctness,
"hide_from_toc": xblock.hide_from_toc,
"enable_hide_from_toc_ui": settings.FEATURES.get("ENABLE_HIDE_FROM_TOC_UI", False),
"xblock_type": get_icon(xblock) if is_xblock_unit and all_excluded_categories_absent else None,
"xblock_type": get_icon(xblock),
}
)

Expand Down
37 changes: 33 additions & 4 deletions xmodule/split_test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from web_fragments.fragment import Fragment
from webob import Response
from xblock.core import XBlock
from xblock.exceptions import NoSuchServiceError
from xblock.fields import Integer, ReferenceValueDict, Scope, String
from xmodule.mako_block import MakoTemplateBlockBase
from xmodule.modulestore.inheritance import UserPartitionList
Expand Down Expand Up @@ -172,10 +173,20 @@ def child_block(self):
def child(self):
"""
Return the user bound child block for the partition or None.
Handles the AttributeError exception that may occur when attempting to retrieve
an icon for the split_test xblock within the CMS.
"""
if self.child_block is not None:
return self.runtime.get_block_for_descriptor(self.child_block)
else:
try:
if self.child_block is not None:
return self.runtime.get_block_for_descriptor(self.child_block)
else:
return None
except AttributeError:
log.warning(
"Error while getting block instance for descriptor with location: [%s]",
self.location
)
return None

def get_child_block_by_location(self, location):
Expand Down Expand Up @@ -212,13 +223,31 @@ def get_content_titles(self):
def get_child_blocks(self):
"""
For grading--return just the chosen child.
Handles the NoSuchServiceError and ValueError exception that may occur when attempting to retrieve
an icon for the split_test xblock within the CMS.
"""
group_id = self.get_group_id()
try:
group_id = self.get_group_id()
except NoSuchServiceError:
log.warning(
"Error while getting user service in runtime with location: [%s]",
self.location
)
return []
except ValueError:
log.warning(
"Error while getting group ID for partition with location: [%s]",
self.location
)
return []

if group_id is None:
return []

# group_id_to_child comes from json, so it has to have string keys
str_group_id = str(group_id)
child_block = None
if str_group_id in self.group_id_to_child:
child_location = self.group_id_to_child[str_group_id]
child_block = self.get_child_block_by_location(child_location)
Expand Down
Loading

0 comments on commit 8979134

Please sign in to comment.