Ejemplo n.º 1
0
def download_file(result_id):
    result = ResultModel.find_by_id(result_id)
    model = DesignModel.find_by_id(result.model_id)

    if model and (model.username == session["username"]
                  or session["admin"] == True):
        try:
            #Set custom mimetype and as_attmt values
            custom_mimetypes = {
                '.txt': 'text/plain',
                '.json': 'text/plain',
                '.csv': 'text/plain',
            }
            file_ext = os.path.basename(result.objname)[-4:]
            mimetype = custom_mimetypes.get(file_ext, None)
            #text files will open in new tab
            as_attachment = False if mimetype in ['text/plain'] else True

            uploads = os.path.join(current_app.config['UPLOADED_FILES_DEST'],
                                   os.path.dirname(result.objname))
            return send_from_directory(directory=uploads,
                                       filename=os.path.basename(
                                           result.objname),
                                       attachment_filename=result.name,
                                       mimetype=mimetype,
                                       as_attachment=as_attachment)
        except:
            traceback.print_exc()
            return "File not found!"
    else:
        flash('Unauthorized: Unable to download model id {}'.format(model_id),
              'danger')

        return redirect(url_for(".index", model_id=model.id))
Ejemplo n.º 2
0
def delete_result(result_id):
    result = ResultModel.find_by_id(result_id)
    model = DesignModel.find_by_id(result.model_id)
    if result and model and model.username == session["username"]:
        result.delete_from_db()
        try:
            uploads = os.path.join(current_app.config['UPLOADED_FILES_DEST'])
            os.remove(os.path.join(uploads, result.objname))
        except:
            traceback.print_exc()
            flash('Result File deletion failed', 'danger')
    else:
        flash('Unauthorized: Unable to delete result id {}'.format(result_id),
              'danger')

    return redirect(url_for(".index", model_id=model.id))
Ejemplo n.º 3
0
def download_file(result_id):

    result = ResultModel.find_by_id(result_id)

    model = DesignModel.find_by_id(result.model_id)

    if model and session["admin"] == True:
        try:
            uploads = os.path.join(current_app.config['UPLOADED_FILES_DEST'],
                                   os.path.dirname(result.objname))
            return send_from_directory(directory=uploads,
                                       filename=os.path.basename(
                                           result.objname),
                                       attachment_filename=result.name,
                                       as_attachment=True)
        except:
            traceback.print_exc()
            return "File not found!"
    else:
        flash('Unauthorized: Unable to download model id {}'.format(model_id),
              'danger')

        return redirect(url_for(".index"))