From 063a9529c49df5f21d83c973f501db09161542cd Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 25 Apr 2022 10:03:10 +0300 Subject: Implement file upload --- etc/default_config.py | 1 + qc_app/__init__.py | 6 ++---- qc_app/entry.py | 46 ++++++++++++++++++++++++++++++++++++++++++-- qc_app/parse.py | 14 ++++++++++++++ qc_app/parse_error.py | 9 --------- qc_app/parse_success.py | 9 --------- qc_app/static/css/styles.css | 12 ++++++++++++ qc_app/templates/index.html | 13 ++++++++++++- 8 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 qc_app/parse.py delete mode 100644 qc_app/parse_error.py delete mode 100644 qc_app/parse_success.py diff --git a/etc/default_config.py b/etc/default_config.py index 80f9d59..76df031 100644 --- a/etc/default_config.py +++ b/etc/default_config.py @@ -7,3 +7,4 @@ import os LOG_LEVEL = os.getenv("LOG_LEVEL", "WARNING") SECRET_KEY = b"" +UPLOAD_FOLDER = "/tmp/qc_app_files" diff --git a/qc_app/__init__.py b/qc_app/__init__.py index 2dd5758..9b5ed76 100644 --- a/qc_app/__init__.py +++ b/qc_app/__init__.py @@ -4,8 +4,7 @@ import os from flask import Flask from .entry import entrybp -from .parse_error import parseerrbp -from .parse_success import parsesuccessbp +from .parse import parsebp def create_app(instance_path): """The application factory""" @@ -16,6 +15,5 @@ def create_app(instance_path): # setup blueprints app.register_blueprint(entrybp, url_prefix="/") - app.register_blueprint(parseerrbp, url_prefix="/error") - app.register_blueprint(parsesuccessbp, url_prefix="/success") + app.register_blueprint(parsebp, url_prefix="/parse") return app 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)) diff --git a/qc_app/parse.py b/qc_app/parse.py new file mode 100644 index 0000000..1d9e3c8 --- /dev/null +++ b/qc_app/parse.py @@ -0,0 +1,14 @@ +"""File parsing module""" +from flask import Blueprint + +parsebp = Blueprint("parse", __name__) + +@parsebp.route("/parse/", methods=["GET"]) +def parse_file(filename): + """Trigger file parsing""" + return f"STUB: Parse of '{filename}' ongoing!!!" + +@parsebp.route("/success", methods=["GET"]) +def success(): + """Indicates success if parsing the file is successful""" + return "STUB: Parse success!!!" diff --git a/qc_app/parse_error.py b/qc_app/parse_error.py deleted file mode 100644 index 1414b78..0000000 --- a/qc_app/parse_error.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Parse Error handling blue print""" -from flask import Blueprint - -parseerrbp = Blueprint("parse_error", __name__) - -@parseerrbp.route("/", methods=["GET"]) -def parse_success(): - """Indicates success if parsing the file is fails""" - return "STUB: Parse error!!!" diff --git a/qc_app/parse_success.py b/qc_app/parse_success.py deleted file mode 100644 index 53b9c42..0000000 --- a/qc_app/parse_success.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Successful file parsing module""" -from flask import Blueprint - -parsesuccessbp = Blueprint("parse_success", __name__) - -@parsesuccessbp.route("/", methods=["GET"]) -def parse_success(): - """Indicates success if parsing the file is successful""" - return "STUB: Parse success!!!" diff --git a/qc_app/static/css/styles.css b/qc_app/static/css/styles.css index 4d569d8..d009e40 100644 --- a/qc_app/static/css/styles.css +++ b/qc_app/static/css/styles.css @@ -29,3 +29,15 @@ fieldset { background-color: #336699; font-weight: bold; } + +.alert { + display: block; + border-style: solid; + border-radius: 3px; +} + +.alert-error { + color: #A35256; + background-color: #F8D7DA; + border-color: #E7C6C9; +} diff --git a/qc_app/templates/index.html b/qc_app/templates/index.html index ec45605..9a69bc5 100644 --- a/qc_app/templates/index.html +++ b/qc_app/templates/index.html @@ -5,7 +5,18 @@ {%block contents%}

upload file

-
+ + {%with messages = get_flashed_messages(with_categories=True) %} + {%if messages %} +
+ {%for category, message in messages %} + {{message}} + {%endfor%} +
+ {%endif%} + {%endwith%} +
file type -- cgit v1.2.3