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 | |
parent | b419a43c851336aeb57407c72c0a37271fbb2ebc (diff) | |
download | gn-uploader-063a9529c49df5f21d83c973f501db09161542cd.tar.gz |
Implement file upload
-rw-r--r-- | etc/default_config.py | 1 | ||||
-rw-r--r-- | qc_app/__init__.py | 6 | ||||
-rw-r--r-- | qc_app/entry.py | 46 | ||||
-rw-r--r-- | qc_app/parse.py | 14 | ||||
-rw-r--r-- | qc_app/parse_error.py | 9 | ||||
-rw-r--r-- | qc_app/parse_success.py | 9 | ||||
-rw-r--r-- | qc_app/static/css/styles.css | 12 | ||||
-rw-r--r-- | qc_app/templates/index.html | 13 |
8 files changed, 85 insertions, 25 deletions
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"<Please! Please! Please! Change This!>" +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/<filename>", 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%} <h1 class="heading">upload file</h1> -<form action="#" method="POST" enctype="multipart/form-data"> +<form action="{{url_for('entry.upload_file')}}" + method="POST" enctype="multipart/form-data"> + {%with messages = get_flashed_messages(with_categories=True) %} + {%if messages %} + <div class="alerts"> + {%for category, message in messages %} + <span class="alert {{category}}">{{message}}</span> + {%endfor%} + </div> + {%endif%} + {%endwith%} + <fieldset> <legend>file type</legend> |