Beispiel #1
0
def create_policy(name, source=None, schedule_src=None, condition_srcs=None, 
                  apply_src=None, enabled=True):
    """ Creates a new policy model object to the database.  If it doesn't 
        compile, an exception will be thrown.
    """
    assert isinstance(name, basestring)  # TODO: make this an exception
    source = policy.combine_sources(source, schedule_src, condition_srcs, 
                                    apply_src)
    # The following method should throw an exception if it fails, so the 
    # following 'raise' statement should never get called.  It is here for 
    # explanatory purposes.
    if not policy.check_source(source):
        msg = 'Invariant error:  policy.check_source() returned False.'
        raise RuntimeError(msg)
    model = PolicyModel(name=name, source=source, enabled=enabled)
    model.modified = datetime.datetime.now()
    model.save()
    return model