From ac3c4d353da8bc3c8156ecee11d76c52ec0ade9d Mon Sep 17 00:00:00 2001 From: Remi Gau Date: Tue, 28 May 2024 15:31:30 +0200 Subject: [PATCH] Apply suggestions from code review Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- ui/ecobidas_ui/generate/views.py | 8 +++++++- ui/ecobidas_ui/protocols/utils.py | 4 ++-- ui/ecobidas_ui/public/views.py | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ui/ecobidas_ui/generate/views.py b/ui/ecobidas_ui/generate/views.py index 2682f3b6..45210f29 100644 --- a/ui/ecobidas_ui/generate/views.py +++ b/ui/ecobidas_ui/generate/views.py @@ -24,7 +24,13 @@ def generate(): @blueprint.route("/download", methods=["GET"]) def download(): - tmp = md(render_template("generate/report.html"), heading_style="ATX") + try: + tmp = md(render_template("generate/report.html"), heading_style="ATX") + with open(Path(current_app.config["UPLOAD_FOLDER"]) / "report.md", "w") as f: + f.write(tmp) + except IOError as e: + current_app.logger.error(f"File write operation failed: {e}") + return "An error occurred while generating the report.", 500 with open(Path(current_app.config["UPLOAD_FOLDER"]) / "report.md", "w") as f: f.write(tmp) return send_from_directory(current_app.config["UPLOAD_FOLDER"], "report.md") diff --git a/ui/ecobidas_ui/protocols/utils.py b/ui/ecobidas_ui/protocols/utils.py index a103e74d..42252881 100644 --- a/ui/ecobidas_ui/protocols/utils.py +++ b/ui/ecobidas_ui/protocols/utils.py @@ -222,7 +222,7 @@ def get_choices(item_data): if "https://raw.githubusercontent.com/ohbm/cobidas_schema/master/" in choices: choices = choices.replace( "https://raw.githubusercontent.com/ohbm/cobidas_schema/master/", - str(local_cobidas_schema()) + "/", + f"{str(local_cobidas_schema())}/", ) with open(choices) as f: choices = json.load(f) @@ -255,7 +255,7 @@ def extract_values_participants(df, json_content): class Found: def __init__(self, df, age_column) -> None: - self.number_of_subjects = int(len(df)) + self.number_of_subjects = len(df) self.subject_age_mean = float(df[age_column].mean()) self.subject_age_min = df[age_column].min() self.subject_age_max = df[age_column].max() diff --git a/ui/ecobidas_ui/public/views.py b/ui/ecobidas_ui/public/views.py index 88829745..2ee4bf4c 100644 --- a/ui/ecobidas_ui/public/views.py +++ b/ui/ecobidas_ui/public/views.py @@ -16,7 +16,8 @@ def home(): @blueprint.route("/faq/") def faq(): - data = json.load(open(STATIC_FOLDER / "json" / "faq.json")) + with open(STATIC_FOLDER / "json" / "faq.json") as f: + data = json.load(f) return render_template("public/faq.html", data=data)