'Recursively traverse the block_hierarchy and add additional
parents. This method is expected to be called only after all
blocks have been created.
The additional parents are obtained from the \'#parents\' field
and is expected to be a list of \'#ref\' values of the parents.
Note: if a \'#parents\' field is found, the block is removed from
the course block since it is expected to not belong to the root.
If the block is meant to be a direct child of the course as
well, the course should be explicitly listed in \'#parents\'.
Arguments:
block_hierarchy (BlockStructureDict):
Definition of block hierarchy.
block_map (dict[str: XBlock]):
Mapping from \'#ref\' values to their XBlocks.'

def add_parents(self, block_hierarchy, block_map):

    parents = block_hierarchy.get('#parents', [])
    if parents:
        block_key = block_map[block_hierarchy['#ref']].location
        course = modulestore().get_item(block_map['course'].location)
        if (block_key in course.children):
            course.children.remove(block_key)
            block_map['course'] = update_block(course)
        for parent_ref in parents:
            parent_block = modulestore().get_item(block_map[parent_ref].location)
            parent_block.children.append(block_key)
            block_map[parent_ref] = update_block(parent_block)
    for child_hierarchy in block_hierarchy.get('#children', []):
        self.add_parents(child_hierarchy, block_map)
