示例#1
0
def upload(id):
    modelrun = ModelRun.query.get(id)
    if modelrun:
      # if modelrun.progress_state in
      # [PROGRESS_STATES['NOT_STARTED'],PROGRESS_STATES['RUNNING']]:
        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            resource_file = storage.upload(file)
            resource_type = request.form['resource_type']
            m = {
                'modelrun_id': id,
                'resource_type': resource_type,
                'resource_name': resource_file.name,
                #'resource_url': url_for('modelresource.download_resource_by_name', name=resource_file.name, _external=True),
                'resource_size': resource_file.size
            }
            resource = ModelResource.create(**m)

            msg = {"message": "Resource create for model run " +
                   str(id), 'resource': modelresource_serializer(resource)}
            return jsonify(msg), 201
        else:
            err = {"message": "File parameter isn't provided"}
            return jsonify(err), 400
    else:
        err = {"message": "Modelrun doesn't exist"}
        return jsonify(err), 404
示例#2
0
def upload(id):
    modelrun = ModelRun.query.get(id)
    if modelrun:
        # if modelrun.progress_state in
        # [PROGRESS_STATES['NOT_STARTED'],PROGRESS_STATES['RUNNING']]:
        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            resource_file = storage.upload(file)
            resource_type = request.form['resource_type']
            m = {
                'modelrun_id': id,
                'resource_type': resource_type,
                'resource_name': resource_file.name,
                #'resource_url': url_for('modelresource.download_resource_by_name', name=resource_file.name, _external=True),
                'resource_size': resource_file.size
            }
            resource = ModelResource.create(**m)

            msg = {
                "message": "Resource create for model run " + str(id),
                'resource': modelresource_serializer(resource)
            }
            return jsonify(msg), 201
        else:
            err = {"message": "File parameter isn't provided"}
            return jsonify(err), 400
    else:
        err = {"message": "Modelrun doesn't exist"}
        return jsonify(err), 404
示例#3
0
def upload_from_url(id):
    '''
      expects json. expects url,filename,resource_type
    '''
    modelrun = ModelRun.query.get(id)
    if modelrun:
        if modelrun.progress_state == PROGRESS_STATES['NOT_STARTED']:
            try:
                data = json.loads(request.get_data())
            except ValueError:
                return jsonify({'message': 'Please specify valid json'}), 400

            if not ('url' in data and 'resource_type' in data
                    and 'filename' in data):
                return jsonify({'message': 'Invalid Input Provided'}), 400

            try:
                filedata = requests.get(data['url'])
                tmp_loc = os.path.join('/tmp/', data['filename'])
                with app.open_instance_resource(tmp_loc, 'wb') as f:
                    f.write(filedata.content)
                resource_file = storage.upload(tmp_loc)
                resource_type = data['resource_type']
                m = {
                    'modelrun_id': id,
                    'resource_type': resource_type,
                    'resource_name': resource_file.name,
                    #'resource_url': url_for('modelresource.download_resource_by_name', name=resource_file.name, _external=True),
                    'resource_size': resource_file.size
                }
                resource = ModelResource.create(**m)
                return jsonify({
                    'message':
                    "Resource create for model run " + str(id),
                    'resource':
                    modelresource_serializer(resource)
                }), 201
            except Exception, e:
                return jsonify({'message':
                                'Couldn\'t get file from url.'}), 400

        else:
            return jsonify({
                'message':
                'Uploading resources to new modelrun is permitted only'
            }), 400
示例#4
0
def upload_from_url(id):
    '''
      expects json. expects url,filename,resource_type
    '''
    modelrun = ModelRun.query.get(id)
    if modelrun:
        if modelrun.progress_state == PROGRESS_STATES['NOT_STARTED']:
            try:
                data = json.loads(request.get_data())
            except ValueError:
                return jsonify({'message': 'Please specify valid json'}), 400

            if not ('url' in data and 'resource_type' in data and 'filename' in data):
                return jsonify({'message': 'Invalid Input Provided'}), 400

            try:
                filedata = requests.get(data['url'])
                tmp_loc = os.path.join('/tmp/', data['filename'])
                with app.open_instance_resource(tmp_loc, 'wb') as f:
                    f.write(filedata.content)
                resource_file = storage.upload(tmp_loc)
                resource_type = data['resource_type']
                m = {
                    'modelrun_id': id,
                    'resource_type': resource_type,
                    'resource_name': resource_file.name,
                    #'resource_url': url_for('modelresource.download_resource_by_name', name=resource_file.name, _external=True),
                    'resource_size': resource_file.size
                }
                resource = ModelResource.create(**m)
                return jsonify({'message': "Resource create for model run " + str(id), 'resource': modelresource_serializer(resource)}), 201
            except Exception, e:
                return jsonify({'message': 'Couldn\'t get file from url.'}), 400

        else:
            return jsonify({'message': 'Uploading resources to new modelrun is permitted only'}), 400