diff options
author | Frederick Muriuki Muriithi | 2024-02-09 04:22:51 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-02-12 18:17:38 +0300 |
commit | 4c077c01db19e8adc01d9559677ad5693a1db909 (patch) | |
tree | 3aeedc2ef8b18aa5a6bddb162531487687ae11e9 | |
parent | dd369b846524fed0c08d1b7318fd73478506c3ee (diff) | |
download | gn-uploader-4c077c01db19e8adc01d9559677ad5693a1db909.tar.gz |
Raise error if file is missing rather than returning a Union value.
-rw-r--r-- | qc_app/files.py | 6 | ||||
-rw-r--r-- | qc_app/samples.py | 9 | ||||
-rw-r--r-- | qc_app/upload/rqtl2.py | 16 |
3 files changed, 17 insertions, 14 deletions
diff --git a/qc_app/files.py b/qc_app/files.py index baac5ec..b163612 100644 --- a/qc_app/files.py +++ b/qc_app/files.py @@ -1,17 +1,15 @@ """Utilities to deal with uploaded files.""" import hashlib from pathlib import Path -from typing import Union from datetime import datetime from flask import current_app from werkzeug.utils import secure_filename from werkzeug.datastructures import FileStorage -def save_file(fileobj: FileStorage, upload_dir: Path) -> Union[Path, bool]: +def save_file(fileobj: FileStorage, upload_dir: Path) -> Path: """Save the uploaded file and return the path.""" - if not bool(fileobj): - return False + assert bool(fileobj), "Invalid file object!" hashed_name = hashlib.sha512( f"{fileobj.filename}::{datetime.now().isoformat()}".encode("utf8") ).hexdigest() diff --git a/qc_app/samples.py b/qc_app/samples.py index 55e94ab..1845818 100644 --- a/qc_app/samples.py +++ b/qc_app/samples.py @@ -219,9 +219,12 @@ def upload_samples(): flash("Invalid grouping/population!", "alert-error") return samples_uploads_page - samples_file = save_file(request.files["samples_file"], Path(app.config["UPLOAD_FOLDER"])) - if not bool(samples_file): - flash("You need to provide a file with the samples data.") + try: + samples_file = save_file(request.files["samples_file"], + Path(app.config["UPLOAD_FOLDER"])) + except AssertionError: + flash("You need to provide a file with the samples data.", + "alert-error") return samples_uploads_page firstlineheading = (request.form.get("first_line_heading") == "on") diff --git a/qc_app/upload/rqtl2.py b/qc_app/upload/rqtl2.py index afe0c2e..b96d9f0 100644 --- a/qc_app/upload/rqtl2.py +++ b/qc_app/upload/rqtl2.py @@ -152,13 +152,15 @@ def upload_rqtl2_bundle(species_id: int, population_id: int): species=species, population=population) - if not bool(request.files.get("rqtl2_bundle_file")): - raise __RequestError__("No R/qtl2 zip bundle provided.") - - the_file = save_file(request.files["rqtl2_bundle_file"], - Path(app.config["UPLOAD_FOLDER"])) - if not bool(the_file): - raise __RequestError__("Please provide a valid R/qtl2 zip bundle.") + try: + the_file = save_file(request.files["rqtl2_bundle_file"], + Path(app.config["UPLOAD_FOLDER"])) + except AssertionError: + flash("Please provide a valid R/qtl2 zip bundle.", + "alert-error error-rqtl2") + return redirect(url_for("upload.rqtl2.upload_rqtl2_bundle", + species_id=species_id, + population_id=population_id)) if not is_zipfile(str(the_file)): raise __RequestError__("Invalid file! Expected a zip file.") |