From 5a16046eddc753b752c5ab1fdb91595adf588a6b Mon Sep 17 00:00:00 2001 From: Mitsuo Tokumori Date: Mon, 13 Nov 2023 10:55:39 -0500 Subject: Add sample file upload for "load_data" blueprint Also add a success page that redirects to the Dashboard --- ustayml/views/load_data.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 ustayml/views/load_data.py (limited to 'ustayml/views/load_data.py') diff --git a/ustayml/views/load_data.py b/ustayml/views/load_data.py new file mode 100644 index 0000000..e57f40c --- /dev/null +++ b/ustayml/views/load_data.py @@ -0,0 +1,50 @@ +import os +from flask import ( + Blueprint, flash, g, redirect, render_template, request, url_for, current_app +) +from werkzeug.exceptions import abort +from werkzeug.utils import secure_filename + +from ustayml.views.auth import login_required +from ustayml.db import get_db, get_paginated_rows, get_row + +bp = Blueprint('load_data', __name__, url_prefix='/load_data') + +ALLOWED_EXTENSIONS = ['txt', 'csv'] + + +@bp.route('/', methods=('GET', 'POST')) +@login_required +def index(): + if request.method == 'POST': + # check if the post request has the file part + if 'diccionario' not in request.files: + flash('No file part') + return redirect(request.url) + file = request.files['diccionario'] + # If the user does not select a file, the browser submits an + # empty file without a filename. + if file.filename == '': + flash('No selected file') + return redirect(request.url) + if file and allowed_file(file.filename): + filename = secure_filename(file.filename) + file.save(os.path.join(current_app.config['DATASET_PATH'], filename)) + return redirect(url_for('load_data.success', name=filename)) + return render_template( + 'load_data/index.html' + ) + +@bp.route('/success', methods=('GET', 'POST')) +@login_required +def success(): + return render_template( + 'load_data/success.html' + ) + + +# Helper functions + +def allowed_file(filename): + return '.' in filename and \ + filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS \ No newline at end of file -- cgit v1.2.3