Example #1
0
def instance_object(fields=[], filters=(), validators=Instance._validations, **instance):
    instance = filter_fields(instance, filters)
    validators['instance_data'] = create_instance_validators(fields)
    instance['schema_id'] = uuid.UUID(instance.get('schema_id', ''))
    instance['table_id'] = uuid.UUID(instance.get('table_id', ''))
    instance = Instance(**instance)
    instance.validate(validators)
    instance._validations = validators
    if (hasattr(instance, 'validation_errors') 
        and instance.validation_errors) \
    or instance.instance_data.get('validation_errors', {}): 
        instance.validation_errors = dict(instance.validation_errors.items() + instance.instance_data.get('validation_errors', {}).items())
        raise BadRequestError('Validations failed: '+ str(instance.validation_errors))
    if instance.instance_data.has_key('validation_errors'):
        del instance.instance_data['validation_errors']
    return instance
Example #2
0
def query_args(new_response, filters=[]):
    """
    Method to unwrap and filter fields that are returned by a JSON REST response
    The 'grab' query arg is intended to be like select columns to reduce the number of fileds returned
    The 'unwrap' query arg is intended to get a field from the top level obj to be the return value
    """
    if hasattr(new_response, 'clean4_dump'):
        new_response = new_response.clean4_dump()
    for key in request.args.getlist('unwrap'):
        new_response = new_response[key]
    grab = request.args.getlist('grab') or new_response.keys()
    if grab or filters:
        grab = set(grab)
        keys = list(set(new_response.keys()) - grab)
        filters = list(set(filters) - grab)
        new_response = filter_fields(new_response, keys + filters)
    return new_response
Example #3
0
def unique_object(filters=(), validators=Unique._validations, **unique):
    unique = filter_fields(unique, filters)
    unique = Unique(**unique)
    unique.validate(validators)
    return unique
Example #4
0
def table_object(filters=(), validators=Table._validations, **table):
    table = filter_fields(table, filters)
    table['fields'] = [field_object(**field) for field in table.get('fields', [])]
    table = Table(**table)
    table.validate(validators)
    return table
Example #5
0
def schema_object(filters=(), validators=Schema._validations, **schema):
    schema = filter_fields(schema, filters)
    schema['tables'] = [table_object(**table) for table in schema.get('tables', [])]
    schema = Schema(**schema)
    schema.validate(validators)
    return schema
Example #6
0
def tenant_object(filters=(), validators=Tenant._validations, **tenant):
    tenant = filter_fields(tenant, filters)
    tenant['schemas'] = [schema_object(**schema) for schema in tenant.get('schemas', [])]
    tenant = Tenant(**tenant)
    tenant.validate(validators)
    return tenant
Example #7
0
def field_object(filters=(), validators=Field._validations, **field):
    field = filter_fields(field, filters)
    field = Field(**field)
    field.validate(validators)
    return field
Example #8
0
def remove_id_tag(orm, fields=('id',)):
    return json.dumps(filter_fields(json.loads(json.dumps(orm, default=remove_OIDs)), fields))
Example #9
0
def indexed_object(filters=(), validators=Index._validations, **indexed):
    indexed = filter_fields(indexed, filters)
    indexed = Index(**indexed)
    indexed.validate(validators)
    return indexed