'Sets the permissions on a history.'

@web.expose
def history_set_default_permissions(self, trans, id=None, **kwd):

    if trans.user:
        if ('update_roles_button' in kwd):
            history = None
            if id:
                try:
                    id = int(id)
                except:
                    id = None
                if id:
                    history = trans.sa_session.query(trans.app.model.History).get(id)
            if (not history):
                history = trans.get_history()
            p = Params(kwd)
            permissions = {}
            for (k, v) in trans.app.model.Dataset.permitted_actions.items():
                in_roles = p.get((k + '_in'), [])
                if (not isinstance(in_roles, list)):
                    in_roles = [in_roles]
                in_roles = [trans.sa_session.query(trans.app.model.Role).get(x) for x in in_roles]
                permissions[trans.app.security_agent.get_action(v.action)] = in_roles
            dataset = ('dataset' in kwd)
            bypass_manage_permission = ('bypass_manage_permission' in kwd)
            trans.app.security_agent.history_set_default_permissions(history, permissions, dataset=dataset, bypass_manage_permission=bypass_manage_permission)
            return trans.show_ok_message('Default    history    permissions    have    been    changed.')
        return trans.fill_template('history/permissions.mako')
    else:
        return trans.show_error_message("You    must    be    logged    in    to    change    a    history's    default    permissions.")
