'Publish the subtree rooted at location to the live course and remove the drafts.
Such publishing may cause the deletion of previously published but subsequently deleted
child trees. Overwrites any existing published xblocks from the subtree.
Treats the publishing of non-draftable items as merely a subtree selection from
which to descend.
Raises:
ItemNotFoundError: if any of the draft subtree nodes aren\'t found
Returns:
The newly published xblock'

def publish(self, location, user_id, **kwargs):

    to_be_deleted = []
    def _internal_depth_first(item_location, is_root):
        '\n                                                Depth    first    publishing    from    the    given    location\n                                                '
        try:
            item = self.get_item(item_location)
        except ItemNotFoundError:
            log.warning('Cannot    find:    %s', item_location)
            return
        if item.has_children:
            for child_loc in item.children:
                _internal_depth_first(child_loc, False)
        if ((item_location.category in DIRECT_ONLY_CATEGORIES) or (not getattr(item, 'is_draft', False))):
            return
        try:
            original_published = super(DraftModuleStore, self).get_item(item_location)
        except ItemNotFoundError:
            original_published = None
        if item.has_children:
            if (original_published is not None):
                for orig_child in original_published.children:
                    if (orig_child not in item.children):
                        published_parent = self.get_parent_location(orig_child)
                        if (published_parent == item_location):
                            self._delete_subtree(orig_child, [as_published])
                        else:
                            pass
        super(DraftModuleStore, self).update_item(item, user_id, isPublish=True, is_publish_root=is_root, allow_not_found=True)
        to_be_deleted.append(as_draft(item_location).to_deprecated_son())
    self._verify_branch_setting(ModuleStoreEnum.Branch.draft_preferred)
    _verify_revision_is_published(location)
    _internal_depth_first(location, True)
    course_key = location.course_key
    bulk_record = self._get_bulk_ops_record(course_key)
    if (len(to_be_deleted) > 0):
        bulk_record.dirty = True
        self.collection.remove({'_id': {'$in': to_be_deleted}})
    self._flag_publish_event(course_key)
    return self.get_item(as_published(location))
