aboutsummaryrefslogtreecommitdiff
path: root/ustayml/views
diff options
context:
space:
mode:
authorMitsuo Tokumori <rtokumori@pucp.edu.pe>2023-11-13 10:55:39 -0500
committerMitsuo Tokumori <rtokumori@pucp.edu.pe>2023-11-13 10:55:39 -0500
commit5a16046eddc753b752c5ab1fdb91595adf588a6b (patch)
tree870192e1f576754c6fd669cd4e3dad6f70a7caf7 /ustayml/views
parentf6fcf9cc3ae3d93d59391b3f12843fba3297f0b2 (diff)
Add sample file upload for "load_data" blueprint
Also add a success page that redirects to the Dashboard
Diffstat (limited to 'ustayml/views')
-rw-r--r--ustayml/views/load_data.py50
-rw-r--r--ustayml/views/students.py3
2 files changed, 53 insertions, 0 deletions
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
diff --git a/ustayml/views/students.py b/ustayml/views/students.py
index 6132746..3b31790 100644
--- a/ustayml/views/students.py
+++ b/ustayml/views/students.py
@@ -68,6 +68,9 @@ def details(student_id):
[student_id]
)
+ if s is None:
+ abort(404, "El ID del estudiante es inválido.")
+
s['fullname'] = f"{s['first_name']} {s['last_name']}"
s['current_attendance'] = f"{s['current_attendance'] * 100:.2f}%"
s['current_merit'] = f"{s['current_merit'] * 100:.2f}%"