Esempio n. 1
0
def save(obj, data, files=None):
    """ Set a property on the given object (entity or relation). This will
    either create a new property object or re-activate an existing object
    from the same source, if one exists. If the property is defined as
    ``active``, existing properties with the same name will be de-activated.

    WARNING: This does not, on its own, perform any validation.
    """
    prop = None

    for cand in obj.properties:
        if cand.name != data.get('name'):
            continue
        if cand.value == data.get('value'):
            prop = cand
        elif cand.active and data.get('active'):
            cand.active = False

    if prop is None:
        prop = Property()
        db.session.add(prop)
        if isinstance(obj, Entity):
            prop.entity = obj
        else:
            prop.relation = obj

    attribute = obj.schema.get_attribute(data.get('name'))
    if attribute.datatype == 'file':
        # if there is a file with the property name in `files`
        # a new file is created and the property updated
        file_key = data.get('name')
        if file_key in files:
            file_data = files.get(file_key)
            file = files_logic.save(data, file_data)
            prop.value_file_id = file.id
            prop.value_string = url_for('files_api.serve', id=file.id)
        elif data.get('value') is None:
            raise TypeError("File for property '%s' is required" % file_key)
    else:
        setattr(prop, attribute.value_column, data.get('value'))
        # set value of precision field for datetime property
        if attribute.datatype == 'datetime':
            prop.value_datetime_precision = data.get('value_precision')

    prop.name = data.get('name')
    prop.author = data.get('author')
    prop.attribute = attribute
    prop.active = data.get('active')
    prop.source_url = data.get('source_url')
    prop.updated_at = datetime.utcnow()
    obj.updated_at = datetime.utcnow()
    return prop
Esempio n. 2
0
def relations_index(obj_id):
    query = Property.all()
    query = query.filter(Property.relation_id != None)
    obj = Relation.by_id(obj_id)
    query = query.filter_by(relation_id=obj_id)
    return _index(query, obj)
Esempio n. 3
0
def entities_index(obj_id):
    query = Property.all()
    query = query.filter(Property.relation_id != None)
    obj = Entity.by_id(obj_id)
    query = query.filter_by(entity_id=obj_id)
    return _index(query, obj)
Esempio n. 4
0
def save(obj, data, files=None):
    """ Set a property on the given object (entity or relation). This will
    either create a new property object or re-activate an existing object
    from the same source, if one exists. If the property is defined as
    ``active``, existing properties with the same name will be de-activated.

    WARNING: This does not, on its own, perform any validation.
    """
    prop = None

    for cand in obj.properties:
        if cand.name != data.get('name'):
            continue
        if cand.value == data.get('value'):
            prop = cand
        elif cand.active and data.get('active'):
            cand.active = False

    if prop is None:
        prop = Property()
        db.session.add(prop)
        if isinstance(obj, Entity):
            prop.entity = obj
        else:
            prop.relation = obj

    if data.get('attribute').datatype == 'file':
        # if there is a file with the property name in `files`
        # a new file is created and the property updated
        file_key = data.get('name')
        if file_key in files:
            file_data = files.get(file_key)
            file = files_logic.save(data, file_data)
            prop.value_file_id = file.id
            prop.value_string = url_for('files_api.serve', id=file.id)
        elif data.get('value') is None:
            raise TypeError("File for property '%s' is required" % file_key)
    else:
        setattr(prop, data.get('attribute').value_column,
                data.get('value'))

    prop.name = data.get('name')
    prop.author = data.get('author')
    prop.schema = data.get('schema')
    prop.attribute = data.get('attribute')
    prop.active = data.get('active')
    prop.source_url = data.get('source_url')
    prop.updated_at = datetime.utcnow()
    obj.updated_at = datetime.utcnow()

    return prop