Skip to content

Commit

Permalink
fix: only load counts once per request
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Nov 10, 2023
1 parent 0c5d284 commit ba4926e
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration
from openedx.core.djangoapps.video_config.toggles import PUBLIC_VIDEO_SHARE
from openedx.core.lib.gating import api as gating_api
from openedx.core.lib.cache_utils import request_cached
from openedx.core.toggles import ENTRANCE_EXAMS
from xmodule.course_block import (
DEFAULT_START_DATE,
Expand Down Expand Up @@ -1401,9 +1402,7 @@ def create_xblock_info( # lint-amnesty, pylint: disable=too-many-statements
# If the ENABLE_TAGGING_TAXONOMY_LIST_PAGE feature flag is enabled, we show the "Manage Tags" options
if use_tagging_taxonomy_list_page():
xblock_info["use_tagging_taxonomy_list_page"] = True
# Create a pattern to match the IDs of the units, e.g. "block-v1:org+course+run+type@vertical+block@*"
unit_key_pattern = str(xblock.location.replace(block_type="vertical")).rsplit("@", 1)[0] + "@*"
xblock_info["tag_counts_by_unit"] = tagging_api.get_object_tag_counts(unit_key_pattern)
xblock_info["tag_counts_by_unit"] = _get_course_unit_tags(xblock.location.context_key)

xblock_info[
"has_partition_group_components"
Expand All @@ -1418,6 +1417,19 @@ def create_xblock_info( # lint-amnesty, pylint: disable=too-many-statements
return xblock_info


@request_cached()
def _get_course_unit_tags(course_key) -> dict:
"""
Get the count of tags that are applied to each unit (vertical) in this course, as a dict.
"""
if not course_key.is_course:
return {} # Unsupported key type, e.g. a library
# Create a pattern to match the IDs of the units, e.g. "block-v1:org+course+run+type@vertical+block@*"
vertical_key = course_key.make_usage_key('vertical', 'x')
unit_key_pattern = str(vertical_key).rsplit("@", 1)[0] + "@*"
return tagging_api.get_object_tag_counts(unit_key_pattern)


def _was_xblock_ever_exam_linked_with_external(course, xblock):
"""
Determine whether this XBlock is or was ever configured as an external proctored exam.
Expand Down

0 comments on commit ba4926e

Please sign in to comment.