diff options
| author | Frederick Muriuki Muriithi | 2026-03-27 10:17:52 -0500 |
|---|---|---|
| committer | Frederick Muriuki Muriithi | 2026-03-27 10:33:18 -0500 |
| commit | 9294ad848b3eda1fb3460df5d0c4c6d836644a31 (patch) | |
| tree | 296d263846ebfbb12551c808c6de11e88a776b06 | |
| parent | 8ec91b6e50fe7f103ab3c10ab7dea89822ff051f (diff) | |
| download | gn-uploader-9294ad848b3eda1fb3460df5d0c4c6d836644a31.tar.gz | |
genotypes: Refactor to consistently use monads.
| -rw-r--r-- | uploader/genotypes/views.py | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/uploader/genotypes/views.py b/uploader/genotypes/views.py index 335fc6b..3fa2131 100644 --- a/uploader/genotypes/views.py +++ b/uploader/genotypes/views.py @@ -2,6 +2,7 @@ import logging from MySQLdb.cursors import DictCursor +from pymonad.either import Left, Right, Either from gn_libs.mysqldb import database_connection from flask import (flash, request, @@ -138,13 +139,19 @@ def create_dataset(species: dict, population: dict, **kwargs):# pylint: disable= with (database_connection(app.config["SQL_URI"]) as conn, conn.cursor(cursorclass=DictCursor) as cursor): - form = request.form - new_dataset = save_new_dataset( - cursor, - population["Id"], - form["geno-dataset-name"], - form["geno-dataset-fullname"], - form["geno-dataset-shortname"]) + def __save_dataset__() -> Either: + form = request.form + try: + return Right(save_new_dataset( + cursor, + population["Id"], + form["geno-dataset-name"], + form["geno-dataset-fullname"], + form["geno-dataset-shortname"])) + except Exception: + msg = "Error adding new Genotype dataset to database." + logger.error(msg, exc_info=True) + return Left(Exception(msg)) def __success__(_success): flash("Successfully created genotype dataset.", "alert-success") @@ -153,18 +160,20 @@ def create_dataset(species: dict, population: dict, **kwargs):# pylint: disable= species_id=species["SpeciesId"], population_id=population["Id"])) - return oauth2_post( - "auth/resource/genotypes/create", - json={ - **dict(request.form), - "species_id": species["SpeciesId"], - "population_id": population["Id"], - "dataset_id": new_dataset["Id"], - "dataset_name": form["geno-dataset-name"], - "dataset_fullname": form["geno-dataset-fullname"], - "dataset_shortname": form["geno-dataset-shortname"], - "public": "on" - } + return __save_dataset__().then( + lambda new_dataset: oauth2_post( + "auth/resource/genotypes/create", + json={ + **dict(request.form), + "species_id": species["SpeciesId"], + "population_id": population["Id"], + "dataset_id": new_dataset["Id"], + "dataset_name": new_dataset["Name"], + "dataset_fullname": new_dataset["FullName"], + "dataset_shortname": new_dataset["ShortName"], + "public": "on" + } + ) ).either( make_either_error_handler( "There was an error creating the genotype dataset."), |
