def _get_usage_dict(course, unit, item, scheme_name=None): """ Get usage info for unit/module. """ parent_unit = get_parent_unit(item) if unit == parent_unit and not item.has_children: # Display the topmost unit page if # the item is a child of the topmost unit and doesn't have its own children. unit_for_url = unit elif (not parent_unit and unit.get_parent()) or (unit == parent_unit and item.has_children): # Display the item's page rather than the unit page if # the item is one level below the topmost unit and has children, or # the item itself *is* the topmost unit (and thus does not have a parent unit, but is not an orphan). unit_for_url = item else: # If the item is nested deeper than two levels (the topmost unit > vertical > ... > item) # display the page for the nested vertical element. parent = item.get_parent() nested_vertical = item while parent != parent_unit: nested_vertical = parent parent = parent.get_parent() unit_for_url = nested_vertical unit_url = reverse_usage_url( 'container_handler', course.location.course_key.make_usage_key(unit_for_url.location.block_type, unit_for_url.location.block_id) ) usage_dict = {'label': u"{} / {}".format(unit.display_name, item.display_name), 'url': unit_url} if scheme_name == RANDOM_SCHEME: validation_summary = item.general_validation_message() usage_dict.update({'validation': validation_summary.to_json() if validation_summary else None}) return usage_dict
def _get_usage_dict(course, unit, item, scheme_name=None): """ Get usage info for unit/module. """ parent_unit = get_parent_unit(item) if unit == parent_unit and not item.has_children: # Display the topmost unit page if # the item is a child of the topmost unit and doesn't have its own children. unit_for_url = unit elif (not parent_unit and unit.get_parent()) or (unit == parent_unit and item.has_children): # Display the item's page rather than the unit page if # the item is one level below the topmost unit and has children, or # the item itself *is* the topmost unit (and thus does not have a parent unit, but is not an orphan). unit_for_url = item else: # If the item is nested deeper than two levels (the topmost unit > vertical > ... > item) # display the page for the nested vertical element. parent = item.get_parent() nested_vertical = item while parent != parent_unit: nested_vertical = parent parent = parent.get_parent() unit_for_url = nested_vertical unit_url = reverse_usage_url( 'container_handler', course.location.course_key.make_usage_key(unit_for_url.location.block_type, unit_for_url.location.block_id) ) usage_dict = {'label': u"{} / {}".format(unit.display_name, item.display_name), 'url': unit_url} if scheme_name == RANDOM_SCHEME: validation_summary = item.general_validation_message() usage_dict.update({'validation': validation_summary.to_json() if validation_summary else None}) return usage_dict
def test_get_parent_unit(self): """ Tests `get_parent_unit` method for the successful result. """ parent = utils.get_parent_unit(self.html_module_1) self.assertEqual(parent.location, self.vertical.location) parent = utils.get_parent_unit(self.child_html_module) self.assertEqual(parent.location, self.vertical_with_container.location) self.assertIsNone(utils.get_parent_unit(None)) self.assertIsNone(utils.get_parent_unit(self.vertical)) self.assertIsNone(utils.get_parent_unit(self.course)) self.assertIsNone(utils.get_parent_unit(self.chapter)) self.assertIsNone(utils.get_parent_unit(self.sequential))
def test_get_parent_unit(self): """ Tests `get_parent_unit` method for the successful result. """ parent = utils.get_parent_unit(self.html_module_1) self.assertEqual(parent.location, self.vertical.location) parent = utils.get_parent_unit(self.child_html_module) self.assertEqual(parent.location, self.vertical_with_container.location) self.assertIsNone(utils.get_parent_unit(None)) self.assertIsNone(utils.get_parent_unit(self.vertical)) self.assertIsNone(utils.get_parent_unit(self.course)) self.assertIsNone(utils.get_parent_unit(self.chapter)) self.assertIsNone(utils.get_parent_unit(self.sequential))
def test_get_parent_unit(self): """ Tests `get_parent_unit` method for the successful result. """ parent = utils.get_parent_unit(self.html_module_1) assert parent.location == self.vertical.location parent = utils.get_parent_unit(self.child_html_module) assert parent.location == self.vertical_with_container.location assert utils.get_parent_unit(None) is None assert utils.get_parent_unit(self.vertical) is None assert utils.get_parent_unit(self.course) is None assert utils.get_parent_unit(self.chapter) is None assert utils.get_parent_unit(self.sequential) is None
def preprocess_collection(user, course, collection): """ Prepare `collection(notes_list)` provided by edx-notes-api for rendering in a template: add information about ancestor blocks, convert "updated" to date Raises: ItemNotFoundError - when appropriate module is not found. """ # pylint: disable=too-many-statements store = modulestore() filtered_collection = list() cache = {} include_path_info = ('course_structure' not in settings.NOTES_DISABLED_TABS) with store.bulk_operations(course.id): for model in collection: update = { u"updated": dateutil_parse(model["updated"]), } model.update(update) usage_id = model["usage_id"] if usage_id in list(cache.keys()): model.update(cache[usage_id]) filtered_collection.append(model) continue usage_key = UsageKey.from_string(usage_id) # Add a course run if necessary. usage_key = usage_key.replace(course_key=store.fill_in_run(usage_key.course_key)) try: item = store.get_item(usage_key) except ItemNotFoundError: log.debug(u"Module not found: %s", usage_key) continue if not has_access(user, "load", item, course_key=course.id): log.debug(u"User %s does not have an access to %s", user, item) continue unit = get_parent_unit(item) if unit is None: log.debug(u"Unit not found: %s", usage_key) continue if include_path_info: section = unit.get_parent() if not section: log.debug(u"Section not found: %s", usage_key) continue if section.location in list(cache.keys()): usage_context = cache[section.location] usage_context.update({ "unit": get_module_context(course, unit), }) model.update(usage_context) cache[usage_id] = cache[unit.location] = usage_context filtered_collection.append(model) continue chapter = section.get_parent() if not chapter: log.debug(u"Chapter not found: %s", usage_key) continue if chapter.location in list(cache.keys()): usage_context = cache[chapter.location] usage_context.update({ "unit": get_module_context(course, unit), "section": get_module_context(course, section), }) model.update(usage_context) cache[usage_id] = cache[unit.location] = cache[section.location] = usage_context filtered_collection.append(model) continue usage_context = { "unit": get_module_context(course, unit), "section": get_module_context(course, section) if include_path_info else {}, "chapter": get_module_context(course, chapter) if include_path_info else {}, } model.update(usage_context) if include_path_info: cache[section.location] = cache[chapter.location] = usage_context cache[usage_id] = cache[unit.location] = usage_context filtered_collection.append(model) return filtered_collection
def preprocess_collection(user, course, collection): """ Prepare `collection(notes_list)` provided by edx-notes-api for rendering in a template: add information about ancestor blocks, convert "updated" to date Raises: ItemNotFoundError - when appropriate module is not found. """ # pylint: disable=too-many-statements store = modulestore() filtered_collection = list() cache = {} include_path_info = ('course_structure' not in settings.NOTES_DISABLED_TABS) with store.bulk_operations(course.id): for model in collection: update = { u"updated": dateutil_parse(model["updated"]), } model.update(update) usage_id = model["usage_id"] if usage_id in cache: model.update(cache[usage_id]) filtered_collection.append(model) continue usage_key = UsageKey.from_string(usage_id) # Add a course run if necessary. usage_key = usage_key.replace(course_key=store.fill_in_run(usage_key.course_key)) try: item = store.get_item(usage_key) except ItemNotFoundError: log.debug("Module not found: %s", usage_key) continue if not has_access(user, "load", item, course_key=course.id): log.debug("User %s does not have an access to %s", user, item) continue unit = get_parent_unit(item) if unit is None: log.debug("Unit not found: %s", usage_key) continue if include_path_info: section = unit.get_parent() if not section: log.debug("Section not found: %s", usage_key) continue if section in cache: usage_context = cache[section] usage_context.update({ "unit": get_module_context(course, unit), }) model.update(usage_context) cache[usage_id] = cache[unit] = usage_context filtered_collection.append(model) continue chapter = section.get_parent() if not chapter: log.debug("Chapter not found: %s", usage_key) continue if chapter in cache: usage_context = cache[chapter] usage_context.update({ "unit": get_module_context(course, unit), "section": get_module_context(course, section), }) model.update(usage_context) cache[usage_id] = cache[unit] = cache[section] = usage_context filtered_collection.append(model) continue usage_context = { "unit": get_module_context(course, unit), "section": get_module_context(course, section) if include_path_info else {}, "chapter": get_module_context(course, chapter) if include_path_info else {}, } model.update(usage_context) if include_path_info: cache[section] = cache[chapter] = usage_context cache[usage_id] = cache[unit] = usage_context filtered_collection.append(model) return filtered_collection