示例#1
0
def modify(operator_id, **kwargs):
    """
    This function will attempt to modify the operator with the passed
    in values.
    
    :keyword username: The username for this operator.
    :keyword password: The password for this operator.
    :keyword access_id: The associated access level id for this operator. 
    """
    session = meta.Session()
    update_attributes = kwargs  # Just to make it clearer

    log.debug("Update attribs = %r" % update_attributes)

    # Force the password to be null if it is empty (prevent logins w/ empty password)
    if update_attributes.get("password") == "":
        update_attributes["password"] = None

    try:
        operator = get(operator_id)
        modified = model.set_entity_attributes(operator, update_attributes, hashed_attributes=["password"])
        session.flush()
    except:
        log.exception("Error modifying operator: {0}".format(operator_id))
        raise
    return (operator, modified)
示例#2
0
def modify(resource_id, group_ids=None, **kwargs):
    """
    This function will modify a resource entry in the database, only updating
    specified attributes.
    
    :param resource_id: The ID of resource to modify.
    :keyword group_ids: The group IDs that this resource should belong to.
    :keyword name: The resource name.
    :keyword addr: The resource address.
    :keyword notes: An (encrypted) notes field.
    :keyword tags: The tags field.
    :keyword description: A description fields (not encrypted).
    """
    if isinstance(group_ids, (basestring,int)):
        group_ids = [int(group_ids)]
    
    if group_ids is not None and len(group_ids) == 0:
        raise ValueError("Cannot remove all groups from a resource.")
    
    session = meta.Session()
    
    resource = get(resource_id)
    
    update_attributes = kwargs
    
    try:
        modified = model.set_entity_attributes(resource, update_attributes, encrypted_attributes=['notes'])
        session.flush()
    except:
        log.exception("Error updating resource.")
        raise
    
    gr_t = model.group_resources_table
    
    if group_ids is not None:
        # Is it different from what's there now?
        if set(group_ids) != set([cg.id for cg in resource.groups]):
            try:
                session.execute(gr_t.delete(gr_t.c.resource_id==resource_id))
                for group_id in group_ids:
                    group_id = int(group_id)
                    gr = model.GroupResource()
                    gr.resource_id = resource.id
                    gr.group_id = group_id
                    session.add(gr)
                session.flush()
            except:
                log.exception("Error adding group memberships")
                raise
            else:
                modified += ['group_ids']
    
    session.flush()
    
    return (resource, modified)
示例#3
0
def modify(group_id, **kwargs):
    """
    This function will update a group record in the database.
    
    :keyword name: The group name.
    :raise ValueError: If the group ID cannot be resolved.
    """
    session = meta.Session()
    group = get(group_id)
    update_attributes = kwargs
    try:
        modified = model.set_entity_attributes(group, update_attributes)
        session.flush()
    except:
        log.exception("Error updating group: {0}".format(group_id))
        raise
    
    return (group, modified)
示例#4
0
def modify(access_id, **kwargs):
    """
    This function will modify the description of an access level.
    :keyword level: The numeric access level mask.
    :keyword description: The  description for the new access level.
    :return: A tuple of access level object and modified rows.
    :raise ValueError: If unable to retrieve access level for ID.
    """
    alevel = get(access_id)
    if not alevel:
        raise ValueError("No matching access level found for ID: {0}".format(access_id))
    
    session = meta.Session()
    try:
        update_attributes = kwargs
        modified = model.set_entity_attributes(alevel, update_attributes)
        session.add(alevel)
        session.flush()
    except:
        log.exception("Unable to modify: {0}".format(access_id))
        raise
    
    return (alevel, modified)