'Handle history sharing.'

@web.expose
@web.require_login('share    Galaxy    histories')
def sharing(self, trans, id=None, histories=[], **kwargs):

    session = trans.sa_session
    if id:
        ids = galaxy.util.listify(id)
        if ids:
            histories = [self.history_manager.get_accessible(self.decode_id(history_id), trans.user, current_history=trans.history) for history_id in ids]
    elif (not histories):
        histories = [trans.history]
    for history in histories:
        if ('make_accessible_via_link' in kwargs):
            self._make_item_accessible(trans.sa_session, history)
        elif ('make_accessible_and_publish' in kwargs):
            self._make_item_accessible(trans.sa_session, history)
            history.published = True
        elif ('publish' in kwargs):
            if history.importable:
                history.published = True
            else:
                pass
        elif ('disable_link_access' in kwargs):
            history.importable = False
        elif ('unpublish' in kwargs):
            history.published = False
        elif ('disable_link_access_and_unpublish' in kwargs):
            history.importable = history.published = False
        elif ('unshare_user' in kwargs):
            user = trans.sa_session.query(trans.app.model.User).get(self.decode_id(kwargs['unshare_user']))
            deleted_sharing_relation = False
            husas = trans.sa_session.query(trans.app.model.HistoryUserShareAssociation).filter_by(user=user, history=history).all()
            if husas:
                deleted_sharing_relation = True
                for husa in husas:
                    trans.sa_session.delete(husa)
            if (not deleted_sharing_relation):
                history_name = escape(history.name)
                user_email = escape(user.email)
                message = ("History    '%s'    does    not    seem    to    be    shared    with    user    '%s'" % (history_name, user_email))
                return trans.fill_template('/sharing_base.mako', controller_list='histories', item=history, message=message, status='error', use_panels=True)
    for history in histories:
        if (history.importable and (not history.slug)):
            self._make_item_accessible(trans.sa_session, history)
    session.flush()
    return trans.fill_template('/sharing_base.mako', controller_list='histories', item=history, use_panels=True)
