'Extract non orphan parents by traversing the list of possible parents and remove current location
from orphan parents to avoid parents calculation overhead next time.'

def _get_non_orphan_parents(self, location, parents, revision):

    non_orphan_parents = []
    bulk_record = self._get_bulk_ops_record(location.course_key)
    for parent in parents:
        parent_loc = Location._from_deprecated_son(parent['_id'], location.course_key.run)
        ancestor_loc = parent_loc
        while (ancestor_loc is not None):
            current_loc = ancestor_loc
            ancestor_loc = self._get_raw_parent_location(as_published(current_loc), revision)
            if (ancestor_loc is None):
                bulk_record.dirty = True
                self.collection.update({'_id': parent_loc.to_deprecated_son()}, {'$set': {'definition.children': []}}, multi=False, upsert=True)
            elif (ancestor_loc.category == 'course'):
                non_orphan_parents.append(parent_loc)
                break
    return non_orphan_parents
