aboutsummaryrefslogtreecommitdiff
path: root/qc_app/entry.py
blob: b7b4b6f9ebeb5ba6de16cb03c8e6b1c5085338e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Entry-point module"""
import os

from werkzeug.utils import secure_filename
from flask import (
    flash,
    request,
    url_for,
    redirect,
    Blueprint,
    render_template,
    current_app as app)

entrybp = Blueprint("entry", __name__)

@entrybp.route("/", methods=["GET", "POST"])
def upload_file():
    """Enables uploading the files"""
    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")

    filename = secure_filename(text_file.filename)
    if not os.path.exists(app.config["UPLOAD_FOLDER"]):
        os.mkdir(app.config["UPLOAD_FOLDER"])

    filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
    text_file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))

    return redirect(url_for(
        "parse.parse", filename=filename,
        filetype=request.form["filetype"]))