Exemple #1
0
def create():
    data = request_data({'author': request.account})
    project = ProjectRef().get(data.get('project'))
    data['project'] = project
    authz.require(authz.project_edit(project))
    file_ = files.save(data, request.files.get('file'))
    db.session.commit()
    return jsonify(file_)
Exemple #2
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