about summary refs log tree commit diff
path: root/qc_app/entry.py
diff options
context:
space:
mode:
Diffstat (limited to 'qc_app/entry.py')
-rw-r--r--qc_app/entry.py46
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))