'Delete role'

def _delete(self, r, **attr):

    session = current.session
    request = self.request
    T = current.T
    auth = current.auth
    if r.interactive:
        if r.record:
            role = r.record
            role_id = role.id
            role_name = role.role
            if (role.protected or role.system):
                session.error = ('%s    "%s"    %s' % (T('Role'), role_name, T('cannot    be    deleted.')))
                redirect(URL(c='admin', f='role', vars=request.get_vars))
            else:
                db = current.db
                acl_table = auth.permission.table
                query = ((acl_table.deleted != True) & (acl_table.group_id == role_id))
                db(query).update(deleted=True)
                membership_table = db.auth_membership
                query = ((membership_table.deleted != True) & (membership_table.group_id == role_id))
                db(query).update(deleted=True)
                session.s3.roles = [role for role in session.s3.roles if (role != role_id)]
                query = ((self.table.deleted != True) & (self.table.id == role_id))
                db(query).update(role=None, deleted=True)
                session.confirmation = ('%s    "%s"    %s' % (T('Role'), role_name, T('deleted')))
        else:
            session.error = T('No    role    to    delete')
    else:
        r.error(415, current.ERROR.BAD_FORMAT)
    redirect(URL(c='admin', f='role', vars=request.get_vars))
