def mark_last_accessed(user, course_key, block): """ Recursively marks the branch to the last accessed block. """ block_key = block.serializer.instance student_module_dict = get_student_module_as_dict(user, course_key, block_key) last_accessed_child_position = student_module_dict.get('position') if last_accessed_child_position and block.get('children'): block['last_accessed'] = True if last_accessed_child_position <= len(block['children']): last_accessed_child_block = block['children'][last_accessed_child_position - 1] last_accessed_child_block['last_accessed'] = True mark_last_accessed(user, course_key, last_accessed_child_block) else: # We should be using an id in place of position for last accessed. However, while using position, if # the child block is no longer accessible we'll use the last child. block['children'][-1]['last_accessed'] = True