diff options
author | Frederick Muriuki Muriithi | 2022-04-25 10:03:10 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2022-04-25 10:03:10 +0300 |
commit | 063a9529c49df5f21d83c973f501db09161542cd (patch) | |
tree | b0ee520655ef25d76d5aee5f4f4e015d54878177 /qc_app/entry.py | |
parent | b419a43c851336aeb57407c72c0a37271fbb2ebc (diff) | |
download | gn-uploader-063a9529c49df5f21d83c973f501db09161542cd.tar.gz |
Implement file upload
Diffstat (limited to 'qc_app/entry.py')
-rw-r--r-- | qc_app/entry.py | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/qc_app/entry.py b/qc_app/entry.py index 951b201..e7ed294 100644 --- a/qc_app/entry.py +++ b/qc_app/entry.py @@ -1,9 +1,51 @@ """Entry-point module""" -from flask import Blueprint, render_template +import os + +from werkzeug.utils import secure_filename +from flask import ( + flash, + request, + url_for, + redirect, + Blueprint, + render_template, + current_app as app) + +from quality_control.parsing import FileType entrybp = Blueprint("entry", __name__) @entrybp.route("/", methods=["GET", "POST"]) def upload_file(): """Enables uploading the files""" - return render_template("index.html") + if request.method == "GET": + return render_template("index.html") + + errors = False + if request.form["filetype"] not in ("average", "standard_error"): + flash("Invalid file type provided.", "alert-error") + errors = True + + if ("qc_text_file" not in request.files or + request.files["qc_text_file"].filename == ""): + flash("No file was uploaded.", "alert-error") + errors = True + + text_file = request.files["qc_text_file"] + if text_file.mimetype != "text/tab-separated-values": + flash("Invalid file! Expected a tab-separated-values file.", + "alert-error") + errors = True + + if errors: + return render_template("index.html") + + filetype = ( + FileType.AVERAGE if request.form["filetype"] == "average" + else FileType.STANDARD_ERROR) + filename = secure_filename(text_file.filename) + if not os.path.exists(app.config["UPLOAD_FOLDER"]): + os.mkdir(app.config["UPLOAD_FOLDER"]) + text_file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename)) + + return redirect(url_for("parse.parse_file", filename=filename)) |